Yii框架允許開發者使用自己喜好的模板語法(例如Prado, Smarty)來編寫控制器或掛件的視圖. 這可以透過編寫和安裝一個viewRenderer應用元件來實現. 這個視圖渲染器攔截CBaseController::renderFile的調用,透過自訂的模板語法編譯視圖文件,然後渲染最終編譯結果.
Info: 只有當編寫的視圖很少復用時才推薦使用自訂模板語法. 否則的話,在應用中復用視圖將會強制使用同樣的模板語法.
接下來, 我們會介紹如何使用 CPradoViewRenderer,一個和Prado框架類似,允許開發者使用自定義模板語法的視圖渲染器. 如果你想要發展自己的視圖渲染器, CPradoViewRenderer是一個很好的指南.
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短標籤to . For example,Short PHP tags are shortcuts to writing PHP expressions and statements in a view. The expression tag
組件標籤
Component tags are used to insert a widget in a view. It uses the following syntax:
return array( 'components'=>array( ......, 'viewRenderer'=>array( 'class'=>'CPradoViewRenderer', ), ), );
<%= CHtml::textField($name,'value'); %> <% foreach($models as $model): %>
<?php echo CHtml::textField($name,'value'); ?> <?php foreach($models as $model): ?>
The value for
showRefreshButton
{false}
instead of"false"
because the latter means a string instead of a boolean. Itse ache ache achesooan 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 asLike cache tags, cliplip tags areortcuts 到 BasebeC ax is as follows,
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"><?php $this->widget('CCaptcha', array( 'captchaAction'=>'captcha', 'showRefreshButton'=>false)); ?></pre><div class="contentsignin">登入後複製</div></div>
夾子標籤
<cache:fragmentID property1=value1 property2=value2 ...> // content being cached </cache:fragmentID >
clipID
is an identifier that uniquely identifies the clip content. The clip tags will be transp評論標籤
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
.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)!