pythonjinja2flask-wtforms

How to use a jinja variable inside of a quoted Html attribute?


I want to iterate through events and entries replacing the indexes in the id="" and name='' attributes in the html form fields inside of the jinja template.

Something like this:

{% for e in events %} 
    <td><input id="events-{{ index1 }}-entries-{{ index2 }}" 
        name="events-{{ index1 }}-entries" type="checkbox" 
        value="1"></td>
{% endfor %}

I can't figure out how to escape the quotes of the attributes so that the variable is inserted in the quoted text.

I've tried to escape the quotes, but unsuccessfully.


Solution

  • I believe you could try to use string concatenation and put the "events-" and "-entries" inside the curlybraces. For example:

    {{ "Hello " ~ name ~ "!" }} when name is assigned to the string 'John' would return "Hello John!"

    You can read more about Jinja string concatenation in the official documentation.