<p>Twig generally doesn't like nested/un-nested blocks like this. A better way to deal with this would be to just set a variable; and since twig renders templates from the inside-out, any variables that you define in child-templates should be available to the parent.</p>

<p>So in 'main.twig':</p>

<pre><code>&lt;head&gt;
    &lt;meta property="og:image" content="{% if seoImage is defined %}{{ seoImage }}{% endif %}" /&gt;
&lt;/head&gt;
</code></pre>

<p>And in your child-template:</p>

<pre><code>{% extends "templates/main" %}

{# note: needs to be defined outside the 'for' loop to become available #}
{% set seoImage="" %} 
{% for block in entry.articleBody %}
    {% if block.type == "articleImage" %}
        {% set seoImage = block.articleImage.first().getUrl() %}
    {% endif %}
{% endfor %}
</code></pre>

<p>You can still use blocks of course in addition to this technique, but generally best to keep them in the same nesting order.</p>

<p>On a side-note: I don't think you need the 'templates/' part of the extends statement (unless you have a 'templates' folder inside 'templates'). Normally you would just use <code>{% extends "_main" %}</code>. The underscore in the name also prevents the template from being loaded directly.</p>

CC BY-SA 3.0
2015-04-26T19:07:12.163Z
Also, yes, I do have a ‘templates’ folder inside ‘craft/templates’—a bit confusing, but helps me distinguish templates called by Routing from child templates, which I put into a separate ‘includes’ folder.
Martin Adams
CC BY-SA 3.0
2015-04-26T19:16:24.340Z
That did it! Thank you, Douglas! I also didn’t know I had to define the variable first `{% set seoImage="" %}` before assigning it a value! Much appreciated.
Martin Adams
CC BY-SA 3.0
2015-04-26T05:06:07.093Z
2015-04-26T17:04:03.210Z
2015-04-26T17:04:03.210Z
Douglas McDonald
<p>I have the following code in the “main.twig” template:</p>

<pre><code>&lt;head&gt;
    {% block head %}
        &lt;meta property="og:image" content="{% block pageImage %}{% endblock %}" /&gt;
    {% endblock %}
&lt;/head&gt;
</code></pre>

<p>Then, I’m trying to populate the image URL into the &lt; meta > tag from a Matrix field:</p>

<pre><code>{% extends "templates/main" %}
{% for block in entry.articleBody %}
    {% if block.type == "articleImage" %}
        {% set image = block.articleImage.first().getUrl() %}
        {% block pageImage %}{{ image }}{% endblock %}
    {% endif %}
{% endfor %}
{% block head %}{{ parent() }}{% endblock %}
</code></pre>

<p>However, no URL is outputted. Does anyone know why?</p>

<p>Thanks!</p>

<matrix><url><image-transforms>
getUrl() not working
# getUrl() not working

(Asked by: username_0 on 2015-04-26)

I have the following code in the “main.twig” template:

    {% block head %}
        
    {% endblock %}


Then, I’m trying to populate the image URL into the < meta > tag from a Matrix field:
{% extends "templates/main" %}
{% for block in entry.articleBody %}
    {% if block.type == "articleImage" %}
        {% set image = block.articleImage.first().getUrl() %}
        {% block pageImage %}{{ image }}{% endblock %}
    {% endif %}
{% endfor %}
{% block head %}{{ parent() }}{% endblock %}

However, no URL is outputted. Does anyone know why?
Thanks!

**Tags:** matrix, url, image-transforms


---

## 1 answers

### Answer by username_1 on 2015-04-26. Score: 4

Twig generally doesn't like nested/un-nested blocks like this. A better way to deal with this would be to just set a variable; and since twig renders templates from the inside-out, any variables that you define in child-templates should be available to the parent.
So in 'main.twig':

    


And in your child-template:
{% extends "templates/main" %}

{# note: needs to be defined outside the 'for' loop to become available #}
{% set seoImage="" %} 
{% for block in entry.articleBody %}
    {% if block.type == "articleImage" %}
        {% set seoImage = block.articleImage.first().getUrl() %}
    {% endif %}
{% endfor %}

You can still use blocks of course in addition to this technique, but generally best to keep them in the same nesting order.
On a side-note: I don't think you need the 'templates/' part of the extends statement (unless you have a 'templates' folder inside 'templates'). Normally you would just use {% extends "_main" %}. The underscore in the name also prevents the template from being loaded directly.

> **Comments:**
> **username_0**: Also, yes, I do have a ‘templates’ folder inside ‘craft/templates’—a bit confusing, but helps me distinguish templates called by Routing from child templates, which I put into a separate ‘includes’ folder.  
> **username_0**: That did it! Thank you, Douglas! I also didn’t know I had to define the variable first `{% set seoImage="" %}` before assigning it a value! Much appreciated.  

---

