{{ 'some text' ~ lipsum(40) ~ 'some more text' }}
{% set lipsum = lipsum(40) %}
最后,您还可以使用一个全局对象,使用一个能够生成lorem ipsum文本的方法:
{{ text.lipsum(40) }}
作为经验法则,使用函数来频繁地使用特性和全局对象。
当你想扩展Twig时,请记住以下几点:
什么?
实施困难吗?
多久一次
什么时候?
宏(macro)
不
频繁
内容生成
全局变量(global)
不
频繁
Helper对象
函数(function)
不
频繁
内容生成
过滤器(filter)
不
频繁
值转换
标签器(tag)
复杂
稀有
DSL语言结构
测试器(test)
不
稀有
布尔判定
操作符(operator)
不
稀有
值转换
全局变量(Globals)
$twig = new Twig_Environment($loader);
$twig->addGlobal('text', new Text());
然后可以在模板中的任何地方使用文本变量:
{{ text.lipsum(40) }}
过滤器(Filters)
创建过滤器器就像将名称与PHP可调用的名称关联一样简单:
// an anonymous function
$filter = new Twig_Filter('rot13', function ($string) {
return str_rot13($string);
});
// or a simple PHP function
$filter = new Twig_Filter('rot13', 'str_rot13');
// or a class static method
$filter = new Twig_Filter('rot13', array('SomeClass', 'rot13Filter'));
$filter = new Twig_Filter('rot13', 'SomeClass::rot13Filter');
// or a class method
$filter = new Twig_Filter('rot13', array($this, 'rot13Filter'));
// the one below needs a runtime implementation (see below for more information)
$filter = new Twig_Filter('rot13', array('SomeClass', 'rot13Filter'));
$filter = new Twig_Filter('rot13', function (Twig_Environment $env, $string) {
// get the current charset for instance
$charset = $env->getCharset();
return str_rot13($string);
}, array('needs_environment' => true));
interface Twig_ExtensionInterface
{
/**
* Returns the token parser instances to add to the existing list.
*
* @return Twig_TokenParserInterface[]
*/
public function getTokenParsers();
/**
* Returns the node visitor instances to add to the existing list.
*
* @return Twig_NodeVisitorInterface[]
*/
public function getNodeVisitors();
/**
* Returns a list of filters to add to the existing list.
*
* @return Twig_Filter[]
*/
public function getFilters();
/**
* Returns a list of tests to add to the existing list.
*
* @return Twig_Test[]
*/
public function getTests();
/**
* Returns a list of functions to add to the existing list.
*
* @return Twig_Function[]
*/
public function getFunctions();
/**
* Returns a list of operators to add to the existing list.
*
* @return array<array> First array of unary operators, second array of binary operators
*/
public function getOperators();
}
$twig = new Twig_Environment($loader);
$twig->addExtension(new Project_Twig_Extension());
Twig核心扩展是扩展工作的很好的例子。
全局变量(Globals)
全局变量可以通过getGlobals()方法在扩展中注册:
class Project_Twig_Extension extends Twig_Extension implements Twig_Extension_GlobalsInterface
{
public function getGlobals()
{
return array(
'text' => new Text(),
);
}
// ...
}
函数(Functions)
函数可以通过getFunctions()方法在扩展中注册:
class Project_Twig_Extension extends Twig_Extension
{
public function getFunctions()
{
return array(
new Twig_Function('lipsum', 'generate_lipsum'),
);
}
// ...
}
class Project_Twig_Extension extends Twig_Extension
{
public function getFilters()
{
return array(
new Twig_Filter('rot13', 'str_rot13'),
);
}
// ...
}
class Project_Twig_Extension extends Twig_Extension
{
public function getTests()
{
return array(
new Twig_Test('even', 'twig_test_even'),
);
}
// ...
}
class Project_Twig_Extension extends Twig_Extension
{
private $rot13Provider;
public function __construct($rot13Provider)
{
$this->rot13Provider = $rot13Provider;
}
public function getFunctions()
{
return array(
new Twig_Function('rot13', array($this, 'rot13')),
);
}
public function rot13($value)
{
return $rot13Provider->rot13($value);
}
}
class RuntimeLoader implements Twig_RuntimeLoaderInterface
{
public function load($class)
{
// implement the logic to create an instance of $class
// and inject its dependencies
// most of the time, it means using your dependency injection container
if ('Project_Twig_RuntimeExtension' === $class) {
return new $class(new Rot13Provider());
} else {
// ...
}
}
}
$twig->addRuntimeLoader(new RuntimeLoader());
class Project_Twig_RuntimeExtension extends Twig_Extension
{
private $rot13Provider;
public function __construct($rot13Provider)
{
$this->rot13Provider = $rot13Provider;
}
public function rot13($value)
{
return $rot13Provider->rot13($value);
}
}
class Project_Twig_Extension extends Twig_Extension
{
public function getFunctions()
{
return array(
new Twig_Function('rot13', array('Project_Twig_RuntimeExtension', 'rot13')),
// or
new Twig_Function('rot13', 'Project_Twig_RuntimeExtension::rot13'),
);
}
}
class MyCoreExtension extends Twig_Extension
{
public function getFilters()
{
return array(
new Twig_Filter('date', array($this, 'dateFilter')),
);
}
public function dateFilter($timestamp, $format = 'F j, Y H:i')
{
// do something different from the built-in date filter
}
}
$twig = new Twig_Environment($loader);
$twig->addExtension(new MyCoreExtension());
在这里,我们已经使用自定义的日期过滤器重载了内置的日期过滤器。
如果您在Twig_Environment上执行相同的操作,请注意它比任何其他注册扩展都优先:
$twig = new Twig_Environment($loader);
$twig->addFilter(new Twig_Filter('date', function ($timestamp, $format = 'F j, Y H:i') {
// do something different from the built-in date filter
}));
// the date filter will come from the above registration, not
// from the registered extension below
$twig->addExtension(new MyCoreExtension());
class Project_Tests_IntegrationTest extends Twig_Test_IntegrationTestCase
{
public function getExtensions()
{
return array(
new Project_Twig_Extension1(),
new Project_Twig_Extension2(),
);
}
public function getFixturesDir()
{
return dirname(__FILE__).'/Fixtures/';
}
}