2

I have pagination twig component that needs to inject seo links into of page (rel canonical).

I tried to use {% block %} twig tag for this, but problem is that pagination component is already included in another {% block %} tag containing main content of page.

In short, im trying to do this:

layout.twig:

<html>
<head>
{% block seo %}{% endblock %}
</head>
<body>
{% block main %}{% endblock %}
</body>
</html>

page.twig:

{% extends 'layout.twig' %}

{% block main %}

some content..
{% include 'pagination.twig' %}

{% endblock %}

pagination.twig:

... pagination links

{% block seo %}
<link href="url" rel="canonical">
{% endblock %}

When im using this code, block seo is empty. Can this be achived some other way?

2

Unfortunately it’s not possible for an included template to override the contents of a block in the parent template, or its layout.

You’d need to just override the seo block from page.twig do whatever you need to do from there instead.

{% extends 'layout.twig' %}

{% block main %}
  some content..
  {% include 'pagination.twig' %}
{% endblock %}

{% block seo %}
  <link href="url" rel="canonical">
{% endblock %}
| improve this answer | |
  • Is then maybe option to set variable inside included file so that it is accessible inside parent file? – saque tri Aug 15 '18 at 23:02
  • That's not possible due to the scopes of Twig. You'll need a plugin for it or change the structure – Robin Schambach Aug 16 '18 at 5:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.