Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twig how to display text only at the first page of pagination

I've got OpenCart and theme using twig, how can I display my text only at the first page of the category and do not show it at the pagination pages.

My template looks like this

{% if description %}
    <div class="row" style="margin-bottom:50px;">
        {{ description }}
    </div>
{% endif %}
like image 454
Danyl Avatar asked Oct 15 '22 02:10

Danyl


1 Answers

This check should not be executing in .twig template. This is a task for controller.

Go to catalog/controller/product/category.php

Find

$data['description'] = html_entity_decode($category_info['description'], ENT_QUOTES, 'UTF-8');

Replaced it with

if ($page == 1) {
    $data['description'] = html_entity_decode($category_info['description'], ENT_QUOTES, 'UTF-8');
} else {
    $data['description'] = '';
}

Tested, works perfect. No template edition needed. Restore your .twig file to the original condition.

If you don't see any changes - clear caches according to this instruction https://stackoverflow.com/a/61524855/3187127


UPDATED

Or, if you want to check anything else in template

Go to catalog/controller/product/category.php

Find

if (isset($this->request->get['page'])) {
    $page = $this->request->get['page'];
} else {
    $page = 1;
}

Add below

if ($page == 1) {
    $data['firstpage'] = 1;
} else {
    $data['firstpage'] = '';
}

Now on your catalog/view/theme/YOUR_THEME/template/product/category.twig you can make check like this

{% if firstpage %}
    This is the first page.
{% endif %}

UPDATED 2 After previous manipulation your code should be like this

if (isset($this->request->get['page'])) {
    $page = $this->request->get['page'];
} else {
    $page = 1;
}

if ($page == 1) {
    $data['firstpage'] = 1;
} else {
    $data['firstpage'] = '';
}
like image 53
focus.style Avatar answered Nov 11 '22 19:11

focus.style