Create a variable from content in a matrix field 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!
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 %}