The YII framework allows developers to write a view of a controller or a pendant using their preferred template syntax (for example, Prado, Smarty). This can be accomplished by writing and installing a viewrenderer application component. This view renderer intercepts cbasecontroller::renderfile calls, compiles the view file with custom template syntax, and then renders the final compilation result.
Info: custom template syntax is recommended only when you write a view that is rarely reused. Otherwise, reusing the view in the app will force the same template syntax to be used.
Next, we'll show you how to use Cpradoviewrenderer, a view renderer similar to the Prado framework that allows developers to use the 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'll compile Source view files and save the resulting PHP files under Theruntime directory. Only when the source view files is changed, would 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 W Rite PHP Code As usual in the source.
In the following, we introduce the template tags which is supported by Cpradoviewrenderer.
PHP Short Tags
Short PHP tags is shortcuts to writing PHP expressions and statements in a view. The expression tag is translated into; and the <%= expression %>
<?php echo expression ?>
statement tag <% statement %>
to <?php statement ?>
. For example,
<%= Chtml::textfield ($name, ' value '); %><% foreach ($models as $model):%>
is translated into
<?php Echo Chtml::textfield ($name, ' value ');? ><?php foreach ($models as $model):?>
Component labels
Component tags is used to inserts 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 .../>
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}/>
would be translated as
<?php $this->widget (' Ccaptcha ', Array ( ' captchaaction ' = ' captcha ', ' showrefreshbutton ' = = false));?>
Note: The value for is showRefreshButton
specified as {false}
instead of "false"
because the latter means a string instead of a Boolean.
Cache tags
Cache tags is shortcuts to using fragment caching. It syntax is as follows,
<cache:fragmentid property1=value1 property2=value2 ...> //content being Cached</cache:fragmentid >
Where fragmentID
should be a identifier that uniquely identifies the content being cached, and the property-value pairs is us Ed to configure the fragment cache. For example,
<cache:profile duration={3600}> //user profile information Here</cache:profile >
would be translated as
<?php if ($this->begincache (' profile ', Array (' duration ' =>3600)):?> //user profiles information here <?php $this->endcache (); endif;?>
Clip Label
Like cache tags, clip tags is shortcuts to calling Cbasecontroller::beginclip and Cbasecontroller::endclip in a view. The syntax is as follows,
<clip:clipID> /content for this clip</clip:clipid >
Where is a clipID
identifier that uniquely identifies the clip content. The clip tags would be translated as
<?php $this->beginclip (' clipid ');?> /content for this clip<?php $this->endclip ();?>
Comment Tags
Comment tags is used to write view comments this should only is visible to developers. Comment tags would be stripped off when the view was displayed to end users. The syntax for comment tags is as follows,
<!---view comments that'll be stripped off--->
2. Mixed template format
Starting from version 1.1.2, it's possible to mix the usage of some alternative template syntax with the normal PHP Synta Cviewrenderer::fileextension property of the installed view renderer must is configured with a value othe R than .php
. For example, if the property is set .tpl
as and then any view file ending with would be .tpl
rendered using the installed VI EW renderer, while the other view files ending with would be .php
treated as normal PHP view script.
The above is the official Yii Framework Guide Series 49--topic: Use of non-mainstream template syntax content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!