Skip to content

Instantly share code, notes, and snippets.

@robdecker
Last active November 10, 2020 19:27
Show Gist options
  • Save robdecker/202b6381a00e4b9f91330ed41eff7966 to your computer and use it in GitHub Desktop.
Save robdecker/202b6381a00e4b9f91330ed41eff7966 to your computer and use it in GitHub Desktop.
[Twig filters, functions, tags, etc] #d8 #twig

Filters

trans / t

This filter (alternatively, t) will run the variable through the Drupal t() function, which will return a translated string. This filter should be used for any interface strings manually placed in the template that will appear for users.

<a href="{{ url('<front>') }}" title="{{ 'Home'|t }}" rel="home" class="site-logo"></a>

placeholder

This filter escapes content to HTML and formats it using drupal_placeholder(), which makes it display as emphasized text.

{% trans %}Submitted on {{ date|placeholder }}{% endtrans %}

drupal_escape

Replace twig's escape filter with our own.

safe_join

{{ items|safe_join(', ') }}

without

{{ content|without('links') }}

{{ content|without('links', 'field_some_data') }}

clean_class

This filter prepares a string for use as a valid HTML class name. See Html::getClass()

{{ some_var|clean_class }}

clean_id

This filter prepares a string for use as a valid HTML ID. See Html::getID()

{{ some_var|clean_id }}

render

{{ some_var|render }}

format_date

This filter prepares a timestamp for use as a formatted date string. See DateFormatter::format()

To use a Drupal date/time format:

{{ some_var|date('U')|format_date('long') }}

raw

This filter should be avoided whenever possible, particularly if you're outputting data that could be user-entered. See this page for more information on auto-escape in Drupal 8.

{{ some_var|raw }}

merge

{{ set some_var = some_var|merge(['item 1', 'item 2']) }}
{{ set some_var = some_var|merge({id: 'the_id'}) }}

default

{{ set some_var = some_other_var|default('The value to use if some_other_var is empty') }}

Functions

render_var

url

<a href="{{ url('view.frontpage.page_1') }}">{{ 'View all content'|t }}</a>
<a href="{{ url('<current>') }}">{{ 'Reload'|t }}</a>
<a href="{{ url('<front>') }}">{{ 'Home'|t }}</a>

path

{# Link to frontpage view. #}
<a href="{{ path('view.frontpage.page_1') }}">{{ 'View all content'|t }}</a>

{# Link to user entity/profile page. #}
<a href="{{ path('entity.user.canonical', {'user': user.id}) }}">{{ 'View user profile'|t }}</a>

{# Link to node page. #}
<a href="{{ path('entity.node.canonical', {'node': node.id}) }}">{{ 'View node page'|t }}</a>

link

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

file_url

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

attach_library

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

active_theme_path

active_theme

create_attribute

{% set my_attribute = create_attribute() %}
{%
  set my_classes = [
    'kittens',
    'llamas',
    'puppies',
  ]
%}
<div{{ my_attribute.addClass(my_classes).setAttribute('id', 'myUniqueId') }}>
  {{ content }}
</div>

<div{{ create_attribute({'class': ['region', 'region--header']}) }}>
  {{ content }}
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment