<p>There's an example of this in the Twig documentation: <a href="https://twig.symfony.com/doc/3.x/tags/for.html#iterating-over-keys-and-values" rel="nofollow noreferrer">https://twig.symfony.com/doc/3.x/tags/for.html#iterating-over-keys-and-values</a></p>

<p>In your example, it would be:</p>

<pre><code>{% for key, value in hash %}
    &lt;span class="{{ key }}"&gt;{{ value }}&lt;/span&gt;
{% endfor %}
</code></pre>

CC BY-SA 4.0
2020-01-14T21:55:51.460Z
Thanks Brad :thumbsup:
Jonathan Schofield
CC BY-SA 4.0
2020-01-14T19:36:43.280Z
2020-01-14T19:36:43.280Z
Brad Bell
<p>This is possibly more of a Twig thing, but if I have a hash…</p>

<pre><code>{% set hash = {
    "key": "value"
} %}
</code></pre>

<p>How can I output the <strong>key name</strong>?</p>

<pre><code>{% for value in hash %}
    &lt;span class="{{ key }}"&gt;{{ value }}&lt;/span&gt;
{% endfor %}
</code></pre>

<hash>
Get a hash’s key name
# Get a hash’s key name

(Asked by: username_0 on 2020-01-14)

This is possibly more of a Twig thing, but if I have a hash…
{% set hash = {
    "key": "value"
} %}

How can I output the key name?
{% for value in hash %}
    {{ value }}
{% endfor %}

**Tags:** hash


---

## 1 answers

### Answer by username_1 on 2020-01-14. Score: 1

There's an example of this in the Twig documentation: https://twig.symfony.com/doc/3.x/tags/for.html#iterating-over-keys-and-values
In your example, it would be:
{% for key, value in hash %}
    {{ value }}
{% endfor %}

> **Comments:**
> **username_0**: Thanks Brad :thumbsup:  

---

