title: Twig - parse variables from imported template
body: <p>I trying to add new function to Twig Extension file which includes template and parses variables from included template. And I have no clue how to get variable from imported template. </p>
  
  <p>My Templates</p>
  
  <p><strong>Config.html</strong></p>
  
  <pre><code>{% set myVariable= "MyVariable" %} 
  </code></pre>
  
  <p><strong>template.html</strong> </p>
  
  <pre><code>{{layout("config") }} 
  
  My Config Variable: {{ myVariable }} 
  </code></pre>
  
  <p>This is my Twig Extension Class.
      
  
  <pre><code>class TwigExtensions extends \Twig_Extension {
  
  public function getFunctions()
  {
      return array(
          new \Twig_SimpleFunction(
              'layout',  array($this, 'twig_layout', ),  [ 'needs_environment' =&gt; true,  'needs_context' =&gt; true,  'is_safe' =&gt; ['all'] ]
          )
      );
  }
  
  function twig_layout(\Twig_Environment &amp;$env, &amp;$context, $template, $variables = [], $withContext = true, $ignoreMissing = false, $sandboxed = false)
  {
      $file_name      = (string)$template . ".html";
  
      if ($withContext) {
          $variables = array_merge($context, $variables);
      }
  
      $result = '';
      try {
          $result = $env-&gt;resolveTemplate($file_name)-&gt;render($variables);
  
          // how to get variables from $file_name tempalte
  
      } catch (Throwable $e) {
          return "NULL"
      }
  
  
      return $result;
  }
  }
  </code></pre>