首頁  >  文章  >  後端開發  >  Yii框架官方指南系列49—專題:使用非主流模板語法

Yii框架官方指南系列49—專題:使用非主流模板語法

黄舟
黄舟原創
2017-02-16 09:46:311301瀏覽



Yii框架允許開發者使用自己喜好的模板語法(例如Prado, Smarty)來編寫控制器或掛件的視圖. 這可以透過編寫和安裝一個viewRenderer應用元件來實現. 這個視圖渲染器攔截CBaseController::renderFile的調用,透過自訂的模板語法編譯視圖文件,然後渲染最終編譯結果.

Info: 只有當編寫的視圖很少復用時才推薦使用自訂模板語法. 否則的話,在應用中復用視圖將會強制使用同樣的模板語法.

接下來, 我們會介紹如何使用 CPradoViewRenderer,一個和Prado框架類似,允許開發者使用自定義模板語法的視圖渲染器. 如果你想要發展自己的視圖渲染器, CPradoViewRenderer是一個很好的指南.

1. 使用CPradoViewRenderer

To use CPradoViewRenderer, we just need to configure the


By default, CPradoViewRenderer will compile source view files and save the resulting PHP files under theruntime directory. Only when the source view files are changed, will the PHP filefiles be reen the source degradation.

Tip:

 While CPradoViewRenderer mainly introduces some new template tags to make writing views easier and faster, you can still write PHPs olloas usual inwe views. that are supported by CPradoViewRenderer.

PHP短標籤

Short PHP tags are shortcuts to writing PHP expressions and statements in a view. The expression tag 

to 

. For example,

組件標籤 Component tags are used to insert a widget in a view. It uses the following syntax:


return array(
    'components'=>array(
        ......,
        'viewRenderer'=>array(
            'class'=>'CPradoViewRenderer',
        ),
    ),
);

, where

<%= CHtml::textField($name,&#39;value&#39;); %>
<% foreach($models as $model): %>
perty initial values can be either quoted strings or PHP expressions enclosed within a pair of curly brackets. For example,


<?php echo CHtml::textField($name,&#39;value&#39;); ?>
<?php foreach($models as $model): ?>

Note:

 The value for 
showRefreshButton

is specified as 

{false}

 instead of 

"false"

 because the latter means a string instead of a boolean.

Itse ache ache achesoo​​an achester let. lows,


<com:WidgetClass property1=value1 property2=value2 ...>
    // body content for the widget
</com:WidgetClass>

// a widget without body content
<com:WidgetClass property1=value1 property2=value2 .../>

where 

fragmentID

 should be an identifier that uniquely identifies the content being cached, and the properpraged 區 the properpment, ache the propertyal.

<com:CCaptcha captchaAction="captcha" showRefreshButton={false} />

would be translated as

<pre class="brush:php;toolbar:false">&lt;?php $this-&gt;widget('CCaptcha', array(     'captchaAction'=&gt;'captcha',     'showRefreshButton'=&gt;false)); ?&gt;</pre>夾子標籤

Like cache tags, cliplip tags areortcuts 到 BasebeC ax is as follows,


<cache:fragmentID property1=value1 property2=value2 ...>
    // content being cached
</cache:fragmentID >

where 

clipID

 is an identifier that uniquely identifies the clip content. The clip tags will be transp

評論標籤

Comment tags are used to write view comments that should only be visible to developers. Comment tags will be stripped off when the view is displayed to end users. The syntax for comment tags is as follows, s


Starting from version 1.1.2, it is possible to mix the usage of some alternative template syntax with the normal PHP syntax. To do so, the CViewRenderer::fileExtension property of the installed view renderer be be . php

. For example, if the property is set as 

.tpl

, then any view file ending with 

.tpl

 will be rendered using the installed view renderer, while  will be rendered using the willlled view renderer, while . as normal PHP view script.

 以上就是Yii框架官方指南系列49-專題:使用非主流模板語法的內容,更多相關內容請關注PHP中文網(www.php.cn)!


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn