番外篇:原生twig模板引擎详解(下集:开发者篇)

龶龲 twig 龪龩龦龤龣龟龞龝龚龙龘龔龑龍龊

龈龚龇龃龀齽齹 twig 龪龩齵齲齱齭齪龪龩齧齣龑齟齜齭龝龚齪 php 龘龔龑齟齜齭齕龟齑齍齋齱齊齆齂 twig 齭齀鼾龍鼺鼸齊鼴齱鼲鼯鼮鼫鼨龈龚鼧鼦鼣

Twig_Environment

鼈鼅齵鼂黿黼 twig 龪龩黻黷齵黳黱黯黬齭黨黤黡黟黜默齭齱黔黒黑黎黋黈 twig 齆齂黈黆黂龪龩黁齭麿麾鼧麽齋麹 twig 黻黷齵麵麴鼅齭麰麯麬麾黼黻黷齵黜麩齲齱鼨麧齭麥麤麡麴 twig 黻黷齭麞麛麚麖齱 $twig 麐麍麉麇麤麅麾龞麿鼯麄黳麀齲齱鼈麐麍麉齭鹾鹽黡黟黜默龊齭鹹鹸鹷鹴鼨鹱鹮麴鹪鹨龙

黆黂鹦龙鹥鹡鹟鹝鹚齵鹗黆黂龪龩齵鼧鼦齭鹖齑麄鹔黻黷黈鹨鹓鹑黁黁齭鹝鹚齵鹗齱鹝鹚齵黆黂鹦

鹐鹌鹨鹋龙麿龈龚黳鹈鹅齕鹂鹁麵鹐鹌齵鹀鸽齭鼯龍鸹龀齽鸷黑鹐鹌鸴鸳齵鸷黑黜默齭鸱鸭鼣

黆黂鹦龙

twig 鸫齪齹鼾鸪鸦鸣黆黂鹦齭齑鸠鸝鸛鸙鸕鸙鸑鸏鸎鼂鸊齭鸦鸣黆黂鹦齑龝龙

Twig_Loader_Filesystem

齱黔鹟麄鹔黻黷黆黂龪龩麄鹔齭齱鷺齑龝龙

$loader = new Twig_Loader_Filesystem($templateDir , $rootPath);

鹪鹨 $templateDir 黼龪龩鷧鷣齵鷡黜鷟鷞齭鷚龑黼鸴黜鷟鷞龞鸴黜黔鷖鷓麴鹪鹨龊齭黨麰麯黼鷑麾鷏鷌鷋齵鹨鹋齭麛鹴鹨鹋黳齵鸴黜鷟鷞鷉麽黼鸴黜黔鷖鷓麴鹪鹨齵齭麰麯麚鹁鷖鷓麴鷌鷆鹪鹨 $rootPath 鹽鷂鸴黜齵鶿鷧鷣齭齑鶼鶹鸠鸫齪齭鶶鶳鼮鼴齱鶯鶭鶩鼯齵鶦鶣鷧鷣齭黨鶡黼 getcwd() 鶚鹨齵鶖齭齑龝鶒

$loader = new Twig_Loader_Filesystem(array($templateDir1, $templateDir2));

麛鹴鼮鶎鶊鶇鶅龪龩麄鹔齭鶂鵿鶅鵿鼾麴鵼鵻齭鼂黿鹷鸭黨麰麯鵺黆鷟鷞龙

$loader->addPath($templateDir3); // 鵫黆鷟鷞

$loader->prependPath($templateDir4); // 鵺黆鷟鷞鵿鹈鵺黆鷟鷞齵鵨鵦齭麯鵥鵣鵠鶇鶅

麚鹁麯龈鹮鵜鷺鵺黆鷟鷞鹴齭黨麰麯鹽鷂麉鵛鵚鵘齭齀鼮鹝鹚龪龩鵖鵿鹝鹚齵鹋黳齭齑龙

$loader->addPath($templateDir, 'admin');

鵒鶹鸠鹽鷂麉鵛鵚鵘鹴齭鸦鸣鵼鵏 main 鵍齭麉鵛鵚鵘鸴鵒黔鵼鵉龝齵鵇鸠鷧鷣鸫齪齹鵅麉齭麰麯齀鵂黆黂龪龩龙

@namespace_name/template_path

齑龙

$twig->render('@admin/index.html', array());

齀鼮麿麉鵛鵚鵘鵏 admin 鵍龝齵鵇鸠鷧鷣鶇鶅鵏 index.html 鵍龪龩齭麉鵛鵚鵘麿鵖鵨鴹龪龩齊鸭鴹鴵鴲龪龩鹴鴱鸠齱

Twig_Loader_Array

齱黔鹟鹨鹋黆黂龪龩齭齲齱齑龝龙

$loader = new Twig_Loader_Array(array(

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

));

鴠麉黼龪龩麉齭鴠鶖黼龪龩鼧鼦齭齀麚麖齱黔鴝鴙鴘鴗齭鷚龑麿鼾鸪鼮鵇鸠龪龩黑鴕麿 php 麄鹔黳齵鴒鹌鷧黳

Twig_Loader_Chain

黆黂鹦鴎齭齱黔鼮鴋麴黆黂鹦麡鴊鵿鼾鴇齭鵒黆黂龪龩鹴齭鶎鶊齋齱齀鸪黆黂鹦齭鼾鴆鶅鵿龪龩鴂鳿鵻鶇鶅齭齲齱鵜鷺齑龝龙

$loader1 = new Twig_Loader_Array(array(
    'base.html' => '{% block content %}{% endblock %}',
));
$loader2 = new Twig_Loader_Array(array(
    'index.html' => '{% extends "base.html" %}{% block content %}Hello {{ name }}{% endblock %}',
    'base.html'  => 'Will never be loaded',
));

$loader = new Twig_Loader_Chain(array($loader1, $loader2));
$twig = new Twig_Environment($loader);

鼨鹱鷑鴋麴黆黂鹦鷌鷋齵鹨鹋齭鳴鳲齵黆黂鹦鸙鸑鼂鸊 Twig_LoaderInterface 齭麴鹨鹝鳫齭鼂黿鹷鸭鳪麰麯鳧鳦鵺黆黆黂鹦齭齲齱鼈鵜鷺龙

addLoader(Twig_LoaderInterface $loader)

鳥鳡鳟鳞鼾鴆黆黂鹦鼂黿鹷鸭齭鳜鷺齋麡黆黂鹦齵鳚鳖

鸏鷂鸽黆黂鹦龙

鷉麽黆黂鹦鳔鳡鼂鸊鼨麧龙

\Twig_LoaderInterface

鼈鼨麧鳎鸠鳍麴鵜鷺龙

public function getSource($name);

鳇鵿龪龩鼧鼦

public function getCacheKey($name);

鳇鵿鸫齪鳁鸷黑黻黷齲齱齵龪龩鸷黑鴠齭鳥麿鸷黑黻黷黳鲿鼂齵鸷黑 id 鳪麰麯鲽鴊鵉鲺鼧鼦鶒麿麄鹔黻黷黆黂鹦黳齭鼈鵜鷺鲶鲵龪龩麄鹔齵鸴黜鷟鷞

public function isFresh($name, $time);

鲲鷂龪龩齵鸷黑黼鲯鳪鸠鲭齭鷖鷓麴鹪鹨鲪鼈黼鸷黑齵龪龩齵鲩鲧鹴鵘齭黨鶡黼鲣鲟鲝鸭齵龪龩齵鲩鲧鹴鵘齭麿鼧麽鼮齊鹗龪龩麄鹔鲜鶭鹴鵘黜鹖齭黒鲚鵒鹗龪龩鸠麐鹷鹴齭鲣鲟鲝鸭齵鸷黑鴂鲗鲭

鵼齹鲕黆黂鹦鴎鲓黆鲑鲍齵鶦鶣齭鲌鲊黆黂鹦黨鼂鸊鼨麧龙

Twig_ExistsLoaderInterface

鼈鼨麧鳎鸠鼾麴鵜鷺龙

public function exists($name);

齱黔鲲鷂龪龩黼鲯黑麿

鸷黑黜默龙

鸷黑黜默齱黔鲉黑黈鲈鲵鲣黻黷鲟鲝鸭齵龪龩麄鹔齭鲅鲁鷂鸷黑 id 龞黜黔麄鹔黻黷鸷黑黜默鱽鱺齭鼈 id 鶡黼麄鹔鷟鷞齭鹀麄鹔麉龊黈鲶鲵鸷黑鱷鱶鹴鵘龞鼈鹴鵘齪黆黂鹦鲲鱵鸷黑黼鲯鹈鹅鲗鲭龊

麿龈龚黳鹈鹅龟鱲齹鸫齪鳁黡黟黜默齵鹐鹌鹨鹋黳 cache 鹐鹌齵鹀鸽齭齑龝龙

麰鹐鶖鸠 鲉黑鲟鲝鸭龪龩齵鷧鷣齭鷚龑鵼 false 麯鱯齱鲟鲝鸷黑齭齀黼鸦鸣鶖齭黨麰麯黼鸷黑黜默龞鼨麧龙 Twig_CacheInterface 齵鼂黿龊

鼂鱫龈麿鼧麽鹝鴵鼈鹐鹌鸫齪齵黼鱪鼾鱨鶖齭鱧鱣鲣鱟鹷鵼鸷黑黜默齭黨鶡黼鼨麧龙 Twig_CacheInterface 齵鼂黿齭

麿鹐鹌黼鷧鷣鷟鷞齵鹴鱜齭鸦鸣齲齱麯龝鸷黑黜默龙

\Twig_Cache_Filesystem

齑鶼鱯齱鸷黑齭鶶鶳鸦鸣齲齱麯龝鸷黑黜默龙

\Twig_Cache_Null

齑鶼鸙鸑鸏鷂鸽鸷黑齭齑鲉黑鵿鹨鹓鹑鹴齭鶶鶳鸙鸑鼂鸊鼨麧齭鲅鼮鸏鷂鸽齵鸷黑黜默鶣鵼鹐鹌鶖鳴鱗黡黟麐麍鼣鸏鷂鸽鸷黑黜默齑龝龙

鼂鸊鼨麧龙 \Twig_CacheInterface

鵉鵜鷺鹀鸽齑龝龙

generateKey($name, $className);

鲶鲵鸷黑 id 齭鼈鼨麧齵鵉鲺鵜鷺麚鹁鼈 id 麇黆黂齊鱔鱗鲟鲝鸭齵龪龩鼣鷖鼾麴鹪鹨黼龶龪龩齵麄鹔麉齭鷖鷓麴鵼鲟鲝鸭齵龪龩 php 鼅麉

write($key, $content);

黒黑鲟鲝鸭齵龪龩鵿鸷黑齭鷖鼾麴鹪鹨鵼鸷黑 id generateKey 鵜鷺鲶鲵齵鶖龊齭鷖鷓麴鵼 php 麥鱐

load($key);

黆黂鲟鲝鸭齵龪龩麄鹔鼂鱫龈黼黆黂鼾麴麰鶯鶭齵 php 麄鹔齭麞麛齲齱 include_once 齭鲅鹝黼齜鲈鼧鼦鵛鱏鱋

getTimestamp($key);

鲶鲵鲣鲟鲝鸭齵龪龩麄鹔齵黒黑鹴鵘齭麯齪黆黂鹦齱麇齊龶龪龩黜鹖鲲鱵黼鲯鲗鲭

twig 齆齂 Extensions

齆齂齱黔鳁 twig 鵺黆鱉鱇鱃齭麚鹁黡黟黜默齵 addExtension 鵜鷺鵺黆鴂麰齭齑龙

$twig->addExtension(new Twig_Extension_Sandbox());

twig 鸏鱂齹鼾鸪齆齂齭龀齽齑龝鼣

鱀黱齆齂 Twig_Extension_Core:

鷂鸽 Twig 齵鱀黱鱇鱃齭齑鰻鰹黈鹁鰸鹦黈鶚鹨黈鴘鴗黁齭鸏鰵黆黂鼈齆齂齭鹝鳔鰲鰵鵺黆

鱟鸽齆齂 Twig_Extension_Escaper:

鼈齆齂鸫齪龪龩黳鷉麽麐麍鸏鰵鱟鸽黈 autoescape 鰻鰹黜龪龩黳鼾鰱鰯麽鼧鼦麐麍鱟鸽黈鷂鸽鹁鰸鹦 raw 齭麿鼂黿鹷黡黟黜默鹴鸏鰵黆黂鼈齆齂齭鹝鳔鰲鰵鵺黆鶒麿鹈鹅鹹鹸鹷黡黟黜默鸭齭齑鶼鰭鲓鲧麚鹁鹐鹌鹨鹋鳴鱗齵鷉鰯鱟鸽鰫鰨齭麰麯麿齆齂鹹鹸鹷鵨齋齱麯龝麥鱐龙

$escaper = new Twig_Extension_Escaper('html');

$twig->addExtension($escaper);

鰧鰣齆齂 Twig_Extension_Sandbox:

鸫齪鰧鰣龪鰢齭齱黔鶯鶭鹝麰鰟齵龪龩麥鱐齭鼮鵉鰛鰙鶯鶭齭麅黿齑龝龙

$tags = array('if');
$filters = array('upper');
$methods = array(
    'Article' => array('getTitle', 'getBody'),
);
$properties = array(
    'Article' => array('title', 'body'),
);
$functions = array('range');
$policy = new Twig_Sandbox_SecurityPolicy($tags, $filters, $methods, $properties, $functions);
$sandbox = new Twig_Extension_Sandbox($policy);
$twig->addExtension($sandbox);

齑鰒鵇鸱齭鰧鰣齆齂鸙鸑鼾麴鰫鰨黜默齭 twig 鸏鱂齹鼾麴鰫鰨黜默龙 Twig_Sandbox_SecurityPolicy 齭麾黎黋鼾麴鰏麉鴝齭麉鴝麯麩齵鼧鼦鱧鹝鲣鰎鰌齭麯龈麅黿黎黋齹齑龝鼧鼦龙

鰎鰌齵龪龩鰻鰹齊鹁鰸鹦黈麰鰉鰇齵黜默鵜鷺齊鰃鰂黈麰齲齱齵鶚鹨齭鯿鰢齑龝龙

鰻鰹黈鹁鰸鹦黈鶚鹨齵鶖鹾黼黜鲪鴙鯽鷌鷋齵鹨鹋齭鯼鴒鱔鯸鯶

鰎鰌齵鵜鷺龞 $methods 麐麍龊黼鼾麴鹨鹋齭鴠麉鵼鼅麉龞鲪鼈黼鷉鳫鷂鼅麉齭鲣鶂鼨齱黔 $obj instanceof $class 鴘鴗龊齭鴠鶖鵼鵜鷺麉鷚鵜鷺麉鷌鷋齵鹨鹋齭鵜鷺麉鯼鴒鱔鹝鯸鯶

鰎鰌齵鰃鰂龞 $properties 麐麍龊齊鰎鰌齵鵜鷺鼾鵂齭鳥鯼鴒鱔鯸鯶

鸏鷂鸽鰫鰨黜默鳎鸙鸑鼂鸊鼨麧鴂麰齭麰鹪鯱麯龈鰫鰨黜默齵鼂鸊齭鹖鯯鯫鴝鹝鴋龀齽鼣

麯龈麥鱐鳪鶹鸠鲿鯩鯨齱鰧鰣龪鰢齭鳪鸙鸑麿龪龩黳鼮鹝鼺鷉齵鼧鼦鴕麿鰧鰣鰻鰹鼧鯥鶭龙

{% sandbox %}
    {% include 'user.html' %}
{% endsandbox %}

齑鶼鸙鸑黜鷉麽龪龩鼧鼦鶯鶭鰧鰣龪鰢齭鶶鶳麿鰧鰣齆齂齵鷖鷓麴鹪鹨鳴鱗 true 鴂麰龞麾鸦鸣鵼 false 龊龙

$sandbox = new Twig_Extension_Sandbox($policy,true);

齑鶼鰧鰣鼧麥鱐鰉鰇齹鹝鰎鰌齵鼧鼦齭鶶鶳鼮鯡鯠鯝麖齭鼈齆齂鶹鸠鲣鸦鸣黆黂齭齑鸙齲齱鸙鸑鰲鰵黆黂

鵖鯜鹦齆齂 Twig_Extension_Profiler:

鵖鯜鹦齆齂齱黔鸫齪龪龩鶯鶭齵鹴鵘黈鼧黑齱麍黁黁鰟鯚齭鶹鸠鸦鸣黆黂齭鲪鼈鸹齱黔龘龔鯙鰱齭麅黿龙

$profile = new Twig_Profiler_Profile();

$twig->addExtension(new Twig_Extension_Profiler($profile));

鯖鶭龪龩鯕鸭鶇鯒鵖鯜鲽鶼龙

$dumper = new Twig_Profiler_Dumper_Text();

echo $dumper->dump($profile);

鯏鹷鹦齆齂 Twig_Extension_Optimizer:

齱黔麿鲟鲝龪龩鵨鯏鹷鯋龃鯇齭鸦鸣黆黂鲅龘鯨鷉麽鯏鹷齭龘龔龑麰齱鯏鹷鹐鹌鯅鯁鵉鶭鵼齭麅黿齑龝龙

$optimizer = new Twig_Extension_Optimizer(Twig_NodeVisitor_Optimizer::OPTIMIZE_FOR);

$twig->addExtension($optimizer);

齀鱣鮴鮰龇鱉鵺黆齭鮬鱔鸦鸣鵺黆齵鹐鹌齭鯅鯁鹐鹌鸠齑龝鼅鮩龙

Twig_NodeVisitor_Optimizer::OPTIMIZE_ALL 龙龘鯨鷉麽鯏鹷齭鸦鸣鶖

Twig_NodeVisitor_Optimizer::OPTIMIZE_NONE 龙鸳鮧鯏鹷齭齀鮤鮡龪龩鲟鲝鹴鵘齭鳥黆鯼鶯鶭鹴鵘齊鼧黑鮞鮝 Twig_NodeVisitor_Optimizer::OPTIMIZE_FOR 龙鯏鹷 for 鰻鰹齭鮚麰鱃鮗鮕麐麍鮓黡

Twig_NodeVisitor_Optimizer::OPTIMIZE_RAW_FILTER 龙鮚麰鱃鮐鮎鹝鳔鸑齵 raw 鹁鰸鹦

Twig_NodeVisitor_Optimizer::OPTIMIZE_VAR_ACCESS 龙鮚麍鯫鹷龪龩黳麐麍齵鰉鰇齊鮌鱷

鸏鷂鸽齆齂龙

twig 鸫齪鮈鯼齵齆齂鱃鮄齭麰麯鵺黆鰻鰹黈鹁鰸鹦黈鶚鹨黈鷉鰯麐麍黈鮀鶣鱏黈鴘鴗齭魽魺鱃齆齂魷鷺鵖鯜鹦鼯魳齭鮎齆齂鰻鰹魱魯魮麩齭鵉鲺鱧鴱鯫鴝齭麇鯒鼾鯒鼣

鵺黆鷉鰯麐麍龙

鷉鰯麐麍鮎麿鷉麽龪龩黳龞魭魩魨龊鸠鲭麩齭齊鵉鲺魤麚龪龩麐麍鶹魣鶳魡鵅齭鵺黆鼾麴鷉鰯麐麍齑龝龙

$twig = new Twig_Environment($loader);

$twig->addGlobal('text', new Text());

鷖鼾麴鹪鹨黼麿龪龩黳齵鷉鰯麐麍麉魞齭鷖鷓麴鵼鵉鶖齭麰麯黼魛鳞鼅鮩齵鶖

鷉鰯麐麍鳎鱃麿鲟鲝鷚魘魕龪龩鯕鵨鲣鵺黆齭鲯魒鳎鱃鲓鱉

鵺黆鹁鰸鹦龙

麅黿齑龝龙

$filter = new Twig_SimpleFilter('rot13', function ($string) {

return str_rot13($string);

});

$twig->addFilter($filter);

鵉黳 Twig_SimpleFilter 鷌鷆鶚鹨齵鷖鼾麴鹪鹨鵼麿龪龩黳鼮鸑齱鵿齵鹁鰸鹦麉魞齭鷖鷓麴鵼鼾麴 php 鲵齋 $callable 齭麾麰麯黼齑龝魐鰢龙

魏麉鶚鹨黈鹈鷂鸽齵 php 鷚齱魋鸏鷂鸽齵鶚鹨麉黈鼅魊魉鵜鷺龙

$filter = new Twig_SimpleFilter('rot13', array('SomeClass', 'rot13Filter'));

$filter = new Twig_SimpleFilter('rot13', 'SomeClass::rot13Filter');

黜默鵜鷺龙

$filter = new Twig_SimpleFilter('rot13', array($this, 'rot13Filter'));

麿齋齱鹁鰸鹦鹴鴵魇鱏魆鵏 | 鵍魀鬼齵鶖鶣鵼鷖鼾麴鹪鹨鳴鱗齭鬻鹁鰸鹦鱂鬺魩魆鹪鹨齭魒鬶鵦齵鶖鬴鳚鳖鳴鳲鳁黜鲪鹪鹨龞鲅鬲鴊鷋鹨鹋鳴鳲鳁鷖鷓麴鹪鹨龊齭

Twig_SimpleFilter 鷌鷆鶚鹨鳪黑麿鷖鳍麴鹪鹨齭麯鹽鷂鬯黆鹐鹌齭黼鼾麴鹨鹋齭鸦鸣鶖鵼龙

array(
            'needs_environment' => false,
            'needs_context' => false,
            'is_variadic' => false,
            'is_safe' => null,
            'is_safe_callback' => null,
            'pre_escape' => null,
            'preserves_safety' => null,
            'node_class' => 'Twig_Node_Expression_Filter',
            'deprecated' => false,
            'alternative' => null,
        )

鹀鸽齑龝龙

needs_environment

齑鶼鹁鰸鹦鸙鸑鰉鰇黡黟麐麍齭齧黋鵼 true 齭鶶鶳鷖鼾麴鹪鹨鼮鳴鱗黡黟麐麍齭鵉鲺鶖鶎鶊鸭鬫

needs_context

齑鶼鸙鸑鰉鰇龈龝麄齭齧黋鵼 true 齭鶶鶳鷖鼾麴鹪鹨鼮鳴鱗龈龝麄麐麍齭鵉鲺鶖鶎鶊鸭鬫齭齑鶼鹚鹴鳪鸙鸑黡黟麐麍齭鶶鶳龈龝麄鹪鹨麿黡黟麐麍鹪鹨鸭鵦

is_safe

鵉鶖黼鼾麴鱟鸽鰫鰨鷌鷋齵鹨鹋鷚鵼 NULL 齭齑鶼龘鯨齹鸏鰵鱟鸽齭鶶鶳齧黋鼈鶖鼮鬧鬤鲶鲵鶖鲣鹽鷂齵鰫鰨鬠鶊鱟鸽

pre_escape

鹽鷂鴵魇鱏鵨齵麐麍麿鳴鱗鹁鰸鹦鵨鸙鸑鵠鲜鶭鱟鸽齭鵛鱏鱋鶖齭鱟鸽鰫鰨鯕鼾齭鹝鱃齲齱鹨鹋鹽鷂鴋麴齭鳟鳞黼鴵魇鱏鵨齵麐麍齭齑鶼黼鵛鵦鶖鼮鳜鲭齭鹁鰸鹦鬺魩魆黳齵鬟麩鹪鹨鹝鬜黼麐麍鳪黼鵛鵦鶖鱧鹝鱣鱟鸽

is_variadic

齑鶼鹁鰸鹦麰鼨鹱魛鳞鴋麴鹪鹨鹴齭鼮鵉齧黋鵼 true 齭鶶鶳 twig 鼮鬟麩鹪鹨鴊鷋鹨鹋鸭鶣鵼鬘鸭鼾麴鹪鹨鳴鱗

deprecated

鰻鬗齀麴鹁鰸鹦鹈鹅鲣鯨齱齹

alternative

鵒鹁鰸鹦鲣鰻鬗鵼鬓齱鹴齭麰齱鼈鹐鹌鹽鷂鼾麴麥鬐鬏

node_class

鸦鸣鶖龙 Twig_Node_Expression_ Filter/ Function/ Test 龊齭鹽鷂鼾麴鼅齭齱黔鬌鴲鯋龃齵鬈鬅鼂鸊齭鹖齑鬂鬀鹨鲲鱵齭鱀黱鲅鶹鸠鼴齱鶚鹨齭鱽黼鳁齹鼾麴鼅鼂鸊齭麿龪龩鲟鲝鸭齵 php 麥鱐黳鶂鼨 %2 麇鲲鱵

preserves_safety

麿黻黷黳髿鸱鼂髽鰂齲齱齭髹鵜黨髿鳁鯠龟鱲齭麰髵鰨

is_safe_callback

鷂鸽鼾麴鲵齋齭齱黔鰵魉鲶鲵 is_safe 鹐鹌齵鼧鼦齭鼈鲵齋鼨鹱 Twig_Node 鼅鮩齵鹪鹨

鰵魉麉魞鹁鰸鹦龙

鹽鹁鰸鹦齵麉魞鹝黼髳鷂齵齭 twig 麰麯鼂鸊鹝鹚鹁鰸鹦麉鼴齱鹚鼾麴鹁鰸鹦鶚鹨齭鳎鸙鸑麿鵺黆鹁鰸鹦鹴鰵魉麽鵖齱鵏 * 鵍魆麥鬐齭齑龝麅黿龙

$filter = new Twig_SimpleFilter('*_path_*', function ($name, $suffix, $arguments) {

    // ...

});

麛鹴髯齧龪龩麥鱐齑龙 'foo'|a_path_b() 鶶鶳鳴鳲鹪鹨鹴鼮黼龙 ('a', 'b', 'foo') 齭鰵魉麽鵖鶎鶊鳴鱗齭齑鶼鸠黡黟麐麍齊龈龝麄鹪鹨齭鶶鶳鲺鷏麿黡黟麐麍齊龈龝麄鹪鹨鸭鵦鳴鱗鼣

鵺黆鶚鹨龙

鵺黆鶚鹨齵鵜鷺齊鹁鰸鹦髬麹鸴髫齭麅黿齑龝龙

$twig = new Twig_Environment($loader);

$function = new Twig_SimpleFunction ($name, $callable, $options);

$twig->addFunction($function);

鮎齹鹐鹌 pre_escape preserves_safety 麩齭鶚鹨齊鹁鰸鹦鸠鸴鹚齵鱇鱃鼂鸊齭鶚鹨黨髩髧鰵魉鶚鹨麉

鰵魉鷂鸽髿鷂鸽齵鹁鰸鹦鷚鶚鹨龙

鵒龪龩黳齲齱鼾麴鶹鸠鷂鸽鹁齵鹁鰸鹦鷚鶚鹨鹴齭鸦鸣鯡鯠鯝麖齭鳥麿麛鯕鵨 twig 鱣髣鴗鰉鰇鰵魉鷂鸽齭齑鶼鶹鸠鷂鸽鯥鯡鯠齭鲯魒齲齱鰵魉鷂鸽鲽鶼齭麅黿齑龝龙

$twig->registerUndefinedFunctionCallback(function ($name) {

    if (function_exists($name)) {

        return new Twig_SimpleFunction($name, $name);

    }

    return false;

});

龈髟鼮齲鵇鸠 php 鶚鹨髜髙鳁龪龩鶒鹁鰸鹦鰵魉鷂鸽高齲齱龙 registerUndefinedFilterCallback()

齑鶼鶎髗鹝鰭鷂鸽齭鳔鳡鲶鲵 false

鷑黔鼈鱇鱃黼麿鲟鲝鯙鰱鲜鶭齵齭鵇麯鶹鸠鰂鱃體鲗

鳟鳞齀齊鰵魉麉魞鹁鰸鹦齊鶚鹨黼鹝鼾鵂齵髐髏

鵺黆鴘鴗龙

齀鬶齵鴘鴗鹝黼鹽鴝鴙鴘鴗齭黨鹝黼麿齋鴗麥鱐齭鱽黼鹽龪龩黳齵齑龝鯖齱龙

{% if my_value is odd %}

鵺黆鼾麴鴘鴗齊鵺黆鹁鰸鹦黈鶚鹨黼髬麹鼅髫齵齭麅黿齑龝龙

$test = new Twig_SimpleTest('red', function ($color) {
    if ($color == 'red') {
        return true;
    }
    return false;
});
$twig->addTest($test);

髗鸭麿龪龩黳鶡麰麯齀鵂齱齹龙

{% if my_value is red %}
是红色
{% else %}
不是红色
{% endif %}

鳟鳞龙鴘鴗齱黔髈鹔鲲鱵齭鴘鴗鶚鹨鲪鼈髇黼鲶鲵髅髄鶖

鵺黆鰻鰹龙

黼鹽鸏鷂鸽鼅髫鵏 if 鵍黈鵏 for 鵍黁鲽鷌齭鹖鯯魯魮齭鸙鸑髃魇 twig 鼧麽龶鴲齭髁骾鲟鲝龶鴲髃骻齭齲齱鴱骸鸱齭鼧鼦鯼鯼骶鯠鼯龍骲骮齭鵇麯髵鰨鹝齕齭鳥齀黼鼾鹔骬鰵骨黱齵骧骦齭麿骢骞齣骝骙骘骕鹾鸠龘齧骘骒骑骐齕龟鲟鲝龶鴲齭鳥鱃验鼂髽鰂鼨骋鲟鲝龶鴲鼂骉齵骙鱣鴱鮕齭齑鶼鰒骅骄骀鱗驾驺齭鶶鶳 twig 黼鼾麴鬲麖鴊驷齵鼂骉鹌鷧齭齀黨鲕鰒鯼鮰驶鰏 php C java 黁黼齑齍麐鷋骙鹦鱐齵齭齀鬶鳁鯠鼾鸪龦鮴龙

twig 龪龩魘魕鵖驴麴驱驭龙

黆黂 loader: 鼮龪龩鹗麥鱐黆黂鵿骐鳖

驪鷺鵖鯜 lexer 龙鼮龪龩鹗麥鱐龟鯜鵼鸠齱齵驩鼯鴝鴙龞 token 驥龊

魷鷺鵖鯜 parser 龙鼮 token 驥鱟鹷鵼鯋龃鯇龞驤默魷鷺鯇 AST:the Abstract Syntax Tree

鲟鲝 compiler 龙鼮魷鷺鯇鱟鹷鵼 php 麥鱐

鲓鴋高鸱龙 https://twig.symfony.com/doc/1.x/internals.html

鵺黆齆齂龙

麯龈鵺黆鷉鰯麐麍黈鹁鰸鹦黈鶚鹨黈鴘鴗黈鰻鰹黁齆齂鶭鵼鱧黼鴝驣鲜鶭齵齭鼾麴鹌鷧驠驠鸙鸑鵺黆鴱鴋驜驛齭麰麯驙齀鸪鮀鶣龚黳鵿鼾鴇魐鷋鼾麴麰龇齱齵鼅齭齀麴鼅鶡黼齆齂齭齆齂黜默齱黔鲓黆鵜鵥齵黷鼾鲜鶭龈鹂鮀鶣齭驘魭鱇鱃鸴鸳齵鹋鹔鼣

齆齂鼅鸙鸑鼂鸊麯龝鼨麧龙

Twig_ExtensionInterface

麚麖鹝鸙鸑鶂鼨鼂鸊齭鳧驕驤默鼅 Twig_Extension 鴂麰

鬶鵦齵鵜鷺鱧黜鲪鵨麄龀齽齵鵺黆鵜鷺齭鬈鬅鼂鸊麰鹪驑鱀黱齆齂鼅龙

Twig_Extension_Core 龞驍黔麄鹔 Twig/Extension/Core.php 黳龊

鹟鼈鼅黨麰麯鯒鯠鱀黱鸫齪齹鱪鸪鹁鰸鹦黈鶚鹨黁

齑鶼鸙鸑鲩鲧龞龇黂龊鹈鹅黑麿齵鷉鰯麐麍黈鹁鰸鹦黈鶚鹨黈鴘鴗黈鰻鰹黁鳎鸙鸑龇鱉鵺黆鲺鷏鴂麰齭齑鶼黼鶂鼨麿黡黟麐麍龈鵺黆齭鶶鶳麾齵鯏鵠驉鼮髬黔魛齍齆齂齭鴂鵥齆齂鲣鸭鵺黆

鸏鷂鸽鷂驅鱏龙

麰麯麚鹁驂驪鷺鵖鯜鹦鳴鳲鹐鹌鹨鹋鲧麐鸦鸣齵鷂驅鱏齭麅黿齑龝龙

$twig = new Twig_Environment($loader);

$lexer = new Twig_Lexer($twig, array(

            'tag_comment' => array('{#', '#}'),

            'tag_block' => array('{%', '%}'),

            'tag_variable' => array('{{', '}}'),

            'whitespace_trim' => '-',

            'interpolation' => array('#{', '}'),

        ));

$twig->setLexer($lexer);

鳥鹝鲌鲊齀鵂齱齭齀鼮齲龪龩鹝麚齱齭鳥鸠鹴鱜黼鴱鸠鳔鸑齵鼣

黜默齵鰵魉鰃鰂龙

鵒齲齱 article.title 鵜鰢鰉鰇麐麍鹴齭鱣騿鶇 article 黼鲯黑麿 title 鰃鰂齭骢鷏黨麰麯鷂鸽 php 齵騽騺鵜鷺齭麯鰵魉鲶鲵鲽鶼齭齑龝龙

class Article
{
    public function __get($name)
    {
        if ('title' == $name) {
            return 'The title';
        }
        // throw some kind of error
    }
    public function __isset($name)
    {
        if ('title' == $name) {
            return true;
        }
        return false;
    }
}

騶鲚龪龩魷鷺騳騰龙

黜鷖鳍鵜鷚齋鴗鯙鰱齵龪龩麰麯鲜鶭魷鷺騶鲚齭麯鵥鳎鸠麚鹁鸭鯥鱃黒黑齭麅黿齑龝龙

try {
    $twig->parse($twig->tokenize(new Twig_Source($template)));
//模板有效
} catch (Twig_Error_Syntax $e) {
    // 模板有语法错误
}

齲齱鹨鹓鹑鲉黑龪龩龙

麿髹騯鸠鼾麴髟騮齭高鸱龙

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

騭騬麬驶龙

1 黈龘龔龑鸙鸑鳟鳞鲣鲟鲝鸭齵龪龩黒騪騨鳎齊鹗龪龩鸠鸳齭鱽騧齋齱骐鳖鳜鸳齭齀麰鱃鱣鱂麇騦騥齭鹖齑齋齱骐鳖鵺黆齹鱉齵齆齂齭齋鴗鹐鹌黨黼驘龘齵齭鳥騣鯠鲽鶼髿麐齭齀騠麰鱃鶡黼鼈鰇騟鮴鮰齭鲅鬲骐鳖 bug

2 DSL 齭黼 Domain Specific Languages 齵騜鱔齭麰麯騙鲝鵼鵏騗騔鸝鷂魷鱺鵍齭齱黔鳁騒騐骕騎齵骨騊麇騇鹂骕騎骦騆齭麰麯骝黼骐鳖騊齊骕騎鸙鸕鯁鷂骨騊齵騅騁齭鲕鹝駾駽騺齵骨麚鹁麾騇鹂骕騎駺駷齭鸏髗魷鱺鲪鼈骝黼 DSL 齧齣齵騠鮰鷧鰻齹齭齣骝骙駳駾鸏髗魷鱺髗鸭鸏鰵駲龲骐鳖駰鶯鶭齭 twig 鸫齪齵魷鷺鶡黼鼾鱨 DSL 齵鼂鸊齭麾鲕齧齣駭麚鹁齀鱨魷鷺駩鸏鸎齵骧骦齭髗鸭 twig 鼮鵉騙鲝鷋 php 骐鳖鶒 SQL 黨黼鼾鱨 DSL 齭麾鴝驣鸫齪魷鷺齭髗鸭鹨鹓鹑駥鹔龟鱲鶯鶭麾鶒鯩魒麤駣鰢黨黼鶒髇齵麇麬 DSL 黼鼾鱨魷鱺齭鸫齪鸴鹖黔駠駜駽騺髬驉驤默齵騇鹂鱃鮄齭鱽黜骨鼅鱽鱺駙黼鸏髗黈駘駗黈鯫鴝齵齭麾髁骾魷鷺鷂鸽黈鲟鲝龶鴲黁鼣

本书共88小节:

评论 (写第一个评论)