6

When to use

{{ variable.property }}

and when to use

{{ variable['property'] }}

Is there a difference between these methods of accessing attributes of a variable or advantages of using one of them?

7

They're both the same for the most part.

The only time I'd use the second way you listed is when the property is a variable.

For instance:

{% set foo = 'name' %}
{{ variable[foo] }}

This will give you {{ variable.name }}

  • Yes, using property as a variable is definitely important thing! +1 – nicael Jun 14 '14 at 17:37
4

Generally speaking, no. They are just two ways of saying the same thing.

Dot syntax is necessary when you are referring to a method within an object. However, if you are only trying to access the property of an object (or a value within an array), either technique will work.

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.