Reputation: 3163
I'm experimenting Python web frameworks and HTML templates. The concept seems restricted, compared to generating entire HTML code on the fly. For instance, to generate a combo box, I found the following Django templating example:
<select id="{{ item.name }}" name="{{ item.name }}">
{% for choice in item.choices %}
{% ifequal item.value choice %}
<option value="{{ choice }}" selected>{{ choice }}</A>
{% else %}
<option value="{{ choice }}">{{ choice }}</A>
{% endifequal %}
{% endfor %}
</select>
The ifequal statement duplicates entire code just to add a "selected" attribute to the selected option. It seems to me this becomes a burden in HTML tags with several attributes and which some few attributes exist or not depending on a condition. Is the above snippet a bad usage of templating? Is there a better way to implement the combo box using it?
Upvotes: 0
Views: 146
Reputation: 5275
It can be written in a single line in this way.
<option value="{{ choice }}" {% ifequal item.value choice %}selected="selected"{% endifequal %}>{{ choice }}</option>
Upvotes: 3