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 #} 齵

评论 (写第一个评论)