Home>Article>Backend Development> Yii Framework Official Guide Series 49 - Special Topic: Using Non-Mainstream Template Syntax

Yii Framework Official Guide Series 49 - Special Topic: Using Non-Mainstream Template Syntax

黄舟
黄舟 Original
2017-02-16 09:46:31 1275browse



The Yii framework allows developers to use their favorite template syntax (such as Prado, Smarty) to write the view of a controller or widget. This can be done through Write and install a viewRenderer application component to achieve this. This view renderer intercepts the call to CBaseController::renderFile, compiles the view file through custom template syntax, and then renders the final compiled result.

Info:It is recommended to use custom template syntax only when writing views that are rarely reused. Otherwise, reusing views in the application will force the use of the same template syntax.

Next, we will introduce how to use CPradoViewRenderer, a view renderer similar to the Prado framework that allows developers to use custom template syntax. If you want to develop your own view renderer, CPradoViewRenderer is a good guide.

1. UseCPradoViewRenderer

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


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

By default, CPradoViewRenderer will compile source view files and save the resulting PHP files under the runtime 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 tags

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


<%= CHtml::textField($name,'value'); %> <% foreach($models as $model): %>

is translated into


 

Component tags

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


##

 // body content for the widget  // a widget without body content 

where

WidgetClassspecifies 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,


would be translated as


widget('CCaptcha', array( 'captchaAction'=>'captcha', 'showRefreshButton'=>false)); ?>

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

Cache tags

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


 // content being cached 

where

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


 // user profile information here 

would be translated as


beginCache('profile', array('duration'=>3600))): ?> // user profile information here endCache(); endif; ?>

clip tag

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


 // content for this clip 

where

clipIDis an identifier that uniquely identifies the clip content. The clip tags will be translated as


beginClip('clipID'); ?> // content for this clip endClip(); ?>

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,


2. Mixed template formats

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.tplwill be rendered using the installed view renderer, while all other view files ending with.phpwill be treated as normal PHP view script.The above is Yii Framework Official Guide Series 49—— Special topic: Content using non-mainstream template syntax. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn