Functions
In Twig, functions are special built-in commands that perform specific operations and return a result. They are useful for tasks such as generating URLs, formatting dates, including templates, and more.
Common functions
include
The include
function is used to include the contents of another template within the current template. This is useful for reusing common elements like headers, footers, or product listings.
{{ include('header.twig') }}
range
The range
function creates an array containing a sequence of numbers, which can be useful for loops or other iterative tasks.
{% for i in range(1, 5) %}
<p>Number: {{ i }}</p>
{% endfor %}
Output: 1 2 3 4 5
block
The block
function renders a block defined in the template or a parent template. This is useful for template inheritance.
{{ block('content') }}