title: Twig: Add item to array from within an included template
body: <p>How can you add an item to an array within an <code>include</code> tag when the array is defined in the main file? </p>
  
  <p>Please note this question is distinctly different from other array questions in Twig because I'm trying to modify the value from within an included template in the main file.</p>
  
  <p>For example:</p>
  
  <p><strong>main.twig</strong></p>
  
  <pre><code>{% set includes = ["test1"] %}
  {% include "test.twig" %}
  {{includes|json_encode}} {# "How do I make this `includes` contain the "test2" array item?" #}
  </code></pre>
  
  <p><strong>test.twig</strong></p>
  
  <pre><code>{% set includes = includes|merge(["test2"]) %}
  {{includes|json_encode}}
  </code></pre>
  
  <p>Currently the output from <code>main.twig</code> gives me only the initial <code>test1</code> item, and not the combination of the two which <code>test.twig</code> is trying to modify.</p>
  
  <p>I may have to utilize an added function. The goal here is basically to allow <code>include</code>d templates the ability to add other CSS, JS, etc, in HTML files without having to modify the parent templates.</p>
  
  <p>Here is a <a href="https://twigfiddle.com/0u825a" rel="nofollow noreferrer">Twig Fiddle</a> for you to play with which shows the output.</p>
  
  <blockquote>
    <p>Please note for other people looking for an answer to this question, what I was attempting to accomplish is possible utilizing a Twig extension with a function that adds items to the array, and a second function to access it. Please view my answer below, that is not marked as correct, to see a way to potentially do this. My question, as stated, is impossible, as shown by DarkBee.</p>
  </blockquote>