title: Render one Twig template without cache
body: <p>Our Symfony project has a login widget, which I was told should loaded via AJAX so as not to mess with page caching. </p>
  
  <p>It displays either 'Log In', or the users email address if he is already logged in.</p>
  
  <p>The problem is, that twig template is also cached! So loading it via ajax didn't help. </p>
  
  <p>How can I tell Twig to render my template from scratch every time, without disabling cache site wide?</p>
  
  <p>The template itself if very simple:</p>
  
  <pre><code>    &lt;small id="login-username"&gt;
          {% if is_granted('ROLE_USER') %}
              {{ app.user.username }}
          {% else %}
              {{ 'LoginWidgetElement.login' | trans }}
          {% endif %}
      &lt;/small&gt;
  </code></pre>
  
  <p>The element's class is also very simple:</p>
  
  <pre><code>&lt;?php
  
  namespace Xyz\Sports\Element\LoginWidget;
  
  use Xyz\Library\AbstractElement;
  use Sensio\Bundle\FrameworkExtraBundle\Configuration as Config;
  
  /**
  * @Config\Template("@LoginWidgetElement/login_widget.html.twig")
  */
  class LoginWidgetElement extends AbstractElement
  {
      public function render()
      {
          return [];
      }
  
      public function renderPreview()
      {
          return $this-&gt;render();
      }
  }
  </code></pre>
  
  <blockquote>
    <p>UPDATE</p>
  </blockquote>
  
  <p>The twig template seems to be checking, therefore I tried sending a response telling the browser not to cache, but it still takes about 15 requests before the variable changes!!??</p>
  
  <p>Twig cache:</p>
  
  <pre><code>&lt;small id=\"login-username\"&gt;
              ";
          // line 9
          if ($this-&gt;extensions['Symfony\Bridge\Twig\Extension\SecurityExtension']-&gt;isGranted("ROLE_USER")) {
              // line 10
              echo "                ";
              echo twig_escape_filter($this-&gt;env, twig_get_attribute($this-&gt;env, $this-&gt;source, twig_get_attribute($this-&gt;env, $this-&gt;source, (isset($context["app"]) || array_key_exists("app", $context) ? $context["app"] : (function () { throw new Twig_Error_Runtime('Variable "app" does not exist.', 10, $this-&gt;source); })()), "user", array()), "username", array()), "html", null, true);
              echo "
              ";
          } else {
              // line 12
              echo "                ";
              echo twig_escape_filter($this-&gt;env, $this-&gt;extensions['Symfony\Bridge\Twig\Extension\TranslationExtension']-&gt;trans("LoginWidgetElement.login"), "html", null, true);
              echo "
              ";
          }
          // line 14
          echo "        &lt;/small&gt;
  </code></pre>
  
  <p>So there is a <code>isGranted('ROLE_USER')</code> check. Now, in my render method:</p>
  
  <pre><code>$response = new Response();
  $response-&gt;headers-&gt;set('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0');
  $response-&gt;headers-&gt;set('Pragma', 'no-cache');
  
  $body = $this-&gt;twig-&gt;render('@LoginWidgetElement/login_widget.html.twig');
  $response-&gt;setContent($body);
  
  return $response;
  </code></pre>
  
  <p>Still no cigar however :'-(</p>