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?
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It only takes a minute to sign up.
Sign up to join this communityWhen 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?
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 }}
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.