0

我正在尝试为我的自定义网站系统创建一个自定义 Twig 标签( http://twig.sensiolabs.org/doc/advanced.html#tags )。

通过在模板中使用以下标签:

{% entity '\\Testimonial\\Entity\\Testimonial' with { 'limit' : 2, 'column' : 'created' } %}

自定义标签将从创建的列排序的数据库中加载最后 2 个推荐。好的,到目前为止一切顺利。

对于 TokenParser,我使用了 include 标签中的代码:

class Entity extends Twig_TokenParser
{
    public function parse(Twig_Token $token)
    {

        $object         = $this -> parser -> getCurrentToken() -> getValue();

        if( empty($object))
        {
            return;
        }

        $expr           = $this->parser->getExpressionParser()->parseExpression();

            $variables      = $this->parseArguments();

        return new EntityNode( $object, $expr, $variables, $token->getLine(), $this->getTag() );
    }
    protected function parseArguments()
    {
        $stream = $this->parser->getStream();

        $variables = null;
        if ($stream->test(Twig_Token::NAME_TYPE, 'with')) {
            $stream->next();

            $variables = $this->parser->getExpressionParser()->parseExpression();
        }

        $stream->expect(Twig_Token::BLOCK_END_TYPE);

        return $variables;
    }
    public function getTag()
    {
        return "entity";
    }
}

对于我从 include 借来的节点和我发现的其他一些示例,结果如下:

class EntityNode extends Twig_Node implements Twig_NodeOutputInterface
{
    public function __construct( 
                    $object,
                    Twig_Node_Expression $expr,
                    Twig_Node_Expression $variables = null, 
                    $lineno, 
                    $tag = null )
    {
        parent::__construct(array('expr' => $expr, 'variables' => $variables), array("object" => $object), $lineno, $tag);
    }
    public function compile(Twig_Compiler $compiler)
    {
        $compiler->addDebugInfo($this);

        $obj        = $this->getAttribute('object');

        if( !is_callable( $obj ) || !class_exists( $obj ))
        {
            // error not callable
        }

        $entities   = forward_static_call( array( $obj , "TemplateEntity" ) , $this -> getNode( "variables" ));
        $subtemplate    = forward_static_call( array( $obj , "TemplateEntityTemplatePath" ));

        $template   = new Twig_Node_Expression_Constant( $subtemplate , $this -> getLine() );
        #return;
        $compiler
            -> write("\$this->env->loadTemplate(")
            -> subcompile($template)
            -> raw(")")
            ;
    }
}

结果是来自 Twig 的错误,说它无法加载基本模板:

Parse error: syntax error, unexpected 'echo' (T_ECHO) in /domains/<domain>/lib/framework/vendors/twig/lib/Twig/Environment.php(328) : eval()'d code on line 370
0 - Exception occurred

Exception -- Autoloader could not find base class __TwigTemplate_c56f3794ae5aed2d0cc25529303a838625ded364d30febb96cd025a5d7622121

我知道在 Twig_Node 之前一切正常,问题在于 Twig 如何解析行$compiler -> write("\$this->env->loadTemplate(") -> subcompile($template) -> raw(")") ;

希望能得到大家的帮助;任何帮助表示赞赏!

4

1 回答 1

0

所以最后 Twig 有一种奇怪的方式在代码中制定错误。

在仔细考虑了 Twig 节点目录中的其他节点示例后,我注意到;编译器方法末尾缺少以下内容raw

class EntityNode extends Twig_Node implements Twig_NodeOutputInterface
{
    public function __construct( 
                    $object,
                    Twig_Node_Expression $expr,
                    Twig_Node_Expression $variables = null, 
                    $lineno, 
                    $tag = null )
    {
        parent::__construct(array('expr' => $expr, 'variables' => $variables), array("object" => $object), $lineno, $tag);
    }
    public function compile(Twig_Compiler $compiler)
    {
        $compiler->addDebugInfo($this);

        $obj        = $this->getAttribute('object');

        if( !is_callable( $obj ) || !class_exists( $obj ))
        {
            // error not callable
        }

        $entities   = forward_static_call( array( $obj , "TemplateEntity" ) , $this -> getNode( "variables" ));
        $subtemplate    = forward_static_call( array( $obj , "TemplateEntityTemplatePath" ));

        $template   = new Twig_Node_Expression_Constant( $subtemplate , $this -> getLine() );
        #return;
        $compiler
            -> write("\$this->env->loadTemplate(")
            -> subcompile($template)
            -> raw(");\n")
            ;
    }
}
于 2013-09-08T19:49:27.187 回答