title: jinja2: get first item in list whether list exists or not
body: <p>For the first line below, the jinja2 template renders whether <code>x</code> exists or not. However, in the second line, the template only renders if <code>x</code> is an existing list. If it isn't I get an error (assume if <code>x</code> exists it is always a list with at least one item):</p>
  
  <pre><code>&lt;p&gt;x: {{ x }}&lt;/p&gt;
  
  &lt;p&gt;x[0]: {{ x[0] }}&lt;/p&gt;
  UndefinedError: 'x' is undefined
  </code></pre>
  
  <p>Is there a better way to check if a variable exists and get its first item than what I have here (I have tons of instances of <code>x</code> which have much longer names in my template and I'd rather not wrap it with the if clause every time):</p>
  
  <pre><code>{% if x %}
    &lt;p&gt;x[0]: {{ x[0] }}&lt;/p&gt;
  {% endif %
  </code></pre>