番外篇:原生twig模板引擎详解(上集:设计师篇)

龹龶 twig 龨龤龠龝龜龘龔龒龎龌龉龅龂齿齼

齹齷龌齳齷齱齮齬齪 drupal 齚齙齖齔齒齐齌齉齇齃齿齂齁鼿 drupal 鼾鼺鼷 twig 龨龤龠龝齂鼳鼰鼬齿鼨龜鼤鼢鼟 twig 齂鼞鼚鼖鼔鼓 drupal 鼏鼎鼍鼊齂齱龹龶 twig 齉鼇鼺龜龘齂齖龒鼃鼀龎齂龒龎黼龉龅龂黻黷齂黶龘黳龨龤黱齉鼇鼺齂鼃龎黼 php 黭黫黩黨黧黣黻黷齂黶龘黭黫黢鼺點黜黛黩黨黚

齳齿默龒龎齂黖齃鼢鼟鼷黕黑黎黌黊鼍鼊齂黈黆黅黂黁麽黧黣麼黻齂麸龨龤龉龅龂麴黈麲麯黚

麬麪麧麣龌 https://twig.symfony.com/

齳齷麃麀鹽麬麪鹼鹻黨鹺鼷 V1.35.3 V2.4.8

黕黑龌

鹟鹜龌

php 鹘鹕齂黨鹺鹔鹑齳鹍齂黈齹齉鹑齳鹉鹅鼖鹃鹁鹘鸿鹽鸻齂鼳鼰 twig 鸷齹鸵鼀鸲鹑齳鸮黳鹃鹁

1.X 鹑齳龌鸫鸪 PHP 5.2.7 齂鸝 1.34 黩鸙鸫鸪鹟黊 PHP 5.3.3.

2.X 鹑齳龌鸫鸪 PHP 7.0.0

鸒鸎 drupal8 黳鼍齂鸋鸊鸆鸷鸮鸄黳鼇鼺 1.X 齂鸃鷿黈 1.X 鷾鷺黊鼢鼟齂鸵鸊鷹鷵鷳黕黑 twig 齂麬鷵鷰鷭鼇鼺 composer 齂鷦鷣鷟鼃鷛鷙鷗麴龌

composer require twig/twig:~1.0

默鼷鷐鷍鷌鷈 twig 鸄鷐黼鼷鸵鷇鷃鷈齉 php 鷂鷁齉 C 黜黛齂龜鶹麬麪齂鸃鷿鶵鶴黈鼃鶳齷鶰鷵鶯鶭黕黑黚

黕黑龌

鶪鸃鷿鼃鶳齷鶰鸒龌 https://github.com/twigphp/Twig/tags

齳齷鶦鶤龌 v1.35.3 龘鶠鼃鶳齉 Twig-1.35.3.tar.gz 齁鼿 twig 鼇鼺 psr-0 齉鶚鶘鶗鶳鷵鶯齂鶔黈鷿鷁齉 lib 鶐齱鶵鶴鹟黊齉鶎齷鶰齂鼨鶋鶇鶃鶪麪鵿鵾鸷鵺鷗麴

鵸麴黈黳齳鵷鵶麯 phpstudy 黎鵵鶰黕黑鷦鷣鵱鵰齂鸃鷿黈 C:\root\twigtest 麀默麪鵿鵾鸷鵺齂黈 http://www.twigtest.com/ 麀默鵫鵩

鵾鸷鵺鵥鵢 index.php 齂鼍鼊鷟鼃龌

<?php

require_once 'lib/Twig/Autoloader.php';

Twig_Autoloader::register();

$loader = new Twig_Loader_Array(array(

    'index' => 'Hello {{ name }}!',

));

$twig = new Twig_Environment($loader);

echo $twig->render('index', array('name' => 'Fabien'));

鴷鴴鵫鵩齂鷗麴鴰鶪鴯鴫齂黕黑鴧鴣黚

黩鸙鼇鼺龌

鴡龉鵸鹼鹻鴝齹鹘鴜龉鴙鴘鼷鸷鵺鴖齷鶰齂鶵鶴鶭鸝齷鶰齐鴒鶗鶳鹘鸲龨龤齷鶰齂鴑鷣鴍鴌龌

黳鵾鸷鵺鵥鵢鴉鸷鵺鴈 templates 鴆鼺黈鴄鴀龨龤齷鶰齂黳鶋黱鵥鵢齷齳齷鶰鴈 index.html.twig 鴆齂鼍鼊鷟鼃龌

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>twig</title>

</head>

<body>

hello world ! {{name}}

</body>

</html>

黳麪鵿鵾鸷鵺鳵鳴鵥鵢 index.php 齂鼍鼊鷟鼃龌

<?php

require_once 'lib/Twig/Autoloader.php';

Twig_Autoloader::register();


$loader = new Twig_Loader_Filesystem('templates');

$twig = new Twig_Environment($loader, array(

    'cache' => 'templates_cache',

));

echo $twig->render('index.html.twig', array('name' => 'yunke'));

鹅鹍鴷鴴鵿鳯 http://www.twigtest.com/index.php 齂鵸鼨鼖鴰鶪龌

hello world ! yunke

鳮鹽黳鵿鳯鵾鸷鵺鼃鸊鳬鼷鹘鸲鴉鸷鵺龌

templates_cache

鶋黱鴄鴀鳪默鷐鷍鷌鷈鳩鳥默 php 齷鶰齉龨龤齷鶰齂鳢鳠鳟鳩鳥鹘黂齂鷟龹齷鶰黨龶鳛鳗鹟鳵鳴鳩鳥龔鳖鳕鳓鴄齼

黳黈龒鳑齙黱 Twig_Environment twig 齉黱鳐鳏鳌齂鳋黁鳉鼀鸲鳅鳂龌

\Twig_Environment::__construct (Twig_LoaderInterface $loader = null, $options = array())

$loader 齱鲼鲺鼷 \Twig_LoaderInterface 黁鲹齉鶗鶳鲵齂鼺鼿鶗鶳龨龤齷鶰

$options 齱鶦鸆鳂鲴齂鲳鶃 twig 鷣默齂鸵鲯鶦鸆鷟鼃龌

debug 齱鲬鲨鼿黢鲧龨鶯齂鹺鲦鲤齂鳢鳠默 false

charset 龨龤鼇鼺齉鲡鲟鳩齙齂鳢鳠默 UTF-8

base_template_class 鲛鲘龨龤鳋齂鼺鼿鲖鳩鳥鹍齉龨龤 php 齷鶰齂鳢鳠默 Twig_Template

cache 麴鶦鲤鸵鲓鴄鳩鳥鹍龨龤齉鸷鵺齂鲒鲐默 false 黈鲏鼺鳩鳥鳓鴄齂鸃齱鳢鳠鲤齂鲋麴黈齱鳓鴄鳏鳌龔黁鲹龌 Twig_CacheInterface 齉鲼鲊齂鶹黩黨鲐齿齼

auto_reload 鹺鲦鲤齂黳龹龨龤齷鶰鲇鳛鹽齱鲬鳵鳴鶗鶳齂鷟鴫鲄鸵鷐黼鲁鱿鼨鵾鱻 debug 鶦鸆鱺鱹

strict_variables 齱鲬鼇鼺鱷鱶鳛鱴龨鶯齂鳢鳠默 false 齂默鱲鹽龨龤黱鼇鼺鱯鲯鳛鱴鼨鱫鳬鱪鱦齂鲬鱥默 NULL

autoescape 齱鲬鱡鱟鶚鶘鱝鱚齂麴鷈齉鲤鸵龌 false 龔鲏鱖鱝鱚齼齂 true 龔鳢鳠鲤齂黎鲯默 html 齼齂 html 龔鱝鱚 html 鲼鱒齼齂 js 龔鱝鱚 js 齼齂 css 龔鱝鱚 css 齼齂 url 龔鱝鱚 url 齼齂 html_attr 龔鱝鱚 html 鹙鱍鷌齼齂 name 龔鲛鼿龨龤黜黛鵩龉鴙鶚鶘鱝鱚鱊鱆齼齂 PHP callback 龔齁鹘鸲 php 鱅黢鶭鱄鱅鱝鱚鱊鱆齂鼬鱅黢黁鳉龨龤齉鵩鲡鷾鳅鳂齼

optimizations 龨龤鳩鳥鹽齉鱁鳗鶦鸆齂鴈 -1 鴆鳑鱀黩鰿鱡鰻鱁鳗龔鳢鳠鲤齼齂鴈 0 鴆鳑鱀鲏鼺鱁鳗齂鸄鸵鶋鰺鲤龜鶹鼃龎黱鱁鳗黜黛鹘鰹

鰵鰴鳑齙龌 $twig->render('index.html.twig', array('name' => 'yunke')); 龹鰰默龌

render($name, array $context = array())

鰮鹘鸲鳅鳂齱龨龤齉齷鶰鵩齂鼺鼿鶗鶳龨龤

鰮鰭鸲齱鰬鰩鰧龨龤齉鳛鱴鳂鲴齂鰦鵩默黳龨龤黱齉鳛鱴鵩齂鰦鲤默鳏鰢齉鳛鱴鲤齂鼬鳛鱴鳂鲴鰟默龨龤齉鴈龒鼃齷鴆齂黳龨龤黱鵶麯鴈 _context 鴆鷈鴷鴴鶪鼬鳂鲴齂鼃齷鼨鸊黂鷐鶪鴈龒鼃齷鴆鶐齱鰝鼬鳛鱴鳂鲴

龨龤齷鶰鷛鵩龌

twig 龨龤齱鹘鸲鸿齷齳鼍鼊齂麴黈鴄鴀黳鳂鱻鶎點 php 鳛鱴點齷鶰黎麧鷵齂鷟鴫齱齷齳齷鶰齂鷛鵩齱鼏鰙齉齂鼏鰙黜黛鵩鰕麴齂鼞鵶鰔鼇鼺鴈 .twig 鴆鲒鴈 .html.twig 鴆麀默黜黛鵩

---- 龒龎鰻齖龔龉龅龂齿齼 ----

鳢鳠鰑鱹鲟龌

{% ... %} 鰋鷣鰇鰅齂鼺鼿鰋鷣鰃鰀齂鷟鲳鶃鴯鯾

{{ ... }} 鯼鯸鰇鰅齂鼺鼿鯼鯸鱀鯷鶯齉龅鯴鴯鴫

{# ... #} 鯱鯰鰇鰅齂鼺鼿鯱鯰齂鳋鯭黭黫黱齉鯱鯰龌 /* ..*/

#{…} 鯨鲤鰇鰅齂鼺鼿鯥鱀鯷鶯黱齉鲡鲟鯡鯨鯠鳛鱴鲤

鸃鯟鱹鰑鲟鳟齱 twig 齉鳢鳠鲤齂齱麴黈鴧鱡鶚鱹鱚齉齂龜鶹鼃龎黩黨鲐齿

鲡鷁鱴 Literals

鲡鲟鯡龌鷟 "Hello World" 齂麴鼺鴈 \ 鴆鱝鱚鯛鯙鲡鲟齂鷟 'It\'s good' 'c:\\Program Files'

鳂鲡龌鸒鸎鯖鳂鹙鯔鳯鳂齂鷟 4/3.14

鳂鲴龌鷟 ["foo", "bar"] 齂鹙 php 鼚鳮齂鼚鴄黳鰦鵩齂鯏齱鹘鸲鯍鯋齉鳂鲡鯇龠鳂鲴齂麴鯄鯀黚鷟龌

{% set a = ["one", ["two", "three"]] %}

黊鮿鮻鳬鴈 three 鴆麴鸃鹕鴷鴴龌 {{ a[1][1] }} 齂鯱鰙鼾鼺鳯鮺鰃鷳鹽鼚鷈鸃鹕龌 {{ a.1.1 }} 齂鮹黊鸃鹕龌 {{ (a.1).1 }} 齂鸃鸵鳯鮸鮷鮵鮱

鮭鮫鱀龌黎鲯鼿 php 齉鮪鮧鳂鲴齂鷟龌 {% set a = { 'foo': 'vfoo', 'bar': 'vbar' } %} 齂鰦鲤麴黈齱鲡鷁鱴點鳛鱴鲒鱀鯷鶯齂鶋黱鰦鵩鲋麴黈鼚鼺龠鮺齂鼚黊龠鮺鹽齂鮥鼚黎鼿鰦鵩鼾鼺鼷鹘鸲鳛鱴齂鸃鸵鳯鮸鮷鮵鮱齂鮤鴰鳑齙龌

{% set foo = "yunke" %}

{% set a = { foo: 'vfoo', 'bar': 'vbar' } %}

{{ a.yunke }}

鸃鼨鼚鼖鸵鼏鼎鮻鳬齂 foo 鮥鼚鲖鳠默齱鳛鱴齂鮹齱鲡鷁鱴齂黊鮿鮻鳬 'vfoo' 鰢鼬鼇鼺 {{ a.foo }} 齂鷟鴫黊鮿鼨 foo 鮢鷾鳛鱴鴰鮡齂鲁鱿鹟黊默鶋鶗龒鸎鮺齂鷟鼃龌

{% set foo = "yunke" %}

{% set a = { (foo): 'vfoo', 'bar': 'vbar' } %}

{{ a.yunke }}

鼰鹽鶐麴黈鮻鳬 'vfoo' 鼷齂 鶗鸎鮺鲼鮠齱鼾鼺鼷鱀鯷鶯鰦鵩齂鼰鹽鸄麴黈鸃鹕龌

{% set foo = 'foo' %}

{ (foo): 'foo', (1 + 1): 'bar', (foo ~ 'b'): 'baz' }

鳂鲴鹙鮭鮫鱀麴黈鮚鮖鼇鼺齂鮕麴黈鯄鯀

鹺鲦鲤龌 true / false 齂鲄鸵龠鮺

鮒鲤龌 null 齂鳑鱀鲤鼚鴄黳齂鮏鸵鸲鮎鵩龌 none

鴷鴴鳛鱴鼍鼊龌

{{yunke}}

鮵黁鴍鴌鳛鱴鴈 yunke 鴆齉鼍鼊齂鰑鱹鲟鼍麴黈鸵鮒鱶齂鷟 {{ yunke }} 齂鼃鳮

{{yunke.name}}

鼺鳯鮺麴黈鴷鴴鳏鳌齉鱍鷌鲒鳂鲴齉鮍鮉齂鸃鷿鴈 yunke 鴆麴黈齱鳏鳌鲋麴黈齱鳂鲴齂 name 齱鱍鷌鵩鲒鳂鲴鰦鵩

{{yunke[‘name’]}}

鲋麴黈鼺鼃鰇齂鼞鼚鷈鼺鼿鳏鳌齂鼰鹽鴈 yunke 鴆鳟鷈齱鳂鲴

{{ attribute(foo, 'data-bar') }}

鷟鴫鱍鷌鵩黱鸒魾鯛鯙鲡鲟鹽麴黈鼇鼺鱍鷌魻鳂齂鷟鴈 - 鴆麴鷈鹙魷鮺鮚魵齂鱍鷌魻鳂麴魴魳鰃鱚

鷟鴫鳛鱴鼚鴄黳齂鼨魯鶪鹘鸲 NULL 鲤龔黳鴍鴌龒魭鱿鲋鲄鸵齼齂鷟鴫鶦鸆黱鴈 strict_variables 鴆鲤默鱲齂鲁鱿鼨鱫鳬鱪鱦齂黳鼍鰻鳯鮺鴈 . 鴆鷵鶯鲼鮠齱鰋鷣黈鼃魩魥齂黈 foo.bar 默齌龌

1 點魣麼 foo 齱鲬齱鹘鸲鳂鲴鮕 bar 齱鶋齉鸵鲯鰦鵩齂鷟鴫齱鱥鱄鱅齂鲬鱥魢魞

2 點魣麼 foo 齱鲬齱鹘鸲鳏鳌齂 bar 默鶋齉鹘鸲鱍鷌齂鷟鴫齱鱥鱄鱅齂鲬鱥魢魞

3 點鷟鴫 foo 齱鹘鸲鳏鳌齂魣麼 bar 齱鲬默鹘鸲鸵鲯齉鷵鷳齂齱鱥黢鼺齂鲬鱥魢魞

4 點鷟鴫 foo 齱鹘鸲鳏鳌齂魣麼齱鲬鴄黳 getBar 鷵鷳鴄黳齂齱鱥黢鼺齂鲬鱥魢魞

5 點鷟鴫 foo 齱鹘鸲鳏鳌齂魣麼齱鲬鴄黳 isBar 鷵鷳鴄黳齂齱鱥黢鼺齂鲬鱥魢魞

6 點鷟鴫鸮鼚魖魕齂魔鹍鱄鱅 NULL

foo['bar'] 鳟魐鼺鼿鳂鲴齂鳟鴑鷣鼀鴜魣麼龌

1 點魣麼 foo 齱鲬齱鹘鸲鳂鲴鮕 bar 齱鶋齉鸵鲯鰦鵩齂鷟鴫齱鱥鱄鱅齂鲬鱥魢魞

2 點鱄鱅 NULL

鶔黈鷺魌黩黨鲐麴黈鱁魈鼇鼺鳯鮺鷵鶯齂鯱鰙 foo.bar bar 麴黈齱鳂鲡齂鳑鱀鳂鲴齉鳂鲡鯇龠齂鷟鴫鼰鹽鼇鼺黱鸎鮺鷵鶯齂鳂鲡鶗鼚鶗龠鮺鸮鲄鸵鮪齐

鷟鴫鮿鴷鴴鹘鸲鳛鱴齉鶘魅鱍鷌齂麴黈鼇鼺 attribute 魻鳂鳑魃齂鶹鹍

{{ " 鲡鷁鱴 " }} {{ "{{}}" }}

麴黈鵶麯鸃鷹鷵鶯鮵黁鮻鳬鲡鷁鱴

鱡鱟鳛鱴龌

齱鰝黳龨龤黱鬿齱鸵鲯齉鱡鱟鳛鱴齂鳢鳠鸵鷟鼃龌

_self 龌龠鼺鶪鮢齹龨龤齂鲤默鼚鬻鬺鬷齉龨龤齷鶰鵩

_context 龌龠鼺鶪鮢齹龒鼃齷齂鲋鶐齱 $twig->render() 齉鰮鰭鸲鳅鳂齂鯏鬿齱鳂鲴

_charset 龌龠鼺鶪鮢齹龨龤齉鲡鲟龎齂鲤鷟鴈 UTF-8

黩黨鲐麴黈鵶麯黜黛鷐黼鬴鸊鱡鱟鳛鱴齂鶹黩黨鲐齿

龉鴙鳛鱴龌

龨龤黩黨鲐麴黈黳龨龤黱鬳鵥鳛鱴齂鼇鼺鴈 set 鴆龌

{% set foo = 'foo' %}

{% set foo = [1, 2] %}

{% set foo = {'foo': 'bar'} %}

鬰麀鲟龌

鬰麀鲟鱁魈鬭齁鬬鶪鷍鹉黂默龔黈鴈 , 鴆齖鬪齼龌

b-and, b-xor, b-or, or, and, ==, !=, <, >, >=, <=, in, matches, starts with, ends with, .., +, -, ~, *, /, //, %, is, **, |, [], .

鳯鮺鬰麀鲟鱁魈鬭魔鷍齂鶔鸵鬰麀鲟鷟鼃龌

鳂鬥鷦鯴鲟龌

黳龨龤黱麴黈鼇鼺鯴鬤鷦鯴鲟鮺龌

+ 龔鶗齼齂 - 龔魷齼齂 * 龔鬢齼齂 / 龔鳕齼齂 % 龔鹜鬟齼齂

// 龔鬜鳕鹍鯥鼃鬙鯖齂鬗鬓鷳鬙鯖齂鷟 20//7 鴯鴫默 2 -20//7 3 齼齂

** 龔鬑鷦鯴齂 2**3 黎鼿 8 2 3 黂鷵齼

魩魥鷦鯴鲟龌

and 龔鴖齼齂 or 龔鲒齼齂 not 龔鬎齼

鴝鬍鴖點鲒點鬌鲒龌 b-and b-or b-xor

魩魥鷦鯴鲟齱鹔鬋麃鬉鬆齉齂黌鬃鱡鬋麃

髿髻鷦鯴鲟龌

麴鼺鰔髹鷦鯴鲟鷟龌 ==, !=, <, >, >=, <=

鯱鰙鲄鸵鴈 === 鴆點鴈 <> 鴆齂黖齃齉鷦鯴鲟鸵龌 starts with ends with

{% if 'Fabien' starts with 'F' %}

{% endif %}

{% if 'Fabien' ends with 'n' %}

{% endif %}

鰺鶴魣麼齱鲬黈鲡鲟鯡黩鸙鲒鴯髸齂齱鹔鬋麃鬉鬆齉

鬴鶗鶇髶齉髿髻鷦鯴鲟麴鼺鼇鼺髵鱥鱀鯷鶯龌 matches 齂鴌鲊鷟鼃龌

{% if phone matches '/^[\\d\\.]+$/' %}

{% endif %}

鯱鰙髵鱥鱀鯷鶯黱鸵鴈 \ 鴆鹽齂鹟黊髱鱝鱚

鸒魾鬰麀鲟龌

鼇鼺鴈 in 鴆鲒鴈 not in 鴆黈髯鲧齱鲬鲖鸒魾龌

{{ 1 in [1, 2, 3] }}

{{ 'cd' in 'abcde' }}

{% if 1 not in [1, 2, 3] %} 等价于:{% if not (1 in [1, 2, 3]) %}

髯鲧鬰麀鲟龌

twig 黱龉鴙鼷鹘鯟髯鲧鱀鯷鶯齂麴鼺鴈 is 鴆點鴈 is not 鴆鬰麀鲟鴑鷣髯鲧龌

{{ num is odd }}

鸃髯鲧鹘鸲鳂齱鲬默髪鳂齂鬴鸊髯鲧鱀鯷鶯鷟鼃龌

constant defined divisibleby empty even iterable null odd sameas

鶹龌 https://twig.symfony.com/doc/1.x/

鶋鰺鬰麀鲟龌

| 鴆龌髨髤鲟齂鼺鼿鰢鼺鹘鸲麯髡鲵

.. 鴆龌髱鳯鮺齂鼺鼿鬳鵥鹘鸲鳂鲴

鷟黳 {% set a= 1..50 %} a 鼨齱鮍鮉鸝 1 黩鸙 50 鴯髸齉鳂鲴齂黎鲯鼿 {{ range(1, 50) }}

~ 鴆龌髞黁鲟齂鴧鱡黎鳮鼿 php 齉鳯鮺鴈 . 鴆齂鼺鼿鼨鬰麀鳂鱝鳗默鲡鲟鮥髞黁髚鹘髗

?: 鴆龌髕鮍鬰麀鲟齂鴖 php 黎鲯齂鸝 Twig 1.12.0 黩鸙齂黈鼃鼺鷳齱黎鲯齉龌

{{ foo ?: 'no' }} {{ foo ? foo : 'no' }} 鬜鳮

{{ foo ? 'yes' }} {{ foo ? 'yes' : '' }} 鬜鳮

?? 鴆龌鴡龉 {{ foo ?? 'no' }} 齂鷟鴫 foo 鲖鱹鱚鼷鮕鼚默 null 鲁鱿鶐鱄鱅 foo 齉鲤齂鲬鱥鱄鱅鴈 no

鱀鯷鶯鯨鯠鲡鲟鯡黱龌

麴黈鵶麯鴈 #{ } 鴆鯥髱龠鮺鼍齉鲡鲟鯡黱鯨鯠鱀鯷鶯齉鲤龔鯋龠鮺鱯鲯髓黌鬃齱鱀鯷鶯齂鯋髏鼺鼿龨龤鱯鲯齼龌

{{ "foo #{bar} baz" }}

{{ "foo #{1 + 2} baz" }}

鮒鱶鲳鶃龌

鳢鳠髋髈鼃齂鰇鰅鹍齉鰮鹘鸲髅鷣鲟鲖鳖鳕齂鼞髃鹍齉髅鷣點鶃鱀 tab 點鮒鱶黎鮒髀鼚鼖鲖骽鲇齂麴黈鼇鼺鴈 spaceless 鴆鰇鰅鬓骺鳕鹘鸿鼍鼊齉鶔鸵鮒髀龌

{% spaceless %}

    <div>

        <strong>foo bar</strong>

    </div>

{% endspaceless %}

鸃鼨鮻鳬龌

<div><strong>foo bar</strong></div>

鲋麴黈黳鰇鰅鼍鼇鼺魷鮺鶭鳖鳕骸骷鰇鰅齉齹鲒鹍鮒髀龌

{{- value -}}

鼰鹽魷鮺鲖鳵鶳默鮒髀骽骵鲟齂鼚黌鴣鳏鼇鼺

麯髡鲵龌

麯髡鲵鼺鼿鳏鳛鱴鴑鷣骽鲇齂鼇鼺髨髤鲟鮺鴈 | 鴆齖鬪齂麴黈鼇鼺骴鶯麃鷳齂齹鷁齉鮻鳬齱鹍鷁齉鮻鯠齂鷟鳏鳛鱴 name 鬓鳕 html 鰇鰅齂鮥鼾鼺鰇魌鱶鶯龔骰鸲鯋骭黅鲡骬鹔麃齼龌

{{ name|striptags|title }}

麯髡鲵麴黈鬻鳅鳂齂鷟鼨鳂鲴鮍鮉髞黁髗鶭龌

{{ list|join(', ') }}

鲋麴黈鳏鯖鸿鼍鼊鼇鼺麯髡鲵齂鷟鼨鹘鰀骫鹔麃龌

{% filter upper %}

    This text becomes uppercase

{% endfilter %}

twig 鳢鳠鷐黼齉麯髡鲵鮤鶹龌 \Twig_Extension_Core::getFilters

鲒麬鷵齷骧龌 https://twig.symfony.com/doc/1.x/

黩黨鲐麴黈鵶麯黜黛鷐黼鬴鸊麯髡鲵

鲡鲟鱝鱚龌

鸵鯟鳛鱴黱齉鼍鼊鼖骥骢 html 鰇鰅齂髿鷟骡鸎鮺點鯋髱龠鮺點鹙鮺 & 黎黎齂鲁鱿鶐鹟黊鱝鱚齂鸵鼀鷹鲨骜骘鷳龔麴鮚鼺齼龌

1 點鼇鼺鶦鸆鳅鳂 autoescape 鶚鶘鱝鱚鱡鰻龨龤鳛鱴齂鶹齹齷鼢鼟齂鳢鳠齱黩鰿齉齂鱝鱚鱊鱆齱 html

2 點鼨 autoescape 龉鴙默 false 鮪骔鱡鱟鳛鱴鶚鶘鱝鱚齂鼰鹽龨龤黩黨鲐鹟黊鳏鼚骒鼏齉鳛鱴骑鶘鱝鱚齂鬰麀鷟鼃龌

{{ yunke.risk|e }}

骏鶯龒鸃鹙鼇鼺麯髡鲵齱鹘鹕齉齂鴈 e 鴆齱鴈 escape 鴆齉骎麃齂鳢鳠齱鼇鼺鴈 html 鴆鱝鱚齂鹟黊鰝鱹鶋鰺鱊鱆鮤龌

{{ yunke.risk|e('js') }}

麴鼺齉鱊鱆鳅鳂鸵龌 js css html url html_attr

鳕鳏鯋鸲鳛鱴鱝鱚鹙鱡鱟鱝鱚齃齂鸄麴黈 鳏龨龤鷿鹘鸿鼍鼊黱齉鱡鰻鳛鱴鱝鱚 龔鱟鰻鱝鱚齼龌

{% autoescape 'js' %}

鳟黊黳鸃鸲鰇鰅鼍鰻齉鼏鼎鳛鱴鸮鼖鱝鱚齂鯱鰙齱鳛鱴齂鲋鶐齱 {{}} 鷿齉鼍鼊齂龨龤鼍鼊鼚鼖鱝鱚

{% endautoescape %}

骋鱆鳅鳂鱥鳢鳠鼇鼺 html 鱝鱚齂黩鰿鱟鰻鲒鱡鱟鳛鱴鱝鱚鹽齂鹟黊鮻鳬鼚鱝鱚鼍鼊黊鼇鼺鷟鼃鷵鶯龌

{{ var|raw }}

鼚髨鱡鱟鱝鱚鷟鼎龉鴙齂 autoescape 鰇鰅鼨鲇鳛鱟鰻齉鱝鱚龨鶯齂鷟鼃龌

{% autoescape 'html' %}

    {{ var }}   {#不论全局转义如何设置 var只进行html转义 #}

    {{ var|raw }}      {# var 不转义#}

    {{ var|escape }}   {# var 只转义一次 不会双转义 #}

{% endautoescape %}

鯱鰙龌鰇鰅 autoescape 鼚鼖骥骢鶪鶋鼍鰻鼇鼺 included 鸒魾鴑鶭齉龨龤

黩鰿鱡鱟鲒鱟鰻鱝鱚鹍鸵鷟鼃髹鱥鹟黊鯱鰙龌

鼚鳏鲡鷁鱴鱝鱚龌

{{ "Twig<br />" }} {# 不转义 #}

{% set text = "Twig<br />" %}

{{ text }} {# 将转义#}

鱀鯷鶯鴯鴫龌

{{ foo ? "Twig<br />" : "<br />Twig" }} {# 不转义 #}


{% set text = "Twig<br />" %}

{{ foo ? text : "<br />Twig" }} {#不论foo为真或假 都会转义结果#}


{% set text = "Twig<br />" %}

{{ foo ? text|raw : "<br />Twig" }} {#不论foo为真或假 都会转义结果 #}


{% set text = "Twig<br />" %}

{{ foo ? text|escape : "<br />Twig" }} {# 当转义策略和外部策略相同时不会再次转义#}

麯髡鲵骆黫龌

鱝鱚鲖鼺黳麯髡鲵鹍齂鮻鳬齹齂鷟龌

{{ var|upper }} 黎髮鼿 {{ var|upper|escape }}

麯髡鲵鴈 raw 鴆鰢鼬鲖鼺黳魔鹍龌

{{ var|raw|upper }} {# 鼨鱝鱚 #}

{{ var|upper|raw }} {# 鼚鱝鱚 #}

鷟鴫魔鹍鹘鸲麯髡鲵鹙鮢齹鱝鱚鱊鱆鬜鳮齂鲁鱿鼚鼖鲖髚黂鷦鼺龌

{% autoescape 'js' %}

    {{ var|escape('html') }} {# 将再次对结果运用js转义 #}

    {{ var }} {# 只运用js转义 #}

    {{ var|escape('js') }} {# 不会再次转义 #}

{% endautoescape %}

鳛鱴鱝鱚鸵鱟鷇鷌齂鴡龉黳鱡鱟鱝鱚默 js 鹽齂黈鼃鳑齙齉鴯鴫鼚齱鵸鮿鳌齉鹕鴉龌

{{ yunke.risk|raw ~ yunke.risk }}

鯏鴰髗鶭齱龹齷齳鶗 js 鱝鱚鹍齉鴯鴫齂鼞鲼鮠龒鱡鰻齱 js 鱝鱚齉鴯鴫齂鹙鲄鸵鶗鴈 raw 鴆麯髡鲵鹽鲯鴫鹘鹕齂鶔黈鶵鶴黳龉龅龨龤鹽鰢鮢骄鱴骂驾鶇髶齉鱝鱚鮥驺齖髯鲧齂

魻鳂龌

麴黈驷 php 鹘鹕鼇鼺魻鳂齂鷟龌

{% for i in range(0, 3) %}

    {{ i }},

{% endfor %}

Twig 1.12 黩鸙黢鼺魻鳂鹽齂麴鼚鴝驶鳅鳂骆黫驲鲤齂麴黈鰝鱹魻鳂龹鰰黱齉鳅鳂鵩齂鷟龌

{% for i in range(low=1, high=10, step=2) %}

    {{ i }},

{% endfor %}

鸃鹕鼇魯鰃鱚鬴鶗魴魳齂麴黈驮鱆鳅鳂骆黫齂马默鰻齖鳅鳂驲鲤齂鶋鰺鳅鳂鼇鼺鳢鳠鲤齂鲋麴黈骆黫鷵鶯鹙鰝鱹鳅鳂鵩鷵鶯鮚鼺齂鼞骆黫鷵鶯鰬鰩齉鳅鳂黌鬃黳鰝鱹鵩鲡齉鳅鳂齹鷁齂鷟龌

{{ "now"|date('d/m/Y H:i', timezone="Europe/Paris") }}

魻鳂齉鳅鳂鵩麴黳麬鷵骑驨鳏鰢魻鳂鼢鼟齉鳅鳂鹘鰹鴰鶪龌

https://twig.symfony.com/doc/1.x/

黩黨鲐麴黈鵶麯黜黛鷐黼鬴鸊魻鳂

鲳鶃鴯鯾龌

鼺鼿驧鶰驤驣點驟鵱驜驛黎齂鲳鶃鴯鯾鬍鼿 {% ... %} 黱龌

驜驛鴯鯾龌

<ul>

    {% for user in users %}

        <li>{{ user.username|e }}</li>

    {% endfor %}

</ul>

鲄鸵驜驛鼍鼊鹽麴黈鴍鴌鳢鳠鲤龌

{% for user in users %}

    *<li>{{ user.username|e }}</li>

{% else %}

    No users have been found.

{% endfor %}

驜驛鳛鱴鰦鵩龌

<h1>Members</h1>

<ul>

    {% for key in users|keys %}

        <li>{{ key }}</li>

    {% endfor %}

</ul>

驜驛鰦鹙鲤龌

<h1>Members</h1>

<ul>

    {% for key, user in users %}

        <li>{{ key }}: {{ user.username|e }}</li>

    {% endfor %}

</ul>

驖鶗驧鶰驜驛齂马驜驛驧鶰默鱲齉驧鸷龌

<ul>

    {% for user in users if user.active %}

        <li>{{ user.username|e }}</li>

    {% endfor %}

</ul>

驜驛鰻齖鼍鼊龌

 <h1>Top Ten Members</h1>
<ul>
    {% for user in users|slice(0, 10) %}
        <li>{{ user.username|e }}</li>
    {% endfor %}
</ul>

for 鴯鯾黱麴黈鼇鼺鯛鯙鳛鱴 loop 齂鷟龌

loop.index 龌鮢齹驕鳑鼷鰮驓黂齂鸝 1 黩鸙

loop.index0 龌鮢齹驕鳑鼷鰮驓黂齂鸝 0 黩鸙

loop.revindex 龌驑鬟驕鳑黂鳂齂魔鹍齱 1

loop.revindex0 龌驑鬟驕鳑黂鳂齂魔鹍齱 0

loop.first 龌齱鲬默鰮鹘黂驕鳑

loop.last 龌齱鲬默魔鹍鹘黂驕鳑

loop.length 龌驕鳑齉驧鸷鬿鳂

loop.parent 龌龠鼺鶪齃鷂驟鵱龒鼃齷

黳鰝鱹驟鵱驧鶰鹽齂 loop 鳛鱴齱鱯鲯齉

鯱鰙黳 for 黱鲄鸵 break 鲒鲐 continue 鴯鯾齂驜驛麴黈驐鳏鳂鲴鲒鲐齱鲼鲺鼷驕鳑鲵黁鲹齉鳏鳌

驧鶰驤驣龌

{% if users|length > 0 %}

show user

{% endif %}

鲋麴黈髯鲧齱鲬默鮒龌

{% if users %}

    <ul>

        {% for user in users %}

            <li>{{ user.username|e }}</li>

        {% endfor %}

    </ul>

{% endif %}

鬙鮷龌

{% if not user.subscribed %}

    <p>You are not subscribed to our mailing list.</p>

{% endif %}

鲋麴黈鸊齖驏驧鶰驤驣龌

{% if 3 > yunke|length > 0 %}

    show 0-3

{% elseif 5 > yunke|length >= 3 %}

    show 3-5

{% else %}

    show 5-..

{% endif %}

齱鲬默 true 髹鱥鹙 php 鹘鹕龌

空字符串:false

数字0: false

字符串:true

非零数字:     true

空白字符串:true

字符串“0”:false

空数组:false

NULL:false

非空数组:true

对象:true

麀鼺鵫鲳鶃龌

鼇鼺 with 鰇鰅麴黈鬳鵥鹘鸲鳴齉麀鼺鵫齂鷿鷁齉鳛鱴马黳鼍鰻鸵鲯齂鰇鰅齃鰻鱯鲯齂鴌鲊鷟鼃龌

{% with %}

    {% set foo = 42 %}

    {{ foo }}           foo is 42 here

{% endwith %}

鳛鱴 foo 黳鼬鰇鰅黈齃齱鴷鴴鼚鶪齉齂黈龒齌鴉鸄麴黈髅鹘鷹鷵鶯驉麃龌

{% with { foo: 42 } %}

    {{ foo }}           foo is 42 here

{% endwith %}

鲋鶐齱鼨鳛鱴龎黱鶪鮭鮫黱鰬鯠髓鳢鳠髋髈鼃鳴鵥齉麀鼺鵫鼍麴黈鴷鴴齃鰻鳛鱴齂麴黈鼇鼺黈鼃鷵鶯鲏鱖龌

{% set bar = 'bar' %}

{% with { foo: 42 } only %}

    {# only foo is defined #}

    {# bar is not defined #}

{% endwith %}

鹙驈鵶鳩黭鰃驄鬜髿鲄鸵 while(){} do{}while() 黎鴯鯾齂鼞 twig 齉驁鹔驀鲨鶐齱麴黈鵶麯驖鶗黜黛齉鷵鶯騿鯏鸵齂鼚麯鳢鳠齱鲄鸵齉

鶗鶳鶋鰺龨龤龌

黳鹘鸲龨龤黱麴黈鵶麯鷟鼃鷵鶯鶗鶳鶋鰺龨龤龌

{% include('sidebar.html') %}

鲋麴黈鼇鼺鳛鱴齂鷟龌

{% include var ~ '_foo.html' %}

鲼鮠龒 include 鮪鰦骭鹍麴黈齱鼏鰙鱀鯷鶯齂鬴鶇髶齉鷟龌

{% include var|default('index') ~ '_foo.html' %}

鵶麯鸃鷹鷵鶯鸒魾鶋鰺龨龤鹍齂黳 include 鰃鰀鬍鴙鷈鼇鼺齉鸵鲯鳛鱴齂黳鲖鶗鶳齉龨龤黱鲋鸵鲯齂鷟鼃龌

{% for box in boxes %}

    {{ include('render_box.html') }}

{% endfor %}

render_box.html 黱麴黈鼇鼺鳛鱴 box 齂鲖鶗鶳齉龨龤鵩鹉騻鼿鶗鶳鲵齉鳋鰰齂鷟鴫齱齷鶰齐鴒鶗鶳鲵齂鲁鱿麴黈鼇鼺鬺鬷鵩黚

龨龤魢騹龌

鹘鸲龨龤麴黈騶鹘鸲騳鵫鱹鱚鴣騲齂鸃鹕鶋鰺龨龤麴黈騯魴魢騹鼬龨龤齂騯魴鹍鶐鴣鼷鼬龨龤齉鴉龨龤齂鬜鳏齉鼬龨龤鶐齱騭龨龤齂鴉龨龤麴黈鵶麯騲鵩騫麃騭龨龤黱鱹鱚齉騲齂鷟鴫鼚騫麃鼨鼇鼺騭龨龤齉鼍鼊齂鸃鰟默龨龤魢騹齂騭龨龤騯魴騲鹙鴉龨龤騫麃騲鸮鼇鼺鷟鼃鷵鶯龌

{% block blockName %}…{% endblock %}

黳鴉龨龤黱騯魴魢騹鹟黳鰮鹘鷣鼇鼺鷟鼃鰃鰀龌

{% extends "base.html" %}

鸃鷿 extends 麴黈鼇鼺鱀鯷鶯黈鶗鶳鼚鳮龨龤齂鷟龌

{% extends request.ajax ? "base_ajax.html" : "base.html" %}

龨龤魢騹鴌鲊鷟鼃齂髿鷟 a.html.twig 齱騭龨龤齂鼍鼊鷟鼃龌

<!DOCTYPE html>

<html lang="zh-cn">

<head>

    <meta charset="UTF-8">

    <title>twig</title>

</head>

<body>

{% block yunke %}

    <div>我是父模板a.html.twig中的内容</div>

{% endblock %}

</body>

</html>

b.html.twig 齱鴉龨龤齂鼍鼊鷟鼃龌

{% extends "a.html.twig" %}

{% block yunke %}

    <div>我是子模板b.html.twig中的内容</div>

{{ parent() }}

{% endblock %}

鼇鼺 echo $twig->render('b.html.twig'); 鼨鮻鳬龌

<!DOCTYPE html>

<html lang="zh-cn">

<head>

    <meta charset="UTF-8">

    <title>twig</title>

</head>

<body>

    <div>我是子模板b.html.twig中的内容</div>

    <div>我是父模板a.html.twig中的内容</div>


</body>

</html>

鷟鵸鶔鶹 {{ parent() }} 鼺鼿鮻鳬騭龨龤黱齉騲鼍鼊

鼚髨齱鴉龨龤鸄齱騭龨龤齂鰺鶴騧騦鬜鳮齉龒鼃齷鳛鱴

鹟黊鯱鰙鷟鴫 B 魢騹鼷 A 齂鲋鶐齱騣 B A 齉鴉龨龤齂鲁鱿黳鮻鳬鹽 B 鷿鷁鳟鷈鳏 A 齉騲鴑鷣騫麃齂鼚鷈鮻鳬騲齃齉鼍鼊齂鼞 b 鷿鷁麴鴑鷣鳛鱴驲鲤黎鬎鮻鳬鬰麀齂黳 b 鷿齉魩魥魈鼿 a 鰋鷣齂鼳鼰黳 b 黱鳏鳛鱴鲒龒鼃齷齉驲鲤鼨鰢鼺黳 a

騟鷛鷙 Macros

鮢鸵鯟龨龤鼍鼊鼖鲖鮷鶇鸊黂鼺鶪鹽齂麴黈鼨鶋鱹鱚默騟齂黳鼇鼺齉麧鷵鴑鷣騟黢鼺鷗麴齂鷿鷁鹟黊鼺鶪齉鳛鱴麴黈鵶麯騟鳅鳂鰬鯠齂鸃鸵鳯驷鳩黭鰃驄黱鱹鱚魻鳂齂鳴鵥鹘鸲龨龤齂黳鷿鷁鱹鱚騟龔鷟鳮黳魻鳂鶎齷鶰黱鱹鱚魻鳂齼齂鴡龉龨龤鵩默 macrod.html.twig 齂鼍鼊鷟鼃龌

{% macro input(name, value = "", type = "text", size = 20) %}

    <input type="{{ type }}" name="{{ name }}" value="{{ value|e }}" size="{{ size }}" />

{% endmacro %}


{% macro user(name="匿名用户", tel = "未知") %}

用户名:{{ name }}  电话号码:{{ tel }}

{% endmacro %}

鸃鶐黳鹘鸲齷鶰黱騯魴鼷鼀鸲騟齂黳鶋鰺龨龤黱鹟黊鼺鶪鹽齂麴黈鸃鹕龌

{% import "macrod.html.twig" as lib %}

<p>{{ lib.input('username') }}</p>

<p>{{ lib.user('yunke',"13812345678") }}</p>

<p>{{ lib.user() }}</p>

鸃鼨鮻鳬龌

<p>    <input type="text" name="username" value="" size="20" />

</p>

<p>用户名:yunke  电话号码:13812345678

</p>

<p>用户名:匿名用户  电话号码:未知

</p>

騎鯠鰃鰀 {% import "macrod.html.twig" as lib %} 鳟鹟黊鴀黳騟黢鼺齹鷗麴齂龒齌齱鼨鯖鸲齷鶰龠鼺鴑鶭鹅鹍鰧鼷鹘鸲鮎鵩鴈 lib 鴆齂鵸鲋麴黈鳟騎鯠騌騋鱒齉騟齂鮻鳬鲯鴫齱鹘鹕齉齂鷟鼃龌

{% from 'macrod.html.twig' import user as id %}

<p>{{ id('yunke',"13812345678") }}</p>

黳龒鷁齉齌鴉黱齂鷟鴫黳 macrod.html.twig 黱鳬鲺鼏鼎騟騯魴驀齃齉鼍鼊齂鼨鼖鲖驮鱆齂鼚鼖龠髗鱪鱦齂鶵鶴鰢鼬鳠騈鶪騟龨龤齷鶰鰢鼬鳟鼺鶭鱹鱚騟

鷟鴫黳鴑鷣騟黢鼺鹽鰬鰩鼷鸊鼿齉鳅鳂齂鲁鱿鸊鼿齉鳅鳂鼨黈齌鱀鷵鶯鲓鴄黳鯛鯙鳛鱴 varargs 黱齂黳騟鼍麴黈鼇鼺for鴯鯾鬓驜驛鯏齂鷟鼃龌

{% macro user(name="匿名用户", tel = "未知") %}

用户名:{{ name }}  电话号码:{{ tel }}  {% for arg in varargs  %}额外参数:{{ arg }}{% endfor %}

{% endmacro %}

鼰鹽鷟鴫鲄鸵鰬鰩鸊鼿鳅鳂齂鼨鼚鼖鸵黖齃鰻齖齉鴍鴌齂髅鰀骫騣 varargs 鼚鸒魾鸵騯魴齉鳅鳂

黳騟騯魴鹽魔騆鲼騄齱黳鳅鳂龒鱹鱚鳢鳠鲤齂鲬鱥黳騟鼍鼇鼺鼬鳅鳂鹽鰢鮢鰧鳬鳢鳠鲤

龨龤鳩齙鰇騃龌

黳鳩麃龨龤鳑齙鹽鷰鷭騀鸝麬鷵齉鰇騃鳩齙駼鱶齂鸃鼚齱驁鶃齉齂鼞鸃鹕鸵髻鴘齉麴黷鷌齂鲋駹鷾麬鷵鷰鷭魔騆鲼騄齂鯍黊鷟鼃龌

1 點黳黩鸙鱹鰑鲟鹍鹙鴯髸鱹鰑鲟齹齂鸵鮕马鸵鹘鸲鮒鱶

2 點鮢鼇鼺鮒髀鲳鶃鲟鴈 - 鴆鹽齂鼚黊黳鯏鹙鱹鰑鲟驀鸻鴀鴙鼏鼎鮒髀

3 點黳鬰麀鲟齹鹍駵鼇鼺鹘鸲鮒鱶齂鸵鮕马鸵鹘鸲

4 點黳鳂鲴齉鴈 , 鴆鹙鮭鮫鱀齉鴈 : 鴆驀鹍鸵鮕马鸵鹘鸲鮒鱶

5 點黳黩鸙駱鸎鮺鹍點鴯髸駱鸎鮺齹鼚黊鴀鴙鼏鼎鮒髀

6 點黳龠鮺齹鹍鼚黊鴀鴙鮒髀

7 點黳鬰麀鲟 | . .. [] 齹鹍鼚黊鴀鴙鮒髀

8 點麯髡鲵鹙魻鳂齉駱鸎鮺齹鹍鼚黊鮒髀

9 點鼇鼺鬋麃鳛鱴鵩齂鯋骭鸻鼺鼃駬駫

10 點鳏駪鰇鰅齂鴒鹘骎鴑

麬麪魔騆鲼騄麪麣默龌

https://twig.symfony.com/doc/1.x/coding_standards.html

駨驺龌

1 點龨龤黩黨鲐麴黈黳鸃鷿黳駫髯鲧鶚駤齉龨龤龌

https://twigfiddle.com

麴黈鵶麯鸊鷹鱶鶯龔 yaml json xml ini 齼鰬鰩鳛鱴齂鹅鹍鴍鴌

2 twig 龨龤龠龝齱鷍駣麴黜黛齉齂黩黨鲐麴黈鵶麯黜黛鷐黼鬴鸊鱡鱟鳛鱴點麯髡鲵點魻鳂點鬰麀鲟點鲳鶃鴯鯾黎黎齂鮤鶹黩黨鲐齿

本书共88小节:

评论 (写第一个评论)