Add quota around each line in a list in jinja2?

Python variable:

names = ["a", "b"] 

What I'm writing currently in the Jinja2 template:

 c({{ names | join(",") }}) 

What I use in the template above:

 c(a, b) 

However, I really need to:

 c("a", "b") 

I checked the Jinja2 document but did not find a filter for this. Anyone have any ideas on this in Jinja2?

+7
source share
2 answers

Use special filters for jinja2:

 def surround_by_quote(a_list): return ['"%s"' % an_element for an_element in a_list] env.filters["surround_by_quote"] = surround_by_quote 
+5
source
 # some.py file names = ['a', 'b', 'c'] # some.html file {{ names|safe }} # renders as the following, brackets included ['a', 'b', 'c'] 
0
source

All Articles