How to set and test a variable in child template to modify a block in parent template I would like to load some javascript on specific pages only.

I'm trying to set a block in my parent template, then test a variable and include the javascript in the child template only when necessary. 

I thought I could do this by declaring the variable beforehand, to make it  accessible outside it's scope, but this is not working (nothing is returned):

Parent '_layout' template:

&lt;html&gt;
    &lt;head&gt;...&lt;/head&gt;
    &lt;body&gt;

        {% block content %}
        {% endblock %}

        ...

        {# site-wide script #}
        &lt;script src="global.js"&gt;&lt;/script&gt;

        {# page-specific script #}
        {% block script %}
        {% endblock %}

    &lt;/body&gt;
&lt;/html&gt;


Child template:

{% extends "_layout" %}

{% set page_assets = '' %}

{% block content %}
...
{% set page_assets = 'true' %}
...
{% endblock %}

{% block script %}
    {% if page_assets is defined and page_assets is not empty %}
      &lt;script src="page-specific.js"&gt;&lt;/script&gt;
    {% endif %}
{% endblock %}
Nope, that's not going to work unfortunately, due to the way scoping works in Twig. Check out the explanation in the Twig Processing Order &amp; Scope article