<p>This is a <a href="http://twig.sensiolabs.org/doc/tags/set.html">twig thing</a>. Loops in twig are scoped. Variables defined in a loop cannot be used outside of that loop. To fix this, you simply have to declare the variable before the loop:</p>

<pre><code>{% set backgroundUrl = "" %}
{% for item in entry.screenshot %}
    {% for asset in item.webpage %}
        {% set backgroundUrl = asset.url %}
   {% endfor %}
{% endfor %}
</code></pre>

<p>If you know that you only have one asset you can shorten it a bit with:</p>

<pre><code>{% set backgroundUrl = "" %}
{% for item in entry.screenshot %}
    {% set backgroundUrl = item.webpage.first().url %}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2014-07-23T21:13:44.447Z
Thanks, Bryan Redeagle!  It did seem like a scope issue, but did not know it was a Twig issue.  Much appreciated!
Roi Agneta
CC BY-SA 3.0
2014-07-24T05:44:11.687Z
You can shorten it even further with just this: `{% set backgroundUrl = entry.screenshot[0].webpage.first().url %}`. If that field is not a required field then I'd drop a conditional around it as well: this: `{% set backgroundUrl = entry.screenshot | length ? entry.screenshot[0].webpage.first().url : 'no screenshot!' %}`
Josh Angell
CC BY-SA 3.0
2014-07-23T19:49:41.623Z
2014-07-23T19:49:41.623Z
Bryan Redeagle
<p>I want to create a variable based on the contents of a matrix block.  This is the code I created:</p>

<pre><code>{% for item in entry.screenshot %} //my matrix field
    {% for asset in item.webpage %} // an asset field in the block
        {% set backgroundUrl = asset.url %} //want to declare a variable using the asset url
   {% endfor %}
{% endfor %}
</code></pre>

<p>This throws a "backgroundUrl not defined" error when I try to use the variable.  Obviously my syntax is wrong.  Any advice would be appreciated!  </p>

<matrix>
Create a variable from content in a matrix field
# Create a variable from content in a matrix field

(Asked by: username_0 on 2014-07-23)

I want to create a variable based on the contents of a matrix block.  This is the code I created:
{% for item in entry.screenshot %} //my matrix field
    {% for asset in item.webpage %} // an asset field in the block
        {% set backgroundUrl = asset.url %} //want to declare a variable using the asset url
   {% endfor %}
{% endfor %}

This throws a "backgroundUrl not defined" error when I try to use the variable.  Obviously my syntax is wrong.  Any advice would be appreciated!

**Tags:** matrix

---

## 1 answers

### Answer by username_1 on 2014-07-23. Score: 6

This is a twig thing. Loops in twig are scoped. Variables defined in a loop cannot be used outside of that loop. To fix this, you simply have to declare the variable before the loop:
{% set backgroundUrl = "" %}
{% for item in entry.screenshot %}
    {% for asset in item.webpage %}
        {% set backgroundUrl = asset.url %}
   {% endfor %}
{% endfor %}

If you know that you only have one asset you can shorten it a bit with:
{% set backgroundUrl = "" %}
{% for item in entry.screenshot %}
    {% set backgroundUrl = item.webpage.first().url %}
{% endfor %}

> **Comments:**
> **username_0**: Thanks, Bryan Redeagle!  It did seem like a scope issue, but did not know it was a Twig issue.  Much appreciated!  
> **username_2**: You can shorten it even further with just this: `{% set backgroundUrl = entry.screenshot[0].webpage.first().url %}`. If that field is not a required field then I'd drop a conditional around it as well: this: `{% set backgroundUrl = entry.screenshot | length ? entry.screenshot[0].webpage.first().url : 'no screenshot!' %}`  

---

