Answers.Body: <p>I didn't have a full understanding of how blocks work, but I've solved my problem. I think because I was trying to set the variable in my template <em>outside of the block</em> (and I was trying to bring it into my _layout.twig file without referencing the block I was calling) nothing was getting properly passed from one file to the other. The solution I'm now using looks like this:</p>
  <p>_layout.twig:</p>
  <pre><code>&lt;meta property=&quot;og:image&quot; content=&quot;{% block ogImage %}{% endblock %}&quot; /&gt;
  </code></pre>
  <p>template.twig:</p>
  <pre><code>{% block ogImage %}
      {% for image in entry.eventImage.all() %}
          {{ image.url() }}
      {% endfor %}
  {% endblock %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-18T18:29:19.947Z
Answers.LastActivityDate: 2020-11-18T18:29:19.947Z
Answers.UserDisplayName: Dannie
Body: <p>I found it pretty straightforward to get something like dynamic page titles working by setting a variable at the template level and pulling that into my site's meta tags:</p>
  <p>My _layout.twig:</p>
  <pre><code>&lt;head&gt;
  {% if title is defined %}
  &lt;title&gt;{{ title }}&lt;/title&gt;
  {% endif %}
  &lt;/head&gt;
  </code></pre>
  <p>My template.twig:</p>
  <pre><code>{% set title = entry.title %}
  </code></pre>
  <p>But what's the best practice for pulling something like an asset URL that needs queried from within {% block content %}? I'm running a for loop on my asset field to grab the Url from within the template, but this doesn't seem to work outside of <code>{% block content %}</code>, and if I run it from within <code>{% block content %}</code> the variable doesn't carry over to _layout.twig where I need it in the meta tag.</p>
  <p>My _layout.twig looks like this:</p>
  <pre><code>{% if openGraphImage is defined %}
  &lt;meta property=&quot;og:image&quot; content=&quot;{{openGraphImage}}&quot; /&gt;
  {% endif %}
  </code></pre>
  <p>But how can I define an image Url that's coming from within an entry query and have it pass back to the meta tag?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-11-18T14:46:05.363Z
Comments.Text: Hey, can you share the code you've tried for setting the image Url?
Comments.UserDisplayName: dmatthams
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-11-18T18:30:58.313Z
Comments.Text: @dmatthams Thanks for your comment! I was trying to set a variable (and pass it into my _layout.twig file outside of a {% block %} tag. Doing so fixed it and I added the working code I'm now using as an answer below.
Comments.UserDisplayName: Dannie
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-11-19T09:12:04.780Z
Comments.Text: Glad you sorted it :)
Comments.UserDisplayName: dmatthams
Tags: <templating>
Title: Getting an asset Url from an entry loop into a meta tag
ThreadText: # Getting an asset Url from an entry loop into a meta tag
  
  (Asked by: username_0 on 2020-11-18)
  
  I found it pretty straightforward to get something like dynamic page titles working by setting a variable at the template level and pulling that into my site's meta tags:
  
  My \_layout.twig:
  
  ```
  <head>
  {% if title is defined %}
  <title>{{ title }}</title>
  {% endif %}
  </head>
  
  ```
  
  My template.twig:
  
  ```
  {% set title = entry.title %}
  
  ```
  
  But what's the best practice for pulling something like an asset URL that needs queried from within {% block content %}? I'm running a for loop on my asset field to grab the Url from within the template, but this doesn't seem to work outside of `{% block content %}`, and if I run it from within `{% block content %}` the variable doesn't carry over to \_layout.twig where I need it in the meta tag.
  
  My \_layout.twig looks like this:
  
  ```
  {% if openGraphImage is defined %}
  <meta property="og:image" content="{{openGraphImage}}" />
  {% endif %}
  
  ```
  
  But how can I define an image Url that's coming from within an entry query and have it pass back to the meta tag?
  
  **Tags:** templating
  
  > **Comments:**
  > **username_1**: Hey, can you share the code you've tried for setting the image Url?  
  > **username_0**: @username_1 Thanks for your comment! I was trying to set a variable (and pass it into my _layout.twig file outside of a {% block %} tag. Doing so fixed it and I added the working code I'm now using as an answer below.  
  > **username_1**: Glad you sorted it :)  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2020-11-18. Score: 2
  
  I didn't have a full understanding of how blocks work, but I've solved my problem. I think because I was trying to set the variable in my template *outside of the block* (and I was trying to bring it into my \_layout.twig file without referencing the block I was calling) nothing was getting properly passed from one file to the other. The solution I'm now using looks like this:
  
  \_layout.twig:
  
  ```
  <meta property="og:image" content="{% block ogImage %}{% endblock %}" />
  
  ```
  
  template.twig:
  
  ```
  {% block ogImage %}
      {% for image in entry.eventImage.all() %}
          {{ image.url() }}
      {% endfor %}
  {% endblock %}
  
  ```
  
  
  ---
  
  