Answers.Body: <p>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 <a href="http://twig.sensiolabs.org/doc/tags/macro.html" rel="nofollow">Twig macro</a>.</p>
  
  <pre><code>{% 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;
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-24T15:05:29.443Z
Answers.Text: Ah - this is exactly the shove I needed. (Obviously) I'm new to twig development, and was searching the docs for all the wrong things it seems. I figured I would have to do it 50x times, (yuck) but the macro is the way to roll.  Thank you very much!!
Answers.UserDisplayName: Damon
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-24T12:40:53.373Z
Answers.LastActivityDate: 2015-03-24T12:46:16.400Z
Answers.LastEditDate: 2015-03-24T12:46:16.400Z
Answers.UserDisplayName: carlcs
Body: <p>I have a form that contains a static select input of U.S. States:</p>
  
  <pre><code>&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;
  </code></pre>
  
  <p>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.</p>
  
  <p>I can access the value that was entered via:</p>
  
  <pre><code>{% set state = craft.request.getParam('state') %}
  </code></pre>
  
  <p>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?</p>
  
  <p>For example, something like this...</p>
  
  <pre><code>&lt;option value="AL" {% if state == AL %}selected="selected" {%endif%}&gt;Alabama&lt;/option&gt;
  </code></pre>
  
  <p>Or, is there a better way all together?</p>
  
  <p>FWIW, here is how I am repopulating my text inputs:</p>
  
  <pre><code>value="{% if model is defined %}{{ model.name }}{% endif %}
  </code></pre>
  
  <p>Thank you!</p>
  
Tags: <form><validation>
Title: Repopulate a static select input
ThreadText: # Repopulate a static select input
  
  (Asked by: username_0 on 2015-03-24)
  
  I have a form that contains a static select input of U.S. States:
  
  ```
  <select id="state" name="state" class="form-control">
  <option value=""> Select </option>
  <option value="AL">Alabama</option>
  <option value="AK">Alaska</option>
  <option value="AZ">Arizona</option>
  ....
  </select>
  
  ```
  
  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...
  
  ```
  <option value="AL" {% if state == AL %}selected="selected" {%endif%}>Alabama</option>
  
  ```
  
  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!
  
  **Tags:** form, validation
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-03-24. Score: 3 | Accepted answer
  
  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](http://twig.sensiolabs.org/doc/tags/macro.html).
  
  ```
  {% macro stateOption(id, name, selectedId) %}
      <option value="{{ id }}"{{ id == selectedId ? ' selected="selected"' }}>{{ name }}</option>
  {% endmacro %}
  {% import _self as forms %}
  
  {% set selectedId = craft.request.getParam('state') %}
  
  {% set states = {
      'AL': 'Alabama',
      'AK': 'Alaska',
      'AZ': 'Arizona',
  } %}
  
  <select id="state" name="state">
      <option value="">Select</option>
  
      {# Go through states and use the custom macro to generate the select options #}
      {% for id, name in states %}
          {{ forms.stateOption(id, name, selectedId) }}
      {% endfor %}
  </select>
  
  ```
  
  > **Comments:**
  > **username_0**: Ah - this is exactly the shove I needed. (Obviously) I'm new to twig development, and was searching the docs for all the wrong things it seems. I figured I would have to do it 50x times, (yuck) but the macro is the way to roll.  Thank you very much!!  
  
  ---
  
  