Rumah > pembangunan bahagian belakang > tutorial php > Yii框架官方指南系列49——专题:使用非主流模板语法

Yii框架官方指南系列49——专题:使用非主流模板语法

黄舟
Lepaskan: 2023-03-05 19:08:01
asal
1374 orang telah melayarinya



Yii框架允许开发者使用自己喜好的模板语法 (例如 Prado, Smarty)来编写控制器或者挂件的视图. 这可以通过编写和安装一个viewRenderer应用组件来实现. 这个视图渲染器拦截CBaseController::renderFile的调用,通过自定义的模板语法编译视图文件,然后渲染最终编译结果.

Info: 只有当编写的视图很少复用时才推荐使用自定义模板语法. 否则的话,在应用中复用视图将会强制使用同样的模板语法.

接下来, 我们会介绍如何使用 CPradoViewRenderer,一个和Prado框架类似,允许开发者使用自定义模板语法的视图渲染器. 如果你想要开发自己的视图渲染器, CPradoViewRenderer是一个很好的指南.

1. 使用CPradoViewRenderer

To use CPradoViewRenderer, we just need to configure the application as follows:


return array(
    'components'=>array(
        ......,
        'viewRenderer'=>array(
            'class'=>'CPradoViewRenderer',
        ),
    ),
);
Salin selepas log masuk

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 files be re-generated. Therefore, using CPradoViewRenderer incurs very little performance degradation.

Tip: While CPradoViewRenderer mainly introduces some new template tags to make writing views easier and faster, you can still write PHP code as usual in the source views.

In the following, we introduce the template tags that are supported by CPradoViewRenderer.

PHP短标签

Short PHP tags are shortcuts to writing PHP expressions and statements in a view. The expression tag <%= expression %> is translated into <?php echo expression ?>; while the statement tag <% statement %>to <?php statement ?>. For example,


<%= CHtml::textField($name,&#39;value&#39;); %>
<% foreach($models as $model): %>
Salin selepas log masuk

is translated into


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

组件标签

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


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

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

where WidgetClass specifies the widget class name or class path alias, and property initial values can be either quoted strings or PHP expressions enclosed within a pair of curly brackets. For example,


<com:CCaptcha captchaAction="captcha" showRefreshButton={false} />
Salin selepas log masuk

would be translated as


<?php $this->widget('CCaptcha', array(
    'captchaAction'=>'captcha',
    'showRefreshButton'=>false)); ?>
Salin selepas log masuk

Note: The value for showRefreshButton is specified as {false} instead of "false" because the latter means a string instead of a boolean.

缓存标签

Cache tags are shortcuts to using fragment caching. Its syntax is as follows,


<cache:fragmentID property1=value1 property2=value2 ...>
    // content being cached
</cache:fragmentID >
Salin selepas log masuk

where fragmentID should be an identifier that uniquely identifies the content being cached, and the property-value pairs are used to configure the fragment cache. For example,


<cache:profile duration={3600}>
    // user profile information here
</cache:profile >
Salin selepas log masuk

would be translated as


<?php if($this->beginCache('profile', array('duration'=>3600))): ?>
    // user profile information here
<?php $this->endCache(); endif; ?>
Salin selepas log masuk

夹子标签

Like cache tags, clip tags are shortcuts to calling CBaseController::beginClip and CBaseController::endClip in a view. The syntax is as follows,


<clip:clipID>
    // content for this clip
</clip:clipID >
Salin selepas log masuk

where clipID is an identifier that uniquely identifies the clip content. The clip tags will be translated as


<?php $this->beginClip('clipID'); ?>
    // content for this clip
<?php $this->endClip(); ?>
Salin selepas log masuk

评论标签

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,


<!---
view comments that will be stripped off
--->
Salin selepas log masuk

2. 混合模板格式

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 must be configured with a value other than .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 all other view files ending with .php will be treated as normal PHP view script.

 以上就是Yii框架官方指南系列49——专题:使用非主流模板语法的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!


sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan