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?

1 Answer 1

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 %}
2
  • Is then maybe option to set variable inside included file so that it is accessible inside parent file?
    – saque tri
    Commented Aug 15, 2018 at 23:02
  • That's not possible due to the scopes of Twig. You'll need a plugin for it or change the structure Commented Aug 16, 2018 at 5:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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