Answers.Body: <p>You can do something like this (with lots of refactoring possible):</p>
  
  <pre><code>{% set thisYear = now | date("Y") %}
  {% set startYear = thisYear - 1 %}
  {% set endYear = startYear - 4 %}
  
  {% set yearRange = startYear..endYear %}
  
  &lt;select name=""&gt;
  {% for year in yearRange %}
      &lt;option value="{{ year }}"&gt;{{ year }}&lt;/option&gt;
  {% endfor %}
  &lt;/select&gt;
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-12T21:16:56.107Z
Answers.Text: It's also simple enough to build this into a macro where you pass in the start year and the number of years back you want it to go. Or the start year and end year You can then build the range and iterate over it.
Answers.UserDisplayName: CraftQuest
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-12T21:35:45.770Z
Answers.Text: You can also do the range right in the for-loop, too. {% for year in startYear..endYear %}
Answers.UserDisplayName: CraftQuest
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-12T22:25:12.123Z
Answers.Text: Thanks, Ryan! {% set yearRange = startYear..endYear %} is what I trying to figure out!! Much appreciated!
Answers.UserDisplayName: Roi Agneta
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-14T02:22:21.600Z
Answers.Text: A quick video on how to do it for those who like to watch and follow: https://craftquest.io/lessons/creating-a-dynamic-select-element
Answers.UserDisplayName: CraftQuest
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-12T21:16:13.593Z
Answers.LastActivityDate: 2020-01-12T21:16:13.593Z
Answers.UserDisplayName: CraftQuest
Body: <p>I want to build a dynamic html select element that includes the prior five years, given the current year. So, since it is now 2020, the result would be:</p>
  
  <pre><code>&lt;select&gt;
  &lt;option value="2015"&gt;2015&lt;/option&gt;
  .
  .
  &lt;option value="2019"&gt;2019&lt;/option&gt;
  </code></pre>
  
  <p></p>
  
  <p>Basically want a "loop while" function, but Twig does not do that. I know I did something like this once before with a macro, but can't for the life of me remember and can't find anything on Stack Exchange. </p>
  
  <p>I know I could do it with Javascript, but would prefer to use Twig. Have the feeling I am missing something obvious, just not obvious to me!</p>
  
Tags: <templating><forloop>
Title: Create a dynamic select element
ThreadText: # Create a dynamic select element
  
  (Asked by: username_0 on 2020-01-12)
  
  I want to build a dynamic html select element that includes the prior five years, given the current year. So, since it is now 2020, the result would be:
  
  ```
  <select>
  <option value="2015">2015</option>
  .
  .
  <option value="2019">2019</option>
  
  ```
  
  Basically want a "loop while" function, but Twig does not do that. I know I did something like this once before with a macro, but can't for the life of me remember and can't find anything on Stack Exchange.
  
  I know I could do it with Javascript, but would prefer to use Twig. Have the feeling I am missing something obvious, just not obvious to me!
  
  **Tags:** templating, forloop
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-01-12. Score: 2
  
  You can do something like this (with lots of refactoring possible):
  
  ```
  {% set thisYear = now | date("Y") %}
  {% set startYear = thisYear - 1 %}
  {% set endYear = startYear - 4 %}
  
  {% set yearRange = startYear..endYear %}
  
  <select name="">
  {% for year in yearRange %}
      <option value="{{ year }}">{{ year }}</option>
  {% endfor %}
  </select>
  
  ```
  
  > **Comments:**
  > **username_1**: It's also simple enough to build this into a macro where you pass in the start year and the number of years back you want it to go. Or the start year and end year You can then build the range and iterate over it.  
  > **username_1**: You can also do the range right in the for-loop, too. {% for year in startYear..endYear %}  
  > **username_0**: Thanks, Ryan! {% set yearRange = startYear..endYear %} is what I trying to figure out!! Much appreciated!  
  > **username_1**: A quick video on how to do it for those who like to watch and follow: https://username_1.io/lessons/creating-a-dynamic-select-element  
  
  ---
  
  