-
-
Save fellypeavelino/c7233127422112a8170f10d967ff37fe 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 %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment