Updating Array Variable In Extending Template I have a layout template:

{% set bodyClass = [] %}

&lt;body class="{% for class in bodyClass %}{{ class }} {% endfor %}"&gt;
    {{ block('pageBody') }}
&lt;/body&gt;


And a template that extends it:

{% extends 'layout' %}

{% set bodyClass = bodyClass|merge(['myclass']) %}

{% block pageBody %}
    &lt;h1&gt;Hello World&lt;/h1&gt;
    &lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ab, consequuntur!&lt;/p&gt;
{% endblock %}


When I try to append to the bodyClass variable I get the error: 


  Variable "bodyClass" does not exist.


However if I call the variable:

{% for class in bodyClass %}
  {{ class  }}
{% endfor %}


The template renders fine. Why can't I merge onto a variable that's been set in an extended template? Is there another way to do this?
You can only access the parent's variable, not modify it. You could however simply add another block specifically for the body class, like so:

Layout template:

&lt;body class="{% block body_class %}default-class-1 default-class-2{% endblock %}"&gt;
    {{ block('pageBody') }}
&lt;/body&gt;


Template that extends it:

{% extends 'layout' %}

{% block body_class %}{{ parent() }} myclass{% endblock %}

{% block pageBody %}
    &lt;h1&gt;Hello World&lt;/h1&gt;
    &lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ab, consequuntur!&lt;/p&gt;
{% endblock %}


Note the call to parent() that includes the default classes defined in the parent template.