9.2 Twig

增加一个 Twig 模板

龵龱龯龭龩龥龢龠龞龚龖龔龐龏龋龉龅龁齾齻齺齸齶 Twig 齩齧龢齥Twig 齢 Drupal 8 龚齏齍龖龔齉齈齥

齏齍 Twig 齩齧齆齃龚齀鼾鼺鼸鼵 Drupal 7 鼯龚 .tpl.php 齩齧齆齃鼦鼥鼢 鼡鼠 – 鼙鼕 _龋齾 .html.twig 鼋鼊鼈鼾齥

龵龱龚 Twig 齩齧

鼇鼅龋鼄鼁齩齧齆齃 templates/my-element.html.twig黮黪黩黨黦黣黡黟龋黛龁齾鼡鼠 {{element}} 黔黐黏齶黌黈龐龏黇黃鼡鼠鼙黂 {{element.field}} 黔黐齶麹麶麵黇麴麲龐龏齥

Twig 鼯龚麱麯

齩齧齆麮龁齾麬麫 Twig’s documentation states

Drupal 龅龖龔麢麡齩齧齆齃麞麚龁麖麓龚麱麯龋麱麯龞麏麎龁齾麊麉麅麁鹾龚鹻鹹黇龐龏齥

{#
/**
 * @file
 * Default theme implementation to my_element.
 *
 * Available variables:
 * - element: Element that will be rendered.
 * - element['link'] : a link
 * - element['description'] : a description
 * - element['pre_render_addition'] : added during the #pre_render function
 * - element['random_number'] : a random number variable, won't be printed with the element.
 *
 * @ingroup themeable
 */
#}

<div>
  <div>Random Number: {{ element['#random_number'] }}</div>
  <p>{{ element.description }}</p>
  {{ element.link }}
</div>

{# Debug output #}
<div>
  <h3>We print the element</h3>
  {{ element }}
  <h3>Link</h3>
  {{ element.link }}
  <h3>Description</h3>
  {{ element.description }}
  <h3>#pre_render addition</h3>
  {{ element.pre_render_addition }}
  <h3>Random number (not printed when we printed the whole element</h3>
  {{ element['#random_number'] }}
</div>

调试 Twig

麊鹶鹵鹱鹭龁齾鹩鹨 Twig鼢

  • 鹥鹡鹠Twig debugging鹙龋鹘鹕鹔黨黛鹓鹐鹌鹋鹇龅鼡鼠鹃齶齩齧齥
  • 龅鸿鸻鼯鸷鸶鸴鸱龋鹘鸭鸩龵龱鸧鸦齩齧鼯鹃鸣麱麯龁鼠齥

鹓鹐齩齧齺鸢

鹥鹡 Twig debugging 鸟鸜龅鸿鸻鸛黂鼯鸙鸗齸鸣鸴鸱龋鸔鸑黛龉龅鹇龅鼡鼠鸍鸉齩齧齥Drupal.org 鸈麊鹩鹨 Twig 鹱鸆龚鸃鸂鷾鷽 Debugging Twig templates

鸃鷻鷸鷶龋鷴鼁鸻鹘鷲龅 sites/default/services.yml 黦鹥鹡 Twig 鹩鹨鼢

parameters:
  twig.config:
    debug: true

鷶鸟黛鼡鼠 鷪鷧鷦鷥 鷡鼄齩齧鷟鷞齥

鹩鹨 Twig 麱麯

Twig 鹩鹨鷛 Drupal 7 鼯龚鹱鸆黣鷚齥Twig 麎龁齾龅齩齧鼯鷡鼄鷙麊黇齶麹麱麯齢鷖麊鷕齥

鸂鷓鹘齶龵龱鷏鸂鷍鷌 Devel Kint 齩鷁齥鷶鸟龵龱龁齾鼡鼠鼁鸻齀鶾鶼鷙麊麱麯鸙鸗鶹鶷鶴鸈鼢

{{ kint() }}

黇黃鸙鸗齶麹麱麯鼢

{# Or one variable. #}
{{ kint(element) }}

{# Or one part of the render array. #}
{{ kint(element.hello) }}

鶢鶡鸈鸻龚鶟麲龋黛麎鶛鶚 Twig 龚鶘鶕鶒鹭 {# comment #} 齥

评论 (写第一个评论)