Set Variables from the Matrix Field - SEO quick question.

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

{% if descr is defined %}
    &lt;meta name="description" content="{{ descr }}" /&gt;
{% endif %}


meta description won't show up? what am I doing wrong?
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 %}
    &lt;meta name="description" content="{{ descr }}" /&gt;
{% 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.