Extra Block Types (EBT) - New Layout Builder experience❗

Extra Block Types (EBT) - styled, customizable block types: Slideshows, Tabs, Cards, Accordions and many others. Built-in settings for background, DOM Box, javascript plugins. Experience the future of layout building today.

Demo EBT modules Download EBT modules

❗Extra Paragraph Types (EPT) - New Paragraphs experience

Extra Paragraph Types (EPT) - analogical paragraph based set of modules.

Demo EPT modules Download EPT modules

Scroll
13/04/2025, by Ivan

Menu

Twig provides a number of convenient functions that can be used directly in templates.

List of Twig Functions

Drupal core adds several custom functions specific to Drupal. They are defined in the TwigExtension class.

You can also define your own Twig functions in a custom module (but not in a theme). For an example of how to do this, see the example file TestExtension.php.

attach_library($library)

Attaches an asset library to the template.

{{ attach_library('classy/node') }}

create_attribute($attributes)

Create new Attribute objects using the create_attribute() function inside a Twig template. These objects can be manipulated just like other Attribute objects passed to the Twig template.

See the change record: https://www.drupal.org/node/2818293

{% set my_attribute = create_attribute() %}
{%
  set my_classes = [
    'kittens',
    'llamas',
    'puppies',
  ]
%}
{{ content }}
{{ content }}

file_url($uri)

This helper function takes a file URI and generates a relative URL path to the file.

{{ file_url(node.field_example_image.entity.uri.value) }}

link($text, $uri, $attributes)

This helper function takes text as the first parameter and uri as the second.

Examples:

{{ link(item.title, item.uri, { 'class':['foo', 'bar', 'baz']} ) }}

path($name, $parameters, $options)

Generates a [relative] URL path with route name and parameters.

{# Link to frontpage view. #}
{{ 'View all content'|t }}

{# Link to user entity/profile page. #}
{{ 'View user profile'|t }}

{# Link to node page. #}
{{ 'View node page'|t }}

The url and path functions are defined similarly to those in \Symfony\Bridge\Twig\Extension\RoutingExtension.

url($name, $parameters, $options)

Generate an absolute URL using route name and parameters:

{{ 'View all content'|t }}

Generate an absolute URL to the current path:

{{ 'Reload'|t }}

Generate an absolute URL to the front page:

{{ 'Home'|t }}

Drupal’s online documentation is © 2000-2020 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.