
PHPTemplate 和 Twig theming paradigms 的比較

關於Twig
Twig 是一種基於 PHP 的編譯模板語言。當您的網頁渲染時,Twig 引擎會取得模板並將其轉換為一個「編譯過的」PHP 模板,然後將其存儲在 sites/default/files/php/twig 這個受保護的目錄中。編譯只需進行一次,模板文件被緩存以供重複使用,並在清除 Twig 緩存時重新編譯。
Drupal 的 Twig 倡議與 Symfony 倡議有相同的動機:採用一個現代、強大、基於物件導向編程(OOP)的引擎,使開發人員能夠專注於正確地構建 Drupal。
1.Docblock
PHPTemplate:
<?php
/**
* @file
* File description
*/
?>
Twig:
{#
/**
* @file
* File description
*/
#}
2.檔案和函數名稱
PHPTemplate file:node--article.tpl.php
Twig file:node--article.html.twig
PHPTemplate function:THEME_node_links()
Twig file:node-links.html.twig
3.變數
顯示變數:
PHPTemplate:<div class="content"><?php print $content; ?></div>
Twig:<div class="content">{{ content }}</div>
顯示井字鍵
PHPTemplate:<?php print $item['#item']['alt']; ?>
Twig:{{ item['#item'].alt }}
指定變數:
PHPTemplate:<?php $custom_var = $content->comments; ?>
Twig:{% set custom_var = content.comments %}
指定陣列:
PHPTemplate:<?php $args = array('@author' => $author, '@date' => $created); ?>
Twig:{% set args = {'@author': author, '@date': created} %}
4.條件式
PHPTemplate:<?php if ($content->comments): endif; ?>
Twig:{% if content.comments %} {% endif %}
PHPTemplate:<?php if (!empty($content->comments)): endif; ?>
Twig:{% if content.comments is not empty %} {% endif %}
PHPTemplate:<?php if (isset($content->comments)): endif; ?>
Twig:{% if content.comments is defined %} {% endif %}
PHPTemplate:<?php if ($count > 0): endif; ?>
Twig:{% if count > 0 %} {% endif %}
5.控制結構
PHPTemplate:<?php foreach ($users as $user) {} ?>
Twig:{% for user in users %} {% endfor %}
6.過濾器
跳脫 HTML 特殊字元:
PHPTemplate:<?php print check_plain($title); ?>
Twig:{{ title }}
原始值:
PHPTemplate:<?php print $title; ?>
Twig:{{ title|raw }}
翻譯:
PHPTemplate:<?php print t('Home'); ?>
Twig:{{ 'Home'|t }}
帶有替換的翻譯:
PHPTemplate:<?php print t('Welcome, @username', array('@username' => $user->name)); ?>
Twig:{{ 'Welcome, @username'|t({ '@username': user.name }) }}
Drupal 8 Twig(with trans tag extension):
{% set username = user.name %}
{% trans %}
Welcome, {{ username }}
{% endtrans %}
串接列表:
PHPTemplate:<?php echo implode(', ', $usernames); ?>
Twig:{{ usernames | join(', ') }}
帶有標記的串接列表:
Twig:{{ usernames | safe_join(', ') }}
PHPTemplate 的範例需要 $usernames 是一個字串陣列。原始的 Twig 範例也是一樣,其中 "usernames" 是一個字串陣列。另一方面,Drupal 8 Twig 的範例則需要一個可渲染對象的陣列。這實際上是 Drupal 8 Twig 和原始 Twig 之間的基本區別。Drupal 8 Twig 顯示純文本和可渲染的陣列。
這個範例的另一個方面是,人們預期所有三個範例會產生相同的輸出,但實際上並不是(默認情況下)。我們看一下這個例子:
{% set numbers = [{'#markup': 'One'}, {'#markup':'Two'}, {'#markup':'Three'}] %}
{{ numbers }}
上面的例子表明項目之間以逗號分隔顯示。但輸出是:OneTwoThree
7.空白字符控制
Twig 具有空白字符控制,允許刪除用於結構化模板文件的空白字符。
<div class="body">
{{- block.content -}}
</div>
等同於
<div class="body">{{ block.content }}</div>
#Notes
在第二個例子中,我們展示了 Twig 如何負責消毒資料。以前這也是模板文件或預處理函數的職責。這對於希望為 Drupal 8 創建 PHPTemplate 主題的任何人來說是一個很大的變化 - 您將需要消毒您自己的資料。

PHPTemplate 和 Twig theming paradigms 的比較

關於Twig
Twig 是一種基於 PHP 的編譯模板語言。當您的網頁渲染時,Twig 引擎會取得模板並將其轉換為一個「編譯過的」PHP 模板,然後將其存儲在 sites/default/files/php/twig 這個受保護的目錄中。編譯只需進行一次,模板文件被緩存以供重複使用,並在清除 Twig 緩存時重新編譯。
Drupal 的 Twig 倡議與 Symfony 倡議有相同的動機:採用一個現代、強大、基於物件導向編程(OOP)的引擎,使開發人員能夠專注於正確地構建 Drupal。
1.Docblock
PHPTemplate:
<?php
/**
* @file
* File description
*/
?>
Twig:
{#
/**
* @file
* File description
*/
#}
2.檔案和函數名稱
PHPTemplate file:node--article.tpl.php
Twig file:node--article.html.twig
PHPTemplate function:THEME_node_links()
Twig file:node-links.html.twig
3.變數
顯示變數:
PHPTemplate:<div class="content"><?php print $content; ?></div>
Twig:<div class="content">{{ content }}</div>
顯示井字鍵
PHPTemplate:<?php print $item['#item']['alt']; ?>
Twig:{{ item['#item'].alt }}
指定變數:
PHPTemplate:<?php $custom_var = $content->comments; ?>
Twig:{% set custom_var = content.comments %}
指定陣列:
PHPTemplate:<?php $args = array('@author' => $author, '@date' => $created); ?>
Twig:{% set args = {'@author': author, '@date': created} %}
4.條件式
PHPTemplate:<?php if ($content->comments): endif; ?>
Twig:{% if content.comments %} {% endif %}
PHPTemplate:<?php if (!empty($content->comments)): endif; ?>
Twig:{% if content.comments is not empty %} {% endif %}
PHPTemplate:<?php if (isset($content->comments)): endif; ?>
Twig:{% if content.comments is defined %} {% endif %}
PHPTemplate:<?php if ($count > 0): endif; ?>
Twig:{% if count > 0 %} {% endif %}
5.控制結構
PHPTemplate:<?php foreach ($users as $user) {} ?>
Twig:{% for user in users %} {% endfor %}
6.過濾器
跳脫 HTML 特殊字元:
PHPTemplate:<?php print check_plain($title); ?>
Twig:{{ title }}
原始值:
PHPTemplate:<?php print $title; ?>
Twig:{{ title|raw }}
翻譯:
PHPTemplate:<?php print t('Home'); ?>
Twig:{{ 'Home'|t }}
帶有替換的翻譯:
PHPTemplate:<?php print t('Welcome, @username', array('@username' => $user->name)); ?>
Twig:{{ 'Welcome, @username'|t({ '@username': user.name }) }}
Drupal 8 Twig(with trans tag extension):
{% set username = user.name %}
{% trans %}
Welcome, {{ username }}
{% endtrans %}
串接列表:
PHPTemplate:<?php echo implode(', ', $usernames); ?>
Twig:{{ usernames | join(', ') }}
帶有標記的串接列表:
Twig:{{ usernames | safe_join(', ') }}
PHPTemplate 的範例需要 $usernames 是一個字串陣列。原始的 Twig 範例也是一樣,其中 "usernames" 是一個字串陣列。另一方面,Drupal 8 Twig 的範例則需要一個可渲染對象的陣列。這實際上是 Drupal 8 Twig 和原始 Twig 之間的基本區別。Drupal 8 Twig 顯示純文本和可渲染的陣列。
這個範例的另一個方面是,人們預期所有三個範例會產生相同的輸出,但實際上並不是(默認情況下)。我們看一下這個例子:
{% set numbers = [{'#markup': 'One'}, {'#markup':'Two'}, {'#markup':'Three'}] %}
{{ numbers }}
上面的例子表明項目之間以逗號分隔顯示。但輸出是:OneTwoThree
7.空白字符控制
Twig 具有空白字符控制,允許刪除用於結構化模板文件的空白字符。
<div class="body">
{{- block.content -}}
</div>
等同於
<div class="body">{{ block.content }}</div>
#Notes
在第二個例子中,我們展示了 Twig 如何負責消毒資料。以前這也是模板文件或預處理函數的職責。這對於希望為 Drupal 8 創建 PHPTemplate 主題的任何人來說是一個很大的變化 - 您將需要消毒您自己的資料。