Thursday, June 07, 2012

PHPTAL Select input "selected"

<select name="category_id">
<tal:block tal:condition="category" tal:repeat="c categories" tal:define="selectedValue category">
<option value="${c/category_id}"
tal:define="optionValue c/category_id"
tal:attributes="selected php:optionValue == selectedValue" >
${c/description} (${c/category_id})
</option>
</tal:block>

<tal:block tal:condition="not: category" tal:repeat="c categories" >
<option value="${c/category_id}"> ${c/description} (${c/category_id})</option>
</tal:block>
</select>

It seems awkward that I have to define the selected value, in order to evaluate it for tal:attributes, so if someone knows of a better way, please let me know.

No comments: