xenophon xenophon - 3 months ago 18
Python Question

Tornado templates setting value field

I have a python array:

a=["a","b","c","d","e","f"]


I pass this as a value to tornado render:

self.render("index.html", a=a)


In my html I have the following:

<input type="hidden" id="xxx" value="
{% for c in a %}
{{c}}
{% end %}
"/>


Which gives me the following messy output:

<input type="hidden" id="xxx" value="

a

b

c

d

e

f

"/>


I tried to use a {{c.strip()}} but that did the same thing. I also trying to mix in javascript however the javascript didn't execute after rendering to populate the value field. I want the html to be normal that is
value="a b c d e f"


Does anyone know how I can achieve this?

Answer

Just join the list in the backend itself.

 self.render("index.html", a=' '.join(a))

And use,

<input type="hidden" id="xxx" value="{{ a }}" />

in template

Comments