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

Viewed 640

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 %}
2 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'] = '';
}

If I'm correct you should be able to fetch the querysting parameter page inside twig with opencart like this:

{{ app.request.query.get('page') }}

You could use this to test if your at the first page of your pagination with

{% if app.request.query.get('page')|default(1) == 1 %}
     You are at the first page
{% endif %}
Related