[Twig] How to display array & call method (attribute)

This is an introduction to how to use attribute, which is a Twig function.
You can view the contents of the array and call methods.

Official:https://twig.symfony.com/doc/3.x/functions/attribute.html

Calling a method using attribute

The basic usage is as follows.

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

How to use attribute: Sample

// make sample function
{% macro foo(name, age) %}
      NAME:{{ name }}
      AGE:{{ age }}
{% endmacro %}

// attribute
{{ attribute(_self, 'foo', ['Mishima', 33]) }}

// result
NAME:Mishima AGE:33

As an aside, since the function is made with macro this time, it can also be called below.

{{ _self.foo('Mishima', 33) }}

The above is an example of how to use macro.

Display the contents of the array with attribute

If the key of the array is special, there is a way to get it using attribute.
(For example, “-” is treated as a minus operator.)

{{ attribute(foo, 'data-foo') }}

// cant use
{{ foo.data-foo }}