Yii框架官方指南系列49——专题:使用非主流模板语法
Yii框架允许开发者使用自己喜好的模板语法 (例如 Prado, Smarty)来编写控制器或者挂件的视图. 这可以通过编写和安装一个viewRenderer应用组件来实现. 这个视图渲染器拦截CBaseController::renderFile的调用,通过自定义的模板语法编译视图文件,然后渲染最终编译结果.
Info: 只有当编写的视图很少复用时才推荐使用自定义模板语法. 否则的话,在应用中复用视图将会强制使用同样的模板语法.
接下来, 我们会介绍如何使用 CPradoViewRenderer,一个和Prado框架类似,允许开发者使用自定义模板语法的视图渲染器. 如果你想要开发自己的视图渲染器, CPradoViewRenderer是一个很好的指南.
1. 使用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 PHP tags are shortcuts to writing PHP expressions and statements in a view. The expression tag <%= expression %>
is translated into <?php echo expression ?>
; while the 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 tags are used to insert 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
showRefreshButton
is specified as{false}
instead of"false"
because the latter means a string instead of a boolean.
缓存标签
Cache tags are shortcuts to using fragment caching. Its syntax is as follows,
<cache:fragmentID property1=value1 property2=value2 ...> // content being cached </cache:fragmentID >
where fragmentID
should be an identifier that uniquely identifies the content being cached, and the property-value pairs are used 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 profile information here <?php $this->endCache(); endif; ?>
夹子标签
Like cache tags, clip tags are 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 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 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,
<!--- view comments that will be stripped off --->
2. 混合模板格式
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 .tpl
will be rendered using the installed view renderer, while all other view files ending with .php
will be treated as normal PHP view script.
以上就是Yii框架官方指南系列49——专题:使用非主流模板语法的内容,更多相关内容请关注PHP中文网(www.php.cn)!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

随着Web应用程序的快速发展,现代Web开发已成为一项重要技能。许多框架和工具可用于开发高效的Web应用程序,其中Yii框架就是一个非常流行的框架。Yii是一个高性能、基于组件的PHP框架,它采用了最新的设计模式和技术,提供了强大的工具和组件,是构建复杂Web应用程序的理想选择。在本文中,我们将讨论如何使用Yii框架来构建Web应用程序。安装Yii框架首先,

随着软件开发的日益复杂化,确保代码质量变得越来越重要。在Yii框架中,单元测试是一种非常强大的工具,可以确保代码的正确性和稳定性。在本文中,我们将深入探讨Yii框架中的单元测试,并介绍如何使用Yii框架进行单元测试。什么是单元测试?单元测试是一种软件测试方法,通常用于测试一个模块、函数或方法的正确性。单元测试通常由开发人员编写,旨在确保代码的正确性和稳定性。

Yii是一款基于PHP的高性能MVC框架,它提供了非常丰富的工具和功能,支持快速、高效地开发Web应用程序。其中,Yii框架的RESTfulAPI功能得到了越来越多开发者的关注和喜爱,因为使用Yii框架可以非常方便地构建出高性能、易扩展的RESTful接口,为Web应用的开发提供了强有力的支持。RESTfulAPI简介RESTfulAPI是一种基于

使用Yii框架实现网页缓存和页面分块的步骤引言:在Web开发过程中,为了提高网站的性能和用户体验,常常需要对页面进行缓存和分块处理。Yii框架提供了强大的缓存和布局功能,可以帮助开发者快速实现网页缓存和页面分块,本文将介绍如何使用Yii框架进行网页缓存和页面分块的实现。一、网页缓存开启网页缓存在Yii框架中,可以通过配置文件来开启网页缓存。打开主配置文件co

近年来,随着游戏行业的快速发展,越来越多的玩家开始寻找游戏攻略来帮助游戏过关。因此,创建一个游戏攻略网站可以让玩家们更加方便地获取游戏攻略,同时也能为玩家提供更好的游戏体验。在创建这样一个网站时,我们可以使用Yii框架来进行开发。Yii框架是一个基于PHP编程语言的Web应用开发框架。它具有高效、安全、扩展性强等特点,可以为我们更快速、高效地创建一个游戏攻略

使用Yii框架中间件加密和解密敏感数据引言:在现代的互联网应用中,隐私和数据安全是非常重要的问题。为了确保用户的敏感数据不被未经授权的访问者获取,我们需要对这些数据进行加密。Yii框架为我们提供了一种简单且有效的方法来实现加密和解密敏感数据的功能。在本文中,我们将介绍如何使用Yii框架的中间件来实现这一目标。Yii框架简介Yii框架是一个高性能的PHP框架,

Yii框架中间件:为应用程序添加日志记录和调试功能【引言】在开发Web应用程序时,我们通常需要添加一些附加功能以提高应用的性能和稳定性。Yii框架提供了中间件的概念,使我们能够在应用程序处理请求之前和之后执行一些额外的任务。本文将介绍如何使用Yii框架的中间件功能来实现日志记录和调试功能。【什么是中间件】中间件是指在应用程序处理请求之前和之后,对请求和响应做

在Yii框架中使用控制器(Controllers)处理文件上传和下载的方法在许多Web应用程序中,文件上传和下载是非常常见的功能。在Yii框架中,我们可以通过控制器(Controllers)来处理文件的上传和下载操作。本文将介绍如何在Yii框架中使用控制器来实现文件的上传和下载,并提供相应的代码示例。一、文件上传文件上传是指将本地计算机上的文件传输到服务器上
