dennohpeter
dennohpeter

Reputation: 451

Nested Double curly braces in jinja2 templates

How do I make the result of one expression evaluate to another in jinja2 templates? Something like:

{% for category in categories %}
  <p>{{ category.{{ category.pk }} }}</p>
{% endfor %}

where category is a class object to evaluate to

<p>{{ category.news }}</p>

and then that to evaluate to something else like

<p>[list, of, items]</p>

Upvotes: 0

Views: 1518

Answers (1)

dennohpeter
dennohpeter

Reputation: 451

I solved it by using [] in place of . as shown below

{% for category in categories %}
  <p>{{ category[category.pk] }}</p>
{% endfor %}

Hint: given by Abhishek Kulkarni

this is because you can use . or [] to get a class attribute as pointed out in the Jinja docs

Upvotes: 1

Related Questions