With an xml jinja2 template, how to explode an element based on text field into multiple elements?

Viewed 16

I have a jinja2 xml template

<final_tag>{{some_tag}}</final_tag>

I have a context with a string containing ",".

 {"some_tag" : "a,b,c"}

How can I use jinja2 to explode the string into multiple elements?

 <final_tag>"a"</final_tag>
 <final_tag>"b"</final_tag>
 <final_tag>"c"</final_tag>

Is there some smart feature or do I have to start thinking about not using jinja2 for this?

1 Answers

You can just split the string and iterate over the result:

{% for value in some_tag.split(',') %}
<final_tag>"{{ value }}"</final_tag>
{% endfor %}
Related