Yii Framework official guide series 49-topics: use non-mainstream template syntax

Source: Internet
Author: User
Tags comment tag php foreach
The Yii Framework allows developers to use their preferred template syntax (such as Prado and Smarty) to write controllers or pendant views. this can be achieved by writing and installing a viewRenderer application component. this view renderer intercepts CB...



The Yii Framework allows developers to use their preferred template syntax (such as Prado and Smarty) to write controllers or pendant views. this can be achieved by writing and installing a viewRenderer application component. this view renderer intercepts the call of CBaseController: renderFile, compiles the view file through the custom template syntax, and then renders the final compilation result.

Info:The custom template syntax is recommended only when few views are edited. otherwise, reuse the view in the application will force 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. use CPradoViewRenderer

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 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 tag

Short PHP tags are shortcuts to writing PHP expressions and statements in a view. The expression tag<%= expression %>Is translated While the statement tag<% statement %>To . For example,


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

Is translated


 
 

Component tag

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
 

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


 

Wocould be translated


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

Note:The valueshowRefreshButtonIs specified{false}Instead"false"Because the latter means a string instead of a boolean.

Cache tag

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


 
      // content being cached
 

WherefragmentIDShocould 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
 

Wocould be translated


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

Clip label

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
 

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


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

Comment tag

Comment tags are used to write view comments that shoshould 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 format

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.php. For example, if the property is set.tpl, Then any view file ending.tplWill be rendered using the installed view renderer, while all other view files ending.phpWill be treated as normal PHP view script.

The above is the Yii Framework official guide series 49-special topic: content using non-mainstream template syntax. For more information, see PHP Chinese website (www.php1.cn )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.