attribute

The attribute function is used to get an attribute of a variable.

{{ attribute(object, method) }}
{{ attribute(object, method, arguments) }}
{{ attribute(array, item) }}

It can be used instead of the . notation.

{{ attribute(posts, 'postTitle') }}

instead of

{{ posts.postTitle }}

It is useful when the attribute key contains punctuation like - or _ as those would not work with the . notation.

{{ attribute(array, 'array-key') }}

Use with logic

You can use the attribute function with an if statement.

{% if attribute(object, key) %}
    ....do something
{% endif %}

You can also use the defined test to check for the existence of the attribute.

{{ attribute(object, key) is defined ? 'Value exists: ' ~ attribute(object, key) : 'Value does not exist' }}

< Back to the list of functions