OpenCart 3.x - including a new template file

Leanne Seawright picture Leanne Seawright · Aug 7, 2017 · Viewed 7.2k times · Source

I need to add a new OpenCart template file into another template file.

Essentially I've created a new head file in /theme/customtheme/template/common/ called "header_home.twig".

Then in home.twig, I've changed {{ header }} to say {{ header_home }}, but it's not displaying anything.

Basically, all I did was copy header.twig and rename it to header_home.twig, and put in "xxxxx" to see if it was calling the new file, which it's not. Instead, it's not displaying anything.

Here's what my home.twig now looks like:

{{ header_home }}
<div id="common-home" class="container">
  <div class="row">{{ column_left }}
    {% if column_left and column_right %}
    {% set class = 'col-sm-6' %}
    {% elseif column_left or column_right %}
    {% set class = 'col-sm-9' %}
    {% else %}
    {% set class = 'col-sm-12' %}
    {% endif %}
    <div id="content" class="{{ class }}">{{ content_top }}{{ content_bottom }}</div>
    {{ column_right }}</div>
</div>
{{ footer }}

I assume I'm somehow missing a step when it comes to adding a new template file? If someone could help me with adding in a new twig file, that would be fantastic.

Answer

zhuiks picture zhuiks · Sep 14, 2017

You can either:

  • Change the name of Twig rendering template on the OpenCart level in the controller/common/header as @bogalakon pointed out (preferably to do this via ocmod so that your future OpenCart updates will not overwrite your hack).
  • or include another template on the Twig level

I.e.:

{{ header }} {# Original rendered to HTML OpenCart header - you can move it to your header_home.twig or just drop it #}
{% include customtheme/template/common/header_home.twig %}
<div id="common-home" class="container">
 ...

Twig is very powerful template language. And you can do much more than just a simple include! It's nice that OpenCart officially accepted it. But for now it's just Twig 1.24.2. Please refer to the Twig documentation.