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

评论 (写第一个评论)