Repopulate a static select input I have a form that contains a static select input of U.S. States:

&lt;select id="state" name="state" class="form-control"&gt;
&lt;option value=""&gt; Select &lt;/option&gt;
&lt;option value="AL"&gt;Alabama&lt;/option&gt;
&lt;option value="AK"&gt;Alaska&lt;/option&gt;
&lt;option value="AZ"&gt;Arizona&lt;/option&gt;
....
&lt;/select&gt;


I have the validation figured out on my form, and also have my text inputs getting repopulated if there are any validation errors. I am curious about the best method to repopulate my select input.

I can access the value that was entered via:

{% set state = craft.request.getParam('state') %}


Which will give me "AL" or "AK" (whichever was chosen). This is fine, but because my list is static, do I now need to have logic on every option?

For example, something like this...

&lt;option value="AL" {% if state == AL %}selected="selected" {%endif%}&gt;Alabama&lt;/option&gt;


Or, is there a better way all together?

FWIW, here is how I am repopulating my text inputs:

value="{% if model is defined %}{{ model.name }}{% endif %}


Thank you!
No, I don't think there's a better way, you need to add that conditional to each select option.
But what you could do is to refactor a bit and move that logic to a Twig macro.

{% macro stateOption(id, name, selectedId) %}
    &lt;option value="{{ id }}"{{ id == selectedId ? ' selected="selected"' }}&gt;{{ name }}&lt;/option&gt;
{% endmacro %}
{% import _self as forms %}

{% set selectedId = craft.request.getParam('state') %}

{% set states = {
    'AL': 'Alabama',
    'AK': 'Alaska',
    'AZ': 'Arizona',
} %}

&lt;select id="state" name="state"&gt;
    &lt;option value=""&gt;Select&lt;/option&gt;

    {# Go through states and use the custom macro to generate the select options #}
    {% for id, name in states %}
        {{ forms.stateOption(id, name, selectedId) }}
    {% endfor %}
&lt;/select&gt;