简体   繁体   中英

OpenCart 3.x - including a new template file

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.

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

Ie:

{{ 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 .

I haven't learn OpenCart 3.x, but I guess you need to change

return $this->load->view('common/header', $data);

to

return $this->load->view('common/header_home', $data);

in catalog/controller/common/header.php .

In order to connect another template via the {{header_home}} construct, you need to create the header_home.php in the folder /controller/common/directory , create the header_home.twig file in the theme /*theme_name*/template/common/ folder and in the controller , which is responsible for the formation of the page, for example in the directory /controller/common/home.php

add a line:

'$ data [' header_home '] = $ this-> load-> controller (' common / header_home '); '

before:

'$ this-> response-> setOutput ($ this-> load-> view (' common / home ', $ data)); '

The answer of @bogalakon is right but if you update your OpenCart core files you will lose your changes so, I suggest you to copy the header.php controller file and rename it to header_home.php and then edit the file and replace the line

return $this->load->view('common/header', $data);

with:

return $this->load->view('common/header_home', $data);

There are several ways to give a different value.

For example:

Consider the controller

$data['header'] = $this-> load-> controller ('common / header');

A variable {{header}} is available in the template

You can place a new template in another file ( header_home.twig ), and then you can immediately download a new template for use:

$data['**header**'] = $this-> load-> controller ('common / ***header_home***');

If you want use {{ header }} in the template.

Or,

$data['**header_home**'] = $this-> load-> controller ('common / header_home');

For use {{ header_home }} in the template.

To add your own custom twig file and include it inside another existing one you need to do three things:

  1. Create the actual custom twig file. In your case it will be: catalog/view/theme/customtheme/template/common/header_home.twig
  2. Create a controller for that twig file. In your case you can just copy: /catalog/controller/common/header.php and rename that to header_home.php . Edit this controller and change the class name to ControllerCommonHeaderHome usually on line 2.
  3. Lastly, since you are going to include header_home inside home.twig , edit /catalog/controller/common/home.php and add the line $data['header'] = $this->load->controller('common/header_home'); after $data['header'] = $this->load->controller('common/header');

That's it. After you've done the steps above, you can now include {{ header_home }} inside home.twig . If you are editing the files directly, I find that sometimes I need to login to the admin of the website, go to design>theme editor, open up the files I added or changed, hit reset, and save. Refresh your website and you should see the changes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM