0

So I can do this:

{% for x in data %}
    {{ x.label }}: {{ x.value }}<br />
{% endfor %}

But I want to do this kind of thing to get one specific value:

{{ data['label' }}

I can't see how to do it, but it must be possible.

1 Answer 1

1

http://twig.sensiolabs.org/doc/functions/attribute.html

{{ attribute(data,'label') }}

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, but I just can't get this to work. I think it's because 'data' is a api.symfony.com/2.0/Symfony/Component/Validator/… - it's the same as this: stackoverflow.com/questions/17514559/… but using Twig (so I'm passing $errors through to Twig)
your question was how to access an array value using a string as key in twig ... i kindly provided a correct answer to your question. Please upvote/accept -> open a new question like "how to ouput validator errors passed to twig" and i will kindly help you over there aswell. ... validator errors are returned as an array of objects by the way, not a pure array ... which easily explains why you don't succeed this way.
No problem, I saw this as the same question, but I have no problem opening new ones
I've posted another question. Thanks again for you help. stackoverflow.com/questions/17517521/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.