午夜国产狂喷潮在线观看|国产AⅤ精品一区二区久久|中文字幕AV中文字幕|国产看片高清在线

    在yii框架中使用php模板引擎twig的例子
    來(lái)源:易賢網(wǎng) 閱讀:2988 次 日期:2014-07-31 11:12:03
    溫馨提示:易賢網(wǎng)小編為您整理了“在yii框架中使用php模板引擎twig的例子”,方便廣大網(wǎng)友查閱!

    twig是一款快速、安全、靈活的php模板引擎,它內(nèi)置了許多filter和tags,并且支持模板繼承,能讓你用最簡(jiǎn)潔的代碼來(lái)描述你的模板。他的語(yǔ)法和python下的模板引擎jinjia以及django的模板語(yǔ)法都非常像。 比如我們?cè)趐hp中需要輸出變量并且將其進(jìn)行轉(zhuǎn)義時(shí),語(yǔ)法比較累贅:

    代碼如下:

    <?php echo $var ?>

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

    但是在twig中可以這樣寫(xiě):

    代碼如下:

    {{ 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就會(huì)遇到點(diǎn)麻煩了,官方網(wǎng)站中已經(jīng)有能夠集成twig的方案,所以這里我也不再贅述。但是由于twig中是不支持php語(yǔ)法的,所以在有些表達(dá)上會(huì)遇到困難,比如我們?cè)趯?xiě)form的視圖時(shí),經(jīng)常會(huì)這么寫(xiě):

    代碼如下:

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

    但是這樣的語(yǔ)法是沒(méi)法在twig中表達(dá)的,所以想去擴(kuò)展下twig的功能,讓他能夠支持我們自定義的widget標(biāo)簽,然后自動(dòng)解析成我們需要的代碼。 總共需要兩個(gè)類: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初始化的地方增加我們的語(yǔ)法解析類:

    代碼如下:

    $twig->addtokenparser(new yii_widgetblock_tokenparser);

    然后我們就可以在twig的模板里這么寫(xiě)了:

    代碼如下:

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

    更多信息請(qǐng)查看IT技術(shù)專欄

    更多信息請(qǐng)查看網(wǎng)絡(luò)編程
    易賢網(wǎng)手機(jī)網(wǎng)站地址:在yii框架中使用php模板引擎twig的例子
    由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

    2025國(guó)考·省考課程試聽(tīng)報(bào)名

    • 報(bào)班類型
    • 姓名
    • 手機(jī)號(hào)
    • 驗(yàn)證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
    工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
    云南網(wǎng)警備案專用圖標(biāo)
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
    云南網(wǎng)警報(bào)警專用圖標(biāo)