溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

PHP的模板引擎Twig怎么在Yii框架中使用

發(fā)布時間:2020-12-22 14:16:09 來源:億速云 閱讀:240 作者:Leah 欄目:開發(fā)技術

這篇文章給大家介紹PHP的模板引擎Twig怎么在Yii框架中使用,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

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


遍歷數(shù)組:

復制代碼 代碼如下:


{% for user in users %}
  * {{ user.name }}
{% else %}
  No user has been found.
{% endfor %}

但是要在Yii Framework集成Twig就會遇到點麻煩了,官方網(wǎng)站中已經(jīng)有能夠集成Twig的方案,所以這里我也不再贅述。但是由于Twig中是不支持PHP語法的,所以在有些表達上會遇到困難,比如我們在寫Form的視圖時,經(jīng)常會這么寫:

復制代碼 代碼如下:


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

關于PHP的模板引擎Twig怎么在Yii框架中使用就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI