class Yii_Node_WidgetBlock extends Twig_Node
{
public function __construct($attrs, Twig_NodeInterface $body, Twig_Node_Expression_Array $args = NULL, $lineno, $tag)
{
$attrs = array_merge(array('value' => false),$attrs);
$nodes = array('args' => $args, 'body' => $body);
parent::__construct($nodes, $attrs, $lineno,$tag);
}
public function compile(Twig_Compiler $compiler)
{
$compiler->addDebugInfo($this);
$compiler->write('$context["'.$this->getAttribute('assign')->getValue().'"] = $context["this"]->beginWidget("'.$this->getAttribute('alias').'",');
$argNode = $this->getNode('args');
$compiler->subcompile($argNode)
->raw(');')
->raw("\n");
$compiler->indent()->subcompile($this->getNode('body'));
$compiler->raw('$context["this"]->endWidget();');
}
}
?>
然后在Twig初始化的地方增加我们的语法解析类:
复制代码 代码如下:
$twig->addTokenParser(new Yii_WidgetBlock_TokenParser);
然后我们就可以在twig的模板里这么写了:
复制代码 代码如下:
{% beginwidget 'CActiveForm' as form %}
<ul>
<li>
{{ form.label(model, 'username') }}
{{ form.textField(model, 'username') }}
</li>
<li>
{{ form.label(model, 'password') }}
{{ form.passwordField(model, 'password') }}
</li>
</ul>
{% endwidget %}
您可能感兴趣的文章: