Using Alternative Template Syntax

Yii allows developers to use their own favorite template syntax (e.g.Prado, Smarty) to write controller or widget views. This is achieved bywriting and installing a viewRendererapplication component. The view renderer intercepts the invocations ofCBaseController::renderFile, compiles the view file with customizedtemplate syntax, and renders the compiling results.

Info: It is recommended to use customized template syntax only when writing views that are less likely to be reused. Otherwise, people who are reusing the views would be forced to use the same customized template syntax in their applications.

In the following, we introduce how to use CPradoViewRenderer, a viewrenderer that allows developers to use the template syntax similar to thatin Prado framework. For people who want todevelop their own view renderers, CPradoViewRenderer is a good reference.

1. Using CPradoViewRenderer ¶

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

  1. return array(
  2. 'components'=>array(
  3. ......,
  4. 'viewRenderer'=>array(
  5. 'class'=>'CPradoViewRenderer',
  6. ),
  7. ),
  8. );

By default, CPradoViewRenderer will compile source view files and savethe resulting PHP files under theruntime directory. Only when thesource view files are changed, will the PHP files be re-generated.Therefore, using CPradoViewRenderer incurs very little performancedegradation.

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 byCPradoViewRenderer.

Short PHP Tags

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

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

is translated into

  1. <?php echo CHtml::textField($name,'value'); ?>
  2. <?php foreach($models as $model): ?>

Component Tags

Component tags are used to insert awidget in a view. It uses the followingsyntax:

  1. <com:WidgetClass property1=value1 property2=value2 ...>
  2. // body content for the widget
  3. </com:WidgetClass>
  4.  
  5. // a widget without body content
  6. <com:WidgetClass property1=value1 property2=value2 .../>

where WidgetClass specifies the widget class name or class pathalias, and property initial values can beeither quoted strings or PHP expressions enclosed within a pair of curlybrackets. For example,

  1. <com:CCaptcha captchaAction="captcha" showRefreshButton={false} />

would be translated as

  1. <?php $this->widget('CCaptcha', array(
  2. 'captchaAction'=>'captcha',
  3. 'showRefreshButton'=>false)); ?>
Note: The value for showRefreshButton is specified as {false} instead of "false" because the latter means a string instead of a boolean.

Cache Tags

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

  1. <cache:fragmentID property1=value1 property2=value2 ...>
  2. // content being cached
  3. </cache:fragmentID >

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

  1. <cache:profile duration={3600}>
  2. // user profile information here
  3. </cache:profile >

would be translated as

  1. <?php if($this->beginCache('profile', array('duration'=>3600))): ?>
  2. // user profile information here
  3. <?php $this->endCache(); endif; ?>

Clip Tags

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

  1. <clip:clipID>
  2. // content for this clip
  3. </clip:clipID >

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

  1. <?php $this->beginClip('clipID'); ?>
  2. // content for this clip
  3. <?php $this->endClip(); ?>

Comment Tags

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,

  1. <!---
  2. view comments that will be stripped off
  3. --->

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 .tplwill be rendered using the installed view renderer, while all other view files endingwith .php will be treated as normal PHP view script.

原文: https://www.yiiframework.com/doc/guide/1.1/zh-cn/topics.prado