Search code examples
pythonhtmltornado

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?


Solution

  • Try Tornado's filter_whitespace feature, new in version 4.3.

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

    There will still be a few extra spaces but it's much closer to what you want.