自定义Twig标签;找不到基类__TwigTemplate_

时间:2013-09-05 08:45:58

标签: templates twig

我正在尝试为自定义网站系统创建自定义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";
    }
}

对于Node我借用了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的错误,说它无法加载basetemplate:

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(")") ;

希望得到你们的帮助;任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:0)

所以最后Twig有一种奇怪的方法来编写代码中的错误。

在仔细考虑Twig节点目录中的其他Node示例后,我在编译器方法;的末尾注意到以下缺少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")
            ;
    }
}