Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-26T19:07:12.163Z
Answers.Text: 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.
Answers.UserDisplayName: Martin Adams
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-26T19:16:24.340Z
Answers.Text: 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.
Answers.UserDisplayName: Martin Adams
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-26T05:06:07.093Z
Answers.LastActivityDate: 2015-04-26T17:04:03.210Z
Answers.LastEditDate: 2015-04-26T17:04:03.210Z
Answers.UserDisplayName: Douglas McDonald
Body: <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>
  
Tags: <matrix><url><image-transforms>
Title: getUrl() not working
ThreadText: # getUrl() not working
  
  (Asked by: username_0 on 2015-04-26)
  
  I have the following code in the “main.twig” template:
  
  ```
  <head>
      {% block head %}
          <meta property="og:image" content="{% block pageImage %}{% endblock %}" />
      {% endblock %}
  </head>
  
  ```
  
  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':
  
  ```
  <head>
      <meta property="og:image" content="{% if seoImage is defined %}{{ seoImage }}{% endif %}" />
  </head>
  
  ```
  
  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, username_1! I also didn’t know I had to define the variable first `{% set seoImage="" %}` before assigning it a value! Much appreciated.  
  
  ---
  
  