Created
May 19, 2015 15:39
-
-
Save goreilly/5756bc2b5ef1ef8e33e3 to your computer and use it in GitHub Desktop.
Twig HTML Select Macro with optgroups
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% macro select (name, id, options, selected, required, includeBlank) %} | |
<select name="{{ name }}" id="{{ id }}" {{ required ? 'required' : '' }}> | |
{% if includeBlank %} | |
<option value=""></option> | |
{% endif %} | |
{% for key, value in options %} | |
{% if value is iterable %} | |
<optgroup label="{{ key }}"> | |
{% for subKey, subValue in value %} | |
<option value="{{ subKey }}" {{ subKey == selected ? 'selected' : '' }}>{{ subValue }}</option> | |
{% endfor %} | |
</optgroup> | |
{% else %} | |
<option value="{{ key }}" {{ key == selected ? 'selected' : '' }}>{{ value }}</option> | |
{% endif %} | |
{% endfor %} | |
</select> | |
{% endmacro %} |
Author
goreilly
commented
May 19, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment