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

评论 (写第一个评论)