title: Variable inside variable in Jinja2
body: <h2>1. Summary</h2>
  
  <p>I can't set use variable inside variable in my Jinja2 example.</p>
  
  <p>Solution must be <a href="https://blog.getpelican.com/" rel="nofollow noreferrer"><strong>Pelican</strong></a>-compatible.</p>
  
  <hr>
  
  <h2>2. MCVE</h2>
  
  <h3>2.1. Expected behavior</h3>
  
  <pre><code>Any text
  
      Goddess Kira greatest of all time!
  
      Goddess Kristina greatest of all time!
  
  
  Another text
  
      Goddess Sasha greatest of all time!
  
      Goddess Katya greatest of all time!
  </code></pre>
  
  <h3>2.2. my attempt</h3>
  
  <ul>
  <li><a href="https://repl.it/@Kristinita/KiraJinja2loops-duplicatetextinsideloops" rel="nofollow noreferrer"><strong>Live demo on Repl.it</strong></a>:</li>
  </ul>
  
  <pre class="lang-python prettyprint-override"><code>"""Jinja2 nested variables example."""
  from jinja2 import Template
  
  KIRA_BLOCK = """
  {% set first_list = ['Kira', 'Kristina'] %}
  Any text
  {% for name in first_list %}
      Goddess {{name}} greatest of all time!
  {% endfor %}
  {% set second_list = ['Sasha', 'Katya'] %}
  Another text
  {% for name in second_list %}
      Goddess {{name}} greatest of all time!
  {% endfor %}
  """
  
  print(Template(KIRA_BLOCK).render())
  </code></pre>
  
  <h3>2.3. Problem</h3>
  
  <p>I have duplicate <code>Goddess</code> and <code>greatest of all time!</code> in loops. Can I don't use duplicates?</p>
  
  <hr>
  
  <h2>3. Not helped</h2>
  
  <h3>3.1. Similar questions</h3>
  
  <p><a href="https://stackoverflow.com/a/32024636/5951529"><strong>This</strong></a>, <a href="https://stackoverflow.com/a/40331888/5951529"><strong>this</strong></a>, <a href="https://stackoverflow.com/q/14254308/5951529"><strong>this</strong></a> and <a href="https://i.imgur.com/Lm25naW.png" rel="nofollow noreferrer"><strong>many others</strong></a> Stack Overflow questions.</p>
  
  <h3>3.2. Nested variables</h3>
  
  <p>For example, this code doesn't work:</p>
  
  <pre class="lang-python prettyprint-override"><code>"""Jinja2 nested variables example."""
  from jinja2 import Template
  
  KIRA_BLOCK = """
  {% set she_greatest = 'Goddess' {{name}} 'greatest of all time!' %}
  {% set first_list = ['Kira', 'Kristina'] %}
  Any text
  {% for name in first_list %}
      {{she_greatest}}
  {% endfor %}
  {% set second_list = ['Sasha', 'Katya'] %}
  Another text
  {% for name in second_list %}
      {{she_greatest}}
  {% endfor %}
  """
  
  print(Template(KIRA_BLOCK).render())
  </code></pre>
  
  <p>I get traceback:</p>
  
  <pre class="lang-python prettyprint-override"><code>Traceback (most recent call last):
    File "KiraJinja2.py", line 18, in &lt;module&gt;
      print(Template(KIRA_BLOCK).render())
    File "C:\Python37\lib\site-packages\jinja2\environment.py", line 945, in __new__
      return env.from_string(source, template_class=cls)
    File "C:\Python37\lib\site-packages\jinja2\environment.py", line 880, in from_string
      return cls.from_code(self, self.compile(source), globals, None)
    File "C:\Python37\lib\site-packages\jinja2\environment.py", line 591, in compile
      self.handle_exception(exc_info, source_hint=source_hint)
    File "C:\Python37\lib\site-packages\jinja2\environment.py", line 780, in handle_exception
      reraise(exc_type, exc_value, tb)
    File "C:\Python37\lib\site-packages\jinja2\_compat.py", line 37, in reraise
      raise value.with_traceback(tb)
    File "&lt;unknown&gt;", line 2, in template
    File "C:\Python37\lib\site-packages\jinja2\environment.py", line 497, in _parse
      return Parser(self, source, name, encode_filename(filename)).parse()
    File "C:\Python37\lib\site-packages\jinja2\parser.py", line 901, in parse
      result = nodes.Template(self.subparse(), lineno=1)
    File "C:\Python37\lib\site-packages\jinja2\parser.py", line 888, in subparse
      self.stream.expect('block_end')
    File "C:\Python37\lib\site-packages\jinja2\lexer.py", line 384, in expect
      self.name, self.filename)
  jinja2.exceptions.TemplateSyntaxError: expected token 'end of statement block', got '{'
  </code></pre>
  
  <hr>
  
  <h2>4. Real example</h2>
  
  <p>For preventing <a href="http://xyproblem.info/" rel="nofollow noreferrer"><strong>XY Problem</strong></a> I post truncated real code:</p>
  
  <h3>4.1. Expected behavior</h3>
  
  <pre class="lang-python prettyprint-override"><code>&lt;div class="KiraCategory"&gt;First&lt;/div&gt;
  
      &lt;a onclick="KiraFunction(document.all.sitename.value, 'https://Alpha.com');" class="Foo"&gt;Alpha&lt;/a&gt;
  
      &lt;a onclick="KiraFunction(document.all.sitename.value, 'https://Bravo.ru');" class="Bar"&gt;Beta&lt;/a&gt;
  
  
  &lt;div class="KiraCategory"&gt;Second&lt;/div&gt;
  
      &lt;a onclick="KiraFunction(document.all.sitename.value, 'https://Charlie.net');" class="Baz"&gt;Gamma&lt;/a&gt;
  </code></pre>
  
  <p>The example shows where static is and where variables.</p>
  
  <h3>4.2. my attempt</h3>
  
  <ul>
  <li><a href="https://repl.it/@Kristinita/KiraJinja2loops-duplicatetextinsideloopsreal" rel="nofollow noreferrer"><strong>Live demo on Repl.it</strong></a>:</li>
  </ul>
  
  <pre class="lang-python prettyprint-override"><code>"""Jinja2 nested variables example."""
  from jinja2 import Template
  
  KIRA_BLOCK = """
  {% set first_list = [
      ('https://Alpha.com', 'Foo', 'Alpha'),
      ('https://Bravo.ru', 'Bar', 'Beta')
  ] %}
  &lt;div class="KiraCategory"&gt;First&lt;/div&gt;
  {% for url, color, sitename in first_list %}
      &lt;a onclick="KiraFunction(document.all.sitename.value, '{{url}}');" class="{{color}}"&gt;{{sitename}}&lt;/a&gt;
  {% endfor %}
  {% set second_list = [
      ('https://Charlie.net', 'Baz', 'Gamma'),
  ] %}
  &lt;div class="KiraCategory"&gt;Second&lt;/div&gt;
  {% for url, color, sitename in second_list %}
      &lt;a onclick="KiraFunction(document.all.sitename.value, '{{url}}');" class="{{color}}"&gt;{{sitename}}&lt;/a&gt;
  {% endfor %}
  """
  
  print(Template(KIRA_BLOCK).render())
  </code></pre>
  
  <p>Duplicates in my attempt.</p>