jordankaiser
5/1/2019 - 2:50 PM

Macros

Macros in Twig

Quick example.

<nav>
  {{ _self.foo('hi') }}
  {# Expected output: <p>hi</p> #}
</nav>

{% macro foo(greeting) %}
  {% import _self as bar %}
  <p>{{ greeting }}</p>
{% endmacro %}

When defining and using the macro in the same file use _self. When we say _self, it's a way of referring to this very template.