Answers.Body: <p>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:</p>
  
  <p>Layout template:</p>
  
  <pre><code>&lt;body class="{% block body_class %}default-class-1 default-class-2{% endblock %}"&gt;
      {{ block('pageBody') }}
  &lt;/body&gt;
  </code></pre>
  
  <p>Template that extends it:</p>
  
  <pre><code>{% 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 %}
  </code></pre>
  
  <p>Note the call to parent() that includes the default classes defined in the parent template.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-18T04:24:03.290Z
Answers.Text: Thanks. Not 100% ideal since I won't be able to control the spacing before and after the classes, but I guess it's good enough.
Answers.UserDisplayName: kmgdev
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-17T14:38:17.697Z
Answers.LastActivityDate: 2018-12-17T14:38:17.697Z
Answers.UserDisplayName: Udo
Answers.Body: <p>I often just declare a second (optional) variable in each template, with the default classes set in a layout, and merged with any additional ones:</p>
  
  <pre><code>{# __layout/base.twig #}
  
  {% set defaultBodyClasses = ['app'] %}
  {# Use `default([])` to make this a non-mandatory assignment in your templates: #}
  {% set bodyClasses = templateProvidedBodyClasses | default([]) | merge(defaultBodyClasses) %}
  
  &lt;body class="{{ bodyClasses | join(' ') }}"&gt;...&lt;/body&gt;
  </code></pre>
  
  <p>…and then in a template, say rendering an Entry…</p>
  
  <pre><code>{# _pages/page.twig #}
  
  {% extends '__layouts/base' %}
  
  {% set templateProvidedBodyClasses = [
      'app--my-static-flag',
      "app--section-#{entry.section.handle}",
      "app--type-#{entry.type.handle}"
  ] %}
  </code></pre>
  
  <p>Hope this helps illuminate another option!</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-06T02:34:37.973Z
Answers.LastActivityDate: 2019-01-06T02:34:37.973Z
Answers.UserDisplayName: August Miller
Body: <p>I have a layout template:</p>
  
  <pre><code>{% set bodyClass = [] %}
  
  &lt;body class="{% for class in bodyClass %}{{ class }} {% endfor %}"&gt;
      {{ block('pageBody') }}
  &lt;/body&gt;
  </code></pre>
  
  <p>And a template that extends it:</p>
  
  <pre><code>{% 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 %}
  </code></pre>
  
  <p>When I try to append to the <code>bodyClass</code> variable I get the error: </p>
  
  <blockquote>
    <p>Variable "bodyClass" does not exist.</p>
  </blockquote>
  
  <p>However if I call the variable:</p>
  
  <pre><code>{% for class in bodyClass %}
    {{ class  }}
  {% endfor %}
  </code></pre>
  
  <p>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?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-12-17T00:19:48.823Z
Comments.Text: I’m not certain that parent template variables are in scope in children. https://twig.symfony.com/doc/2.x/tags/extends.html
Comments.UserDisplayName: Chattervast
Tags: <templating><variable><array>
Title: Updating Array Variable In Extending Template
ThreadText: # Updating Array Variable In Extending Template
  
  (Asked by: username_0 on 2018-12-16)
  
  I have a layout template:
  
  ```
  {% set bodyClass = [] %}
  
  <body class="{% for class in bodyClass %}{{ class }} {% endfor %}">
      {{ block('pageBody') }}
  </body>
  
  ```
  
  And a template that extends it:
  
  ```
  {% extends 'layout' %}
  
  {% set bodyClass = bodyClass|merge(['myclass']) %}
  
  {% block pageBody %}
      <h1>Hello World</h1>
      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ab, consequuntur!</p>
  {% 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?
  
  **Tags:** templating, variable, array
  
  > **Comments:**
  > **username_1**: I’m not certain that parent template variables are in scope in children. https://twig.symfony.com/doc/2.x/tags/extends.html  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2018-12-17. Score: 4 | Accepted answer
  
  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:
  
  ```
  <body class="{% block body_class %}default-class-1 default-class-2{% endblock %}">
      {{ block('pageBody') }}
  </body>
  
  ```
  
  Template that extends it:
  
  ```
  {% extends 'layout' %}
  
  {% block body_class %}{{ parent() }} myclass{% endblock %}
  
  {% block pageBody %}
      <h1>Hello World</h1>
      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ab, consequuntur!</p>
  {% endblock %}
  
  ```
  
  Note the call to parent() that includes the default classes defined in the parent template.
  
  > **Comments:**
  > **username_0**: Thanks. Not 100% ideal since I won't be able to control the spacing before and after the classes, but I guess it's good enough.  
  
  ---
  
  ### Answer by username_3 on 2019-01-06. Score: 0
  
  I often just declare a second (optional) variable in each template, with the default classes set in a layout, and merged with any additional ones:
  
  ```
  {# __layout/base.twig #}
  
  {% set defaultBodyClasses = ['app'] %}
  {# Use `default([])` to make this a non-mandatory assignment in your templates: #}
  {% set bodyClasses = templateProvidedBodyClasses | default([]) | merge(defaultBodyClasses) %}
  
  <body class="{{ bodyClasses | join(' ') }}">...</body>
  
  ```
  
  …and then in a template, say rendering an Entry…
  
  ```
  {# _pages/page.twig #}
  
  {% extends '__layouts/base' %}
  
  {% set templateProvidedBodyClasses = [
      'app--my-static-flag',
      "app--section-#{entry.section.handle}",
      "app--type-#{entry.type.handle}"
  ] %}
  
  ```
  
  Hope this helps illuminate another option!
  
  
  ---
  
  