122

I have some variables in a jinja2 template which are strings seperated by a ';'.

I need to use these strings separately in the code. i.e. the variable is variable1 = "green;blue"

{% list1 = {{ variable1 }}.split(';') %}
The grass is {{ list1[0] }} and the boat is {{ list1[1] }}

I can split them up before rendering the template but since it are sometimes up to 10 strings inside the string this gets messy.

I had a jsp before where I did:

<% String[] list1 = val.get("variable1").split(";");%>    
The grass is <%= list1[0] %> and the boat is <%= list1[1] %>

EDIT:

It works with:

{% set list1 = variable1.split(';') %}
The grass is {{ list1[0] }} and the boat is {{ list1[1] }}
user3605780
  • 6,542
  • 13
  • 42
  • 67
  • CAn you split the string before sending it to the template? – kylieCatt May 28 '15 at 19:27
  • @IanAuld yes I can but like I said it gets messy because it are a lot of strings and they all contain a lot of strings inside them. – user3605780 May 28 '15 at 19:28
  • 1
    You could write your own filter to do a split on whatever character you like. See http://stackoverflow.com/questions/20678004/jinja2-split-string-by-white-spaces – junnytony May 28 '15 at 19:33

3 Answers3

196

After coming back to my own question after 5 year and seeing so many people found this useful, a little update.

A string variable can be split into a list by using the split function (it can contain similar values, set is for the assignment) . I haven't found this function in the official documentation but it works similar to normal Python. The items can be called via an index, used in a loop or like Dave suggested if you know the values, it can set variables like a tuple.

{% set list1 = variable1.split(';') %}
The grass is {{ list1[0] }} and the boat is {{ list1[1] }}

or

{% set list1 = variable1.split(';') %}
{% for item in list1 %}
    <p>{{ item }}<p/>
{% endfor %} 

or

{% set item1, item2 = variable1.split(';') %}
The grass is {{ item1 }} and the boat is {{ item2 }}
HackDolphin
  • 166
  • 11
user3605780
  • 6,542
  • 13
  • 42
  • 67
  • 14
    Jinja2 will also assign expanded-tuple style ala `{% set list1,list2 = variable1.split(';') %}`. – Dave Apr 11 '16 at 17:54
  • Is this a list or a set? Because in set, it will follow the properties of the set and not the list. – as2d3 Jun 08 '17 at 09:18
  • 1
    @AbhishekAgrawal the value which is passed into the template is a semicolon seperated string. – user3605780 Jun 08 '17 at 09:29
  • Yes, but we created list1, then whether it is a list or a set? – as2d3 Jun 08 '17 at 09:33
  • 1
    @AbhishekAgrawal I think the split function creates list. the 'set' is from settting a variable not dataset. But I'm not sure if it is list or set, but you can just get to your data with {{ list1[0] }}. – user3605780 Jun 08 '17 at 09:46
  • " I haven't found this function in the official documentation" such a shame. I do think that Jinja has the worst documentation even ! Thanks for the tip. – Orabîg May 29 '20 at 08:44
  • Thanks much for this very useful find. – objectNotFound Mar 19 '21 at 01:44
  • 1
    The `split` function is the one present in string objects (all string methods are available from jinja2 templates?): https://www.geeksforgeeks.org/python-string-split/ – emi Oct 06 '21 at 16:45
  • 3
    @emi it works because of https://jinja.palletsprojects.com/en/3.0.x/templates/#python-methods : `Python Methods You can also use any of the methods defined on a variable’s type. The value returned from the method invocation is used as the value of the expression. Here is an example that uses methods defined on strings (where page.title is a string): {{ page.title.capitalize() }}` – Talkerbox Nov 04 '21 at 05:47
22

If there are up to 10 strings then you should use a list in order to iterate through all values.

{% set list1 = variable1.split(';') %}
{% for list in list1 %}
<p>{{ list }}</p>
{% endfor %}
Waqar Detho
  • 1,502
  • 18
  • 17
13

You can’t run arbitrary Python code in jinja; it doesn’t work like JSP in that regard (it just looks similar). All the things in jinja are custom syntax.

For your purpose, it would make most sense to define a custom filter, so you could for example do the following:

The grass is {{ variable1 | splitpart(0, ',') }} and the boat is {{  splitpart(1, ',') }}
Or just:
The grass is {{ variable1 | splitpart(0) }} and the boat is {{  splitpart(1) }}

The filter function could then look like this:

def splitpart (value, index, char = ','):
    return value.split(char)[index]

An alternative, which might make even more sense, would be to split it in the controller and pass the splitted list to the view.

HackDolphin
  • 166
  • 11
poke
  • 369,085
  • 72
  • 557
  • 602