Using Alternative Template Syntax
In the following, we introduce how to use CPradoViewRenderer, a viewrenderer that allows developers to use the template syntax similar to thatin . For people who want todevelop their own view renderers, CPradoViewRenderer is a good reference.
To use , we just need to configure the application asfollows:
By default, CPradoViewRenderer will compile source view files and savethe resulting PHP files under the directory. Only when thesource view files are changed, will the PHP files be re-generated.Therefore, using CPradoViewRenderer incurs very little performancedegradation.
In the following, we introduce the template tags that are supported by.
Short PHP tags are shortcuts to writing PHP expressions and statements ina view. The expression tag is translated into<?php echo expression ?>
; while the statement tag <% statement
to
%><?php statement ?>
. For example,
- <%= CHtml::textField($name,'value'); %>
- <% foreach($models as $model): %>
- <?php echo CHtml::textField($name,'value'); ?>
- <?php foreach($models as $model): ?>
Component tags are used to insert awidget in a view. It uses the followingsyntax:
- // body content for the widget
- </com:WidgetClass>
- // a widget without body content
- <com:WidgetClass property1=value1 property2=value2 .../>
where specifies the widget class name or class , and property initial values can beeither quoted strings or PHP expressions enclosed within a pair of curlybrackets. For example,
would be translated as
- <?php $this->widget('CCaptcha', array(
- 'captchaAction'=>'captcha',
- 'showRefreshButton'=>false)); ?>
Cache tags are shortcuts to using fragmentcaching. Its syntax is as follows,
- <cache:fragmentID property1=value1 property2=value2 ...>
- // content being cached
- </cache:fragmentID >
where should be an identifier that uniquely identifies thecontent being cached, and the property-value pairs are used to configurethe fragment cache. For example,
- // user profile information here
- </cache:profile >
Like cache tags, clip tags are shortcuts to calling and CBaseController::endClip in a view. Thesyntax is as follows,
- <clip:clipID>
- // content for this clip
- </clip:clipID >
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(); ?>
Comment tags are used to write view comments that should only be visibleto developers. Comment tags will be stripped off when the view is displayedto end users. The syntax for comment tags is as follows,
- <!---
- view comments that will be stripped off
- --->
2. Mixing Template Formats ¶
Starting from version 1.1.2, it is possible to mix the usage of some alternativetemplate syntax with the normal PHP syntax. To do so, the CViewRenderer::fileExtensionproperty 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 will be rendered using the installed view renderer, while all other view files endingwith .php
will be treated as normal PHP view script.