我正在尝试创建一个自定义的Twig标签,例如:
{% mytag 'foo','bar' %}
Hello world!!
{% endmytag %}
Run Code Online (Sandbox Code Playgroud)
此标记应打印输出my func("Hello world!!", "foo", "bar").
有人可以发布一些示例代码来创建这样的自定义标签吗?可以接受任意数量的参数的人将更加欣赏.
注意:我对创建自定义函数不感兴趣,我需要将标记的主体作为第一个参数传入.
Ala*_*blo 79
在谈论标签之前,您应该了解Twig如何在内部工作.
首先,由于Twig代码可以放在文件,字符串甚至数据库上,Twig会打开并使用Loader读取您的流.大多数已知的加载器都是Twig_Loader_Filesystem从文件中打开twig代码,并Twig_Loader_Array直接从字符串中获取twig代码.
然后,解析此树枝代码以构建解析树,其中包含树枝代码的对象表示.每个对象都被调用Node,因为它们是树的一部分.至于其他语言,嫩枝制成的标记,如{%,{#,function(),"string"...等等枝杈语言结构会读几令牌来建立正确的节点.
然后遍历解析树,并编译成PHP代码.生成的PHP类遵循Twig_Template接口,因此呈现器可以调用doDisplay该类的方法来生成最终结果.
如果启用缓存,则可以查看生成的文件并了解正在进行的操作.
所有内部树枝标签,如{% block %},{% set %}...使用相同的接口,自定义标签的开发,所以如果你需要一些特定的样本,你可以看看枝杈源代码.
但是,你想要的样本无论如何都是一个好的开始,所以让我们开发它.
令牌解析器的目标是解析和验证您的标记参数.例如,{% macro %}标记需要一个名称,如果您给一个字符串,它将崩溃.
当Twig找到一个标记时,它将查看方法TokenParser返回的标记名称的所有已注册类getTag().如果名称匹配,则Twig调用parse()该类的方法.
当parse()被调用时,流指针仍然是标签上的名称标记.因此,我们应该获取所有内联参数,并通过查找BLOCK_END_TYPE标记来完成标记声明.然后,我们subparse标签体(什么是包含在标签内,因为它也可能含有树枝逻辑,如标签和其他东西):该decideMyTagFork方法将每一个新的标签在体内发现时间被称为:与将打破子解析,如果它返回true.请注意,此方法名称不参与接口,这只是Twig内置扩展中使用的标准.
作为参考,Twig令牌可以是以下内容:
EOF_TYPE:流的最后一个标记,表示结束.
TEXT_TYPE:不参与twig语言的文本:例如,在Twig代码中Hello, {{ var }},hello,是一个TEXT_TYPE标记.
BLOCK_START_TYPE:"开始执行语句"令牌, {%
VAR_START_TYPE:"开始获取表达式结果"标记, {{
BLOCK_END_TYPE:"完成执行语句"令牌, %}
VAR_END_TYPE:"完成获取表达式结果"标记, }}
NAME_TYPE:这个标记就像一个没有引号的字符串,就像树枝中的变量名一样, {{ i_am_a_name_type }}
NUMBER_TYPE:此类型的节点包含数字,例如3,-2,4.5 ......
STRING_TYPE:包含用引号或双引号封装的字符串,例如'foo'和"bar"
OPERATOR_TYPE:包含运营商,如+,-但也~,?......你将永远不会左右需要这个令牌枝条已经提供了一个表达式解析器.
INTERPOLATION_START_TYPE,"开始插值"标记(因为Twig> = 1.5),插值是在树枝串内的表达式解释,例如"my string, my #{variable} and 1+1 = #{1+1}".插值的开始是#{.
INTERPOLATION_END_TYPE,"结束插值"标记(因为Twig> = 1.5),}例如在插值打开时未转义为字符串.
MyTagTokenParser.php
<?php
class MyTagTokenParser extends \Twig_TokenParser
{
public function parse(\Twig_Token $token)
{
$lineno = $token->getLine();
$stream = $this->parser->getStream();
// recovers all inline parameters close to your tag name
$params = array_merge(array (), $this->getInlineParams($token));
$continue = true;
while ($continue)
{
// create subtree until the decideMyTagFork() callback returns true
$body = $this->parser->subparse(array ($this, 'decideMyTagFork'));
// I like to put a switch here, in case you need to add middle tags, such
// as: {% mytag %}, {% nextmytag %}, {% endmytag %}.
$tag = $stream->next()->getValue();
switch ($tag)
{
case 'endmytag':
$continue = false;
break;
default:
throw new \Twig_Error_Syntax(sprintf('Unexpected end of template. Twig was looking for the following tags "endmytag" to close the "mytag" block started at line %d)', $lineno), -1);
}
// you want $body at the beginning of your arguments
array_unshift($params, $body);
// if your endmytag can also contains params, you can uncomment this line:
// $params = array_merge($params, $this->getInlineParams($token));
// and comment this one:
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
}
return new MyTagNode(new \Twig_Node($params), $lineno, $this->getTag());
}
/**
* Recovers all tag parameters until we find a BLOCK_END_TYPE ( %} )
*
* @param \Twig_Token $token
* @return array
*/
protected function getInlineParams(\Twig_Token $token)
{
$stream = $this->parser->getStream();
$params = array ();
while (!$stream->test(\Twig_Token::BLOCK_END_TYPE))
{
$params[] = $this->parser->getExpressionParser()->parseExpression();
}
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
return $params;
}
/**
* Callback called at each tag name when subparsing, must return
* true when the expected end tag is reached.
*
* @param \Twig_Token $token
* @return bool
*/
public function decideMyTagFork(\Twig_Token $token)
{
return $token->test(array ('endmytag'));
}
/**
* Your tag name: if the parsed tag match the one you put here, your parse()
* method will be called.
*
* @return string
*/
public function getTag()
{
return 'mytag';
}
}
Run Code Online (Sandbox Code Playgroud)
编译器是用PHP编写标签应该做的代码.在您的示例中,您希望将body作为第一个参数调用,将所有标记参数作为其他参数调用.
当身体进入{% mytag %}并且{% endmytag %}可能很复杂并且还编译自己的代码时,我们应该使用输出缓冲(ob_start()/ ob_get_clean())来填充functionToCall()参数.
MyTagNode.php
<?php
class MyTagNode extends \Twig_Node
{
public function __construct($params, $lineno = 0, $tag = null)
{
parent::__construct(array ('params' => $params), array (), $lineno, $tag);
}
public function compile(\Twig_Compiler $compiler)
{
$count = count($this->getNode('params'));
$compiler
->addDebugInfo($this);
for ($i = 0; ($i < $count); $i++)
{
// argument is not an expression (such as, a \Twig_Node_Textbody)
// we should trick with output buffering to get a valid argument to pass
// to the functionToCall() function.
if (!($this->getNode('params')->getNode($i) instanceof \Twig_Node_Expression))
{
$compiler
->write('ob_start();')
->raw(PHP_EOL);
$compiler
->subcompile($this->getNode('params')->getNode($i));
$compiler
->write('$_mytag[] = ob_get_clean();')
->raw(PHP_EOL);
}
else
{
$compiler
->write('$_mytag[] = ')
->subcompile($this->getNode('params')->getNode($i))
->raw(';')
->raw(PHP_EOL);
}
}
$compiler
->write('call_user_func_array(')
->string('functionToCall')
->raw(', $_mytag);')
->raw(PHP_EOL);
$compiler
->write('unset($_mytag);')
->raw(PHP_EOL);
}
}
Run Code Online (Sandbox Code Playgroud)
创建扩展以暴露您的TokenParser更简洁,因为如果您的扩展需要更多,您将在此处声明所需的一切.
MyTagExtension.php
<?php
class MyTagExtension extends \Twig_Extension
{
public function getTokenParsers()
{
return array (
new MyTagTokenParser(),
);
}
public function getName()
{
return 'mytag';
}
}
Run Code Online (Sandbox Code Playgroud)
mytag.php
<?php
require_once(__DIR__ . '/Twig-1.15.1/lib/Twig/Autoloader.php');
Twig_Autoloader::register();
require_once("MyTagExtension.php");
require_once("MyTagTokenParser.php");
require_once("MyTagNode.php");
$loader = new Twig_Loader_Filesystem(__DIR__);
$twig = new Twig_Environment($loader, array (
// if you want to look at the generated code, uncomment this line
// and create the ./generated directory
// 'cache' => __DIR__ . '/generated',
));
function functionToCall()
{
$params = func_get_args();
$body = array_shift($params);
echo "body = {$body}", PHP_EOL;
echo "params = ", implode(', ', $params), PHP_EOL;
}
$twig->addExtension(new MyTagExtension());
echo $twig->render("mytag.twig", array('firstname' => 'alain'));
Run Code Online (Sandbox Code Playgroud)
mytag.twig
{% mytag 1 "test" (2+3) firstname %}Hello, world!{% endmytag %}
Run Code Online (Sandbox Code Playgroud)
Result
body = Hello, world!
params = 1, test, 5, alain
Run Code Online (Sandbox Code Playgroud)
如果启用缓存,则可以看到生成的结果:
protected function doDisplay(array $context, array $blocks = array())
{
// line 1
ob_start();
echo "Hello, world!";
$_mytag[] = ob_get_clean();
$_mytag[] = 1;
$_mytag[] = "test";
$_mytag[] = (2 + 3);
$_mytag[] = (isset($context["firstname"]) ? $context["firstname"] : null);
call_user_func_array("functionToCall", $_mytag);
unset($_mytag);
}
Run Code Online (Sandbox Code Playgroud)
对于这种特定情况,即使你把其他人{% mytag %}放在一个{% mytag %}(例如{% mytag %}Hello, world!{% mytag %}foo bar{% endmytag %}{% endmytag %})中,这也会有效.但是如果您正在构建这样的标记,那么您可能会使用更复杂的代码,并且$_mytag即使您在解析树中更深入,也会因为它具有相同的名称而覆盖您的变量.
因此,让我们通过使其稳健来完成此示例.
A NodeVisitor就像一个监听器:当编译器读取解析树以生成代码时,它将NodeVisitor在进入或离开节点时输入所有已注册的代码.
所以我们的目标很简单:当我们输入一个类型的节点时MyTagNode,我们将增加一个深度计数器,当我们离开一个节点时,我们将减少这个计数器.在编译器中,我们将能够使用此计数器生成要使用的正确变量名称.
MyTagNodeVisitor.php
<?php
class MyTagNodevisitor implements \Twig_NodeVisitorInterface
{
private $counter = 0;
public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env)
{
if ($node instanceof MyTagNode)
{
$node->setAttribute('counter', $this->counter++);
}
return $node;
}
public function leaveNode(\Twig_NodeInterface $node, \Twig_Environment $env)
{
if ($node instanceof MyTagNode)
{
$node->setAttribute('counter', $this->counter--);
}
return $node;
}
public function getPriority()
{
return 0;
}
}
Run Code Online (Sandbox Code Playgroud)
然后在您的扩展中注册NodeVisitor:
MyTagExtension.php
class MyTagExtension
{
// ...
public function getNodeVisitors()
{
return array (
new MyTagNodeVisitor(),
);
}
}
Run Code Online (Sandbox Code Playgroud)
在编译器中,全部替换"$_mytag"为sprintf("$mytag[%d]", $this->getAttribute('counter')).
MyTagNode.php
// ...
// replace the compile() method by this one:
public function compile(\Twig_Compiler $compiler)
{
$count = count($this->getNode('params'));
$compiler
->addDebugInfo($this);
for ($i = 0; ($i < $count); $i++)
{
// argument is not an expression (such as, a \Twig_Node_Textbody)
// we should trick with output buffering to get a valid argument to pass
// to the functionToCall() function.
if (!($this->getNode('params')->getNode($i) instanceof \Twig_Node_Expression))
{
$compiler
->write('ob_start();')
->raw(PHP_EOL);
$compiler
->subcompile($this->getNode('params')->getNode($i));
$compiler
->write(sprintf('$_mytag[%d][] = ob_get_clean();', $this->getAttribute('counter')))
->raw(PHP_EOL);
}
else
{
$compiler
->write(sprintf('$_mytag[%d][] = ', $this->getAttribute('counter')))
->subcompile($this->getNode('params')->getNode($i))
->raw(';')
->raw(PHP_EOL);
}
}
$compiler
->write('call_user_func_array(')
->string('functionToCall')
->raw(sprintf(', $_mytag[%d]);', $this->getAttribute('counter')))
->raw(PHP_EOL);
$compiler
->write(sprintf('unset($_mytag[%d]);', $this->getAttribute('counter')))
->raw(PHP_EOL);
}
Run Code Online (Sandbox Code Playgroud)
不要忘记在示例中包含NodeVisitor:
mytag.php
// ...
require_once("MyTagNodeVisitor.php");
Run Code Online (Sandbox Code Playgroud)
自定义标签是扩展树枝的一种非常强大的方式,这个介绍为您提供了一个良好的开端.这里没有描述很多功能,但是通过查看twig内置扩展,由我们编写的类扩展的抽象类,以及通过读取由twig文件生成的生成的php代码,您将获得创建任何内容的所有内容.你想要的标签.