移至主內容
首頁  >  Drupal目錄  >  在 Twig 模板中的函數

在 Twig 模板中的函數

Tag :
twig, functions
Written by Shiuan on 13 December 2023

Twig 提供了許多方便的函數,可以直接在模板中使用。

List of Twig Functions

Drupal 核心添加了一些特定的 Drupal 自定義函數。這些函數在 TwigExtension class 中定義。

您還可以在自定義的模組中定義自己的 Twig 函數(但不能在主題中)。要找到如何做到這一點的範例,請參見以下連結當中的範例。 
core/modules/system/tests/modules/twig_extension_test/src/TwigExtension/TestExtension.php

attach_library($library)

將一個資產庫(asset library)附加到模板。

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

active_theme()

顯示當前啟用主題的機器名稱。

{{ active_theme() }} {# my_custom_theme #}

active_theme_path()

顯示目前使用主題的相對路徑。

{{ active_theme_path() }} {# themes/custom/my_custom_theme #}

create_attribute($attributes)

在 Twig 模板中使用 create_attribute() 函數創建新的屬性對象。然後可以像處理其他傳入 Twig 模板的屬性對象一樣對這些對象進行操作。

{% 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>

render_var($arg)

包裝 render() 的便利函數。

{{ render_var({ '#theme': 'foo_bar', '#title': 'baz' }) }}

file_url($uri)

此輔助函數接受指向文件的 URI,並創建文件的相對 URL 路徑。

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

link($text, $uri, $attributes)

此輔助函數的第一個參數是文本,第二個參數是 URI。第三個選填參數是屬性對象,可用於提供像是額外的 CSS classes。

請注意,此函數處理 Drupal 選單項目文本的 <nolink><button> 選項,當它們被使用時,生成 <span><button> 元素而不是 <a>。如果 URI 參數是具有 set_active_class 選項設置為 TRUE 的 \Drupal\Core\Url 對象,則它還會自動添加 data-drupal-link-system-path 屬性和 is-active CSS class(如果連接處於活動狀態)

範例:

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

使用陣列為 $attributes 的舊式簡寫:

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

(仍然受支持,但不建議於 Drupal > 8.3 使用,詳情請參見https://www.drupal.org/node/2818293

path($name, $parameters, $options)

根據路由名稱和參數生成「相對」URL 路徑。

{# 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 to taxonomy term. #}
<a href="{{ path('entity.taxonomy_term.canonical', {'taxonomy_term': term.id}) }}">{{ 'View taxonomy term'|t }}</a>

{# Add a destination query parameter. Simple Example #}
<a href="{{ path('user.login', {}, {'query': {'destination': path('<current>') }}) }}">{{ 'View node page'|t }}</a>.

{# Add a destination query parameter. Advanced Example, event_user_sync.registration is a custom route from a custom module with the name event_user_sync #}
{% set redirect_path = path('event_user_sync.registration', {'node': node.id}) %}
<a href="{{ path('user.register',{},{'query':{'destination': redirect_path }}) }}">{{ 'Register'|t }}</a>

URL 和路徑函數的定義與 \Symfony\Bridge\Twig\Extension\RoutingExtension 中的定義非常相似。

url($name, $parameters, $options)

生成給定路由名稱和參數的絕對 URL:

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

生成導向當前 URL 的絕對 URL:

<a href="{{ url('<current>') }}">{{ 'Reload'|t }}</a>

生成導向首頁的絕對 URL:

<a href="{{ url('<front>') }}">{{ 'Home'|t }}</a>

生成導向特定節點的絕對 URL:

<a href="{{ url('entity.node.canonical', { 'node': 123 }) }}">{{ 'Visit node 123'|t }}</a>

生成導向特定分類詞的絕對 URL:

<a href="{{ url('entity.taxonomy_term.canonical', { 'taxonomy_term': 123 }) }}">{{ 'Visit taxonomy term 123'|t }}</a>

如果在節點模板中工作並且有一個可用的節點變數,則可以生成導向當前節點的絕對 URL:

<a href="{{ url('entity.node.canonical', { 'node': node.id() }) }}">{{ 'Visit this page'|t }}</a>

返回Drupal
首頁  >  Drupal  >  在 Twig 模板中的函數

在 Twig 模板中的函數

Tag :
twig, functions
Written by Shiuan on 13 December 2023

Twig 提供了許多方便的函數,可以直接在模板中使用。

List of Twig Functions

Drupal 核心添加了一些特定的 Drupal 自定義函數。這些函數在 TwigExtension class 中定義。

您還可以在自定義的模組中定義自己的 Twig 函數(但不能在主題中)。要找到如何做到這一點的範例,請參見以下連結當中的範例。 
core/modules/system/tests/modules/twig_extension_test/src/TwigExtension/TestExtension.php

attach_library($library)

將一個資產庫(asset library)附加到模板。

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

active_theme()

顯示當前啟用主題的機器名稱。

{{ active_theme() }} {# my_custom_theme #}

active_theme_path()

顯示目前使用主題的相對路徑。

{{ active_theme_path() }} {# themes/custom/my_custom_theme #}

create_attribute($attributes)

在 Twig 模板中使用 create_attribute() 函數創建新的屬性對象。然後可以像處理其他傳入 Twig 模板的屬性對象一樣對這些對象進行操作。

{% 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>

render_var($arg)

包裝 render() 的便利函數。

{{ render_var({ '#theme': 'foo_bar', '#title': 'baz' }) }}

file_url($uri)

此輔助函數接受指向文件的 URI,並創建文件的相對 URL 路徑。

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

link($text, $uri, $attributes)

此輔助函數的第一個參數是文本,第二個參數是 URI。第三個選填參數是屬性對象,可用於提供像是額外的 CSS classes。

請注意,此函數處理 Drupal 選單項目文本的 <nolink><button> 選項,當它們被使用時,生成 <span><button> 元素而不是 <a>。如果 URI 參數是具有 set_active_class 選項設置為 TRUE 的 \Drupal\Core\Url 對象,則它還會自動添加 data-drupal-link-system-path 屬性和 is-active CSS class(如果連接處於活動狀態)

範例:

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

使用陣列為 $attributes 的舊式簡寫:

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

(仍然受支持,但不建議於 Drupal > 8.3 使用,詳情請參見https://www.drupal.org/node/2818293

path($name, $parameters, $options)

根據路由名稱和參數生成「相對」URL 路徑。

{# 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 to taxonomy term. #}
<a href="{{ path('entity.taxonomy_term.canonical', {'taxonomy_term': term.id}) }}">{{ 'View taxonomy term'|t }}</a>

{# Add a destination query parameter. Simple Example #}
<a href="{{ path('user.login', {}, {'query': {'destination': path('<current>') }}) }}">{{ 'View node page'|t }}</a>.

{# Add a destination query parameter. Advanced Example, event_user_sync.registration is a custom route from a custom module with the name event_user_sync #}
{% set redirect_path = path('event_user_sync.registration', {'node': node.id}) %}
<a href="{{ path('user.register',{},{'query':{'destination': redirect_path }}) }}">{{ 'Register'|t }}</a>

URL 和路徑函數的定義與 \Symfony\Bridge\Twig\Extension\RoutingExtension 中的定義非常相似。

url($name, $parameters, $options)

生成給定路由名稱和參數的絕對 URL:

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

生成導向當前 URL 的絕對 URL:

<a href="{{ url('<current>') }}">{{ 'Reload'|t }}</a>

生成導向首頁的絕對 URL:

<a href="{{ url('<front>') }}">{{ 'Home'|t }}</a>

生成導向特定節點的絕對 URL:

<a href="{{ url('entity.node.canonical', { 'node': 123 }) }}">{{ 'Visit node 123'|t }}</a>

生成導向特定分類詞的絕對 URL:

<a href="{{ url('entity.taxonomy_term.canonical', { 'taxonomy_term': 123 }) }}">{{ 'Visit taxonomy term 123'|t }}</a>

如果在節點模板中工作並且有一個可用的節點變數,則可以生成導向當前節點的絕對 URL:

<a href="{{ url('entity.node.canonical', { 'node': node.id() }) }}">{{ 'Visit this page'|t }}</a>