Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

Page title, breadcrumbs and pagination

A finished page needs three more things besides content: a title in the browser tab, a path from the home page (breadcrumbs) and, for long lists, links to the next pages. The first is set through the $this->document object, the other two are arrays the controller passes to the template.

The document object

$this->document collects what the header prints inside the head tag. It must be filled before the header controller is called:

// catalog/controller/information/hello.php
$this->load->language('information/hello');
 
$this->document->setTitle($this->language->get('heading_title'));
 
$data['header'] = $this->load->controller('common/header');

Open index.php?route=information/hello — the browser tab shows «Hello».

MethodWhat it sets
setTitle($title)Page title
setDescription($text)Meta description
setKeywords($text)Meta keywords
addStyle($href)A stylesheet
addScript($href)A script
addLink($href, $rel)A link tag, for example the canonical address

The controller builds an array, from the home page to the current one:

$language = 'language=' . $this->config->get('config_language');
 
$data['breadcrumbs'] = [];
 
$data['breadcrumbs'][] = [
    'text' => $this->language->get('text_home'),
    'href' => $this->url->link('common/home', $language),
];
 
$data['breadcrumbs'][] = [
    'text' => $this->language->get('heading_title'),
    'href' => $this->url->link('information/hello', $language),
];

The template prints it right after the header:

<ul class="breadcrumb">
  {% for breadcrumb in breadcrumbs %}
    <li class="breadcrumb-item"><a href="{{ breadcrumb.href }}">{{ breadcrumb.text }}</a></li>
  {% endfor %}
</ul>

The line «Home / Hello» appears above the heading.

Pagination

Page-by-page output takes three steps: read the page number, ask the model only for the rows of this page, build the links.

$page = max(1, (int) ($this->request->get['page'] ?? 1));
$limit = 4;
 
$this->load->model('catalog/product');
 
$products = $this->model_catalog_product->getProducts(['start' => ($page - 1) * $limit, 'limit' => $limit]);
$total = $this->model_catalog_product->getTotalProducts([]);

In version 4 the links are built by the controller common/pagination:

$data['pagination'] = $this->load->controller('common/pagination', [
    'total' => $total,
    'page' => $page,
    'limit' => $limit,
    'url' => $this->url->link('information/hello', $language . '&page={page}'),
]);

The text {page} in the address is replaced by the page number. The controller returns ready HTML, the template prints it with {{ pagination }}. Open index.php?route=information/hello&page=2 — the page shows the second four products and the links «1 2 3».

Common mistake

$this->document->setTitle() is called after $this->load->controller('common/header'). The header is already rendered by then, and the tab shows the shop name instead of the page title. Set everything on $this->document first, and call the header after that.

Thanks for reading!

Donate to the site — we work hard for you.