1

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 %}
    <span class="{{ key }}">{{ value }}</span>
{% endfor %}
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 %}
    <span class="{{ key }}">{{ value }}</span>
{% endfor %}
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.