在yii框架中使用php模板引擎twig的例子
来源:易贤网 阅读:2666 次 日期:2014-07-31 11:12:03
温馨提示:易贤网小编为您整理了“在yii框架中使用php模板引擎twig的例子”,方便广大网友查阅!

twig是一款快速、安全、灵活的php模板引擎,它内置了许多filter和tags,并且支持模板继承,能让你用最简洁的代码来描述你的模板。他的语法和python下的模板引擎jinjia以及django的模板语法都非常像。 比如我们在php中需要输出变量并且将其进行转义时,语法比较累赘:

代码如下:

<?php echo $var ?>

<?php echo htmlspecialchars(\$var, ent_quotes, 'utf-8') ?>

但是在twig中可以这样写:

代码如下:

{{ var }}

{{ var|escape }}

{{ var|e }}         {# shortcut to escape a variable #}

遍历数组:

代码如下:

{% for user in users %}

  * {{ user.name }}

{% else %}

  no user has been found.

{% endfor %}

但是要在yii framework集成twig就会遇到点麻烦了,官方网站中已经有能够集成twig的方案,所以这里我也不再赘述。但是由于twig中是不支持php语法的,所以在有些表达上会遇到困难,比如我们在写form的视图时,经常会这么写:

代码如下:

<?php $form=$this->beginwidget('cactiveform'); ?>

    <span>login</span>

    <ul>

  <li>

    <?php echo $form->label($model,'username'); ?>

                <?php echo $form->textfield($model,'username'); ?>

  </li>

  <li>

    <?php echo $form->label($model,'password'); ?>

                <?php echo $form->passwordfield($model,'password'); ?>

  </li>

  <li class=last>

    <button type=submit>login</button>

  </li>

</ul>

    <?php echo $form->error($model,'password'); ?>

<?php $this->endwidget(); ?>

但是这样的语法是没法在twig中表达的,所以想去扩展下twig的功能,让他能够支持我们自定义的widget标签,然后自动解析成我们需要的代码。 总共需要两个类:tokenparser和node,下面直接上代码:

代码如下:

<?php

/*

 * this file is an extension of twig.

 *

 * (c) 2010 lfyzjck

 */

/**

 * parser widget tag in yii framework

 *

 * {% beginwidget 'cactiveform' as form %}

 *    content of form

 * {% endwidget %}

 *

 */

class yii_widgetblock_tokenparser extends twig_tokenparser

{

    /**

     * parses a token and returns a node.

     *

     * @param twig_token $token a twig_token instance

     *

     * @return twig_nodeinterface a twig_nodeinterface instance

     */

    public function parse(twig_token $token)

    {

        $lineno = $token->getline();

        $stream = $this->parser->getstream();

        $name = $stream->expect(twig_token::string_type);

        if($stream->test(twig_token::punctuation_type)){

            $args = $this->parser->getexpressionparser()->parsehashexpression();

        }

        else{

            $args = new twig_node_expression_array(array(), $lineno);

        }

        $stream->expect(twig_token::name_type);

        $assign = $stream->expect(twig_token::name_type);

        $stream->expect(twig_token::block_end_type);

        $body = $this->parser->subparse(array($this, 'decideblockend'), true);

        $stream->expect(twig_token::block_end_type);

        return new yii_node_widgetblock(array(

            'alias' => $name->getvalue(),

            'assign' => $assign,

        ), $body, $args, $lineno, $this->gettag());

    }

    /**

     * gets the tag name associated with this token parser.

     *

     * @param string the tag name

     */

    public function gettag()

    {

        return 'beginwidget';

    }

    public function decideblockend(twig_token $token)

    {

        return $token->test('endwidget');

    }

}

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 %}

更多信息请查看IT技术专栏

更多信息请查看网络编程
由于各方面情况的不断调整与变化,易贤网提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!
关于我们 | 联系我们 | 人才招聘 | 网站声明 | 网站帮助 | 非正式的简要咨询 | 简要咨询须知 | 加入群交流 | 手机站点 | 投诉建议
工业和信息化部备案号:滇ICP备2023014141号-1 云南省教育厅备案号:云教ICP备0901021 滇公网安备53010202001879号 人力资源服务许可证:(云)人服证字(2023)第0102001523号
云南网警备案专用图标
联系电话:0871-65317125(9:00—18:00) 获取招聘考试信息及咨询关注公众号:hfpxwx
咨询QQ:526150442(9:00—18:00)版权所有:易贤网
云南网警报警专用图标