1

quick question.

{% for block in entry.matrix %}
    {% if block.type == "seo" %}
    {% set descr = block.metaDescription %}
    {% endif %}
{% endfor %}

{% if descr is defined %}
    <meta name="description" content="{{ descr }}" />
{% endif %}

meta description won't show up? what am I doing wrong?

5

Twig's for function is scoped. This means that variables defined inside of it will not be accessible outside the loop.

What you need to do, is to declare the variable with an initial value before the loop:

{% set descr = null %}

{% for block in entry.matrix %}
    {% if block.type == "seo" %}
    {% set descr = block.metaDescription %}
    {% endif %}
{% endfor %}

{% if descr != null %}
    <meta name="description" content="{{ descr }}" />
{% endif %}

This works, because in Twig the inner scope (i.e. your loop) will have access to variables belonging to its parent scope, but not vice versa.

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.