Page 1 of 1

[SOLVED] How to check if current page is the homepage in twig?

Posted: Thu Jul 03, 2025 2:06 am
by paola_84
Hello,
in version 3.0.3.8 I need to modify "column_left.twig" and "column_right.twig" so that if you are in the home page there is this:

Code: Select all

class="col-sm-6"
while if you are in any other page there is what is now by default:

Code: Select all

class="col-sm-3 hidden-xs"
How can I do it?
Thank you!

Re: How to check if current page is the homepage in twig?

Posted: Thu Jul 03, 2025 7:53 pm
by paulfeakins
paola_84 wrote:
Thu Jul 03, 2025 2:06 am
How can I do it?
There are many ways, but I would create 2 new templates:
column_left_home.twig
column_right_home.twig

Modify those as you wish and include them on the home template.

Re: How to check if current page is the homepage in twig?

Posted: Thu Jul 03, 2025 9:56 pm
by paola_84
Hi Paul, thanks for the reply! I tried but apparently I'm making some mistakes because it doesn't work. Here's what I did:

1) I created the files column_left_home.twig and column_right_home.twig

2) I created the controller files and I changed:
- the class from ControllerCommonColumnLeft to ControllerCommonColumnLeftHome
- this line from

Code: Select all

$modules = $this->model_design_layout->getLayoutModules($layout_id, 'column_left');
to

Code: Select all

$modules = $this->model_design_layout->getLayoutModules($layout_id, 'column_left_home');
- this line from

Code: Select all

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

Code: Select all

return $this->load->view('common/column_left_home', $data);
- And of course the same things for the right column.

3) In the home controller file I changed:

Code: Select all

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

Code: Select all

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

4) In the home twig file I changed:

Code: Select all

{{ column_left }}
{{ column_right }}
to

Code: Select all

{{ column_left_home }}
{{ column_right_home }}
What am I missing?

Re: How to check if current page is the homepage in twig?

Posted: Thu Jul 03, 2025 10:51 pm
by khnaz35
You could adopt this approach. Haven't tested on my end

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<modification>
    <name>Column Left Class for Homepage</name>
    <version>1.0.0</version>
    <author><![CDATA[<font color="#19700E"><b>khnaz35</b></font>]]></author>
    <link>mailto:khnaz35@gmail.com</link>
    <code>column_left_home_class_patch</code>

    <!-- Inject 'route' into column_left controller -->
    <file path="catalog/controller/common/column_left.php">
        <operation>
            <search><![CDATA[return $this->load->view('common/column_left', $data);]]></search>
            <add position="before"><![CDATA[
$data['route'] = isset($this->request->get['route']) ? $this->request->get['route'] : 'common/home';
            ]]></add>
        </operation>
    </file>

    <!-- Modify class in column_left.twig based on route -->
    <file path="catalog/view/theme/default/template/common/column_left.twig">
        <operation>
            <search><![CDATA[<aside id="column-left" class="col-sm-3 hidden-xs">]]></search>
            <replace><![CDATA[
<aside id="column-left" class="{% if route == 'common/home' %}col-sm-6{% else %}col-sm-3 hidden-xs{% endif %}">
            ]]></replace>
        </operation>
    </file>
</modification>

Re: How to check if current page is the homepage in twig?

Posted: Thu Jul 03, 2025 11:27 pm
by paola_84
It works great, thank you very much khnaz35!
Now I'm left with the doubt of what I did wrong with the other method :laugh:

Re: [SOLVED] How to check if current page is the homepage in twig?

Posted: Fri Jul 04, 2025 1:40 am
by khnaz35
Glad could help ;)