简体   繁体   中英

How to prefill select form field with twig

How can I use Twig to prefill a select option in a form?

<select>
    <option value="6" {{person[0].age == '6' ? selected : ''}}>6</option>
</select>

I've tried the above to no avail.

I ended up doing something like this:

{% if person[0].age == '6' %}
     <option value="6" selected>6</option>
{% else %}
     <option value="6">6</option>
{% endif %}

It's just a lot more code than being able to do it like the example in my question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM