1

I usually use {% block %}{% endblock %} to build my template but for the SEO plugin I'll be needing {% set %}{% endset %}. However my pages don't seem to render. If I change it to {% block %}{% endblock %} it works fine.

Anyone know what is causing this?

This is my base file _master.html (partly):

<body class="boxed bg-cover" style="background-image: url(img/backgrounds/street.jpg)">
 <div class="global-wrap">
     {% set header %}
     {% endset %}

this is my index.html (partly):

{% extends "_master" %}
 {% set header %}
   {% include 'header.html' %}
 {% endset %} 
6
  • can you show your code? May 27 '15 at 20:13
  • I edited the post. Its pretty straightforward.
    – imnotsure
    May 27 '15 at 20:23
  • 1
    You'll need to print the header variable after setting, ie {{ header }} May 27 '15 at 20:25
  • Are you sure that the SEO plugin isn't working when you use block? That's largely a formatting preference... Templates render from the inside-out, so your do would still get called before anything in the master template.
    – Lindsey D
    May 27 '15 at 21:08
  • I got a bit thrown off by this --> Note: In our examples we have chosen to use the {% set %} tag to build our layouts because it gets processed in the same way as all of the other Twig tags - from top to bottom. The {% block %} tag, while useful for many use cases in your templates, does not get processed until last, so we’re unable to update our meta data using craft.sproutSeo.meta() in a {% block %} tag because our craft.sproutSeo.optimize() tag gets processed before the {% block %} tag runs.
    – imnotsure
    May 27 '15 at 21:15
5

I think that's just the way they have set up the templates in the example, so what you're doing is just fine (using {% block %}), if that's how your templates are set up.

The difference is by using {% set %} you are just setting your content to a variable which would be {{ echoed }} into the layout file that you extend. Whereas using {% block %} you are telling twig what block of content should be inherited into the layout you are extending.

The twig docs probably explain it better:

http://twig.sensiolabs.org/doc/tags/extends.html
http://twig.sensiolabs.org/doc/tags/set.html

From the looks of it with the plugin in question, providing you add {{ craft.sproutSeo.optimize() }} into your layout template you should be all good to go.

Example with {% set %}

_layout.html

<body>
  {{ content }}
</body>

index.html

{% extends '_layout' %}

{% set content %}
 Some content
{% endset %}

Example with {% block %}

_layout.html

<body>
  {% block content %}{% endblock %}
</body>

index.html

{% extends '_layout' %}

{% block content %}
 Some content
{% endblock %}
1
  • thanks. Ill think in my case block will just be fine indeed.
    – imnotsure
    May 27 '15 at 21:17

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.