<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>

CC BY-SA 4.0
2020-01-12T21:16:56.107Z
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.
CraftQuest
CC BY-SA 4.0
2020-01-12T21:35:45.770Z
You can also do the range right in the for-loop, too. {% for year in startYear..endYear %}
CraftQuest
CC BY-SA 4.0
2020-01-12T22:25:12.123Z
Thanks, Ryan! {% set yearRange = startYear..endYear %} is what I trying to figure out!! Much appreciated!
Roi Agneta
CC BY-SA 4.0
2020-01-14T02:22:21.600Z
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
CraftQuest
CC BY-SA 4.0
2020-01-12T21:16:13.593Z
2020-01-12T21:16:13.593Z
CraftQuest
<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>

<templating><forloop>
Create a dynamic select element
# 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:

2015
.
.
2019


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 %}


{% for year in yearRange %}
    {{ year }}
{% endfor %}

> **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://craftquest.io/lessons/creating-a-dynamic-select-element  

---

