Yii框架官方指南系列44——专题:Theming(主题)
Theming是一个在Web应用程序里定制网页外观的系统方式。通过采用一个新的主题,网页应用程序的整体外观可以立即和戏剧性的改变。
在Yii,每个主题由一个目录代表,包含view文件,layout文件和相关的资源文件,如图片, CSS文件, JavaScript文件等。主题的名字就是他的目录名字。全部主题都放在在同一目录WebRoot/themes
下 。在任何时候,只有一个主题可以被激活。
提示:默认的主题根目录
WebRoot/themes
可被配置成其他的。只需要配置themeManager应用部件的属性basePath和baseUrl为你所要的值。
要激活一个主题,设置Web应用程序的属性theme为你所要的名字。可以在application configuration中配置或者在执行过程中在控制器的动作里面修改。
注:主题名称是区分大小写的。如果您尝试启动一个不存在的主题,
yii:
:app()->theme
将返回null
。
主题目录里面内容的组织方式和application base path目录下的组织方式一样。例如,所有的view文件必须位于views
下 ,布局view文件在views/layouts
下 ,和系统view文件在views/system
下。例如,如果我们要替换PostController
的create
view文件为classic
主题下,我们将保存新的view文件为WebRoot/themes/classic/views/post/create.php
。
对于在module里面的控制器view文件,相应主题view文件将被放在views
目录下。例如,如果上述的PostController
是在一个命名为forum
的模块里 ,我们应该保存create
view 文件为WebRoot/themes/classic/views/forum/post/create.php
。如果 forum
模块嵌套在另一个名为support
模块里 ,那么view文件应为WebRoot/themes/classic/views/support/forum/post/create.php
。
注:由于
views
目录可能包含安全敏感数据,应当配置以防止被网络用户访问。
当我们调用render或renderPartial显示视图,相应的view文件以及布局文件将在当前激活的主题里寻找。如果发现,这些文件将被render渲染。否则,就后退到viewPath和layoutPath 所指定的预设位置寻找。
baseurl属性,我们就可以为此图像文件生成如下url,
yii">登录后复制提示:在一个主题的视图,我们经常需要链接其他主题资源文件。例如,我们可能要显示一个在主题下
images
目录里的图像文件。使用当前激活主题的baseurl属性,我们就可以为此图像文件生成如下url,
yii: :app()->theme->baseUrl . '/images/FileName.gif'登录后复制
Below is an example of directory organization for an application with two themes basic
and fancy
.
WebRoot/ assets protected/ .htaccess components/ controllers/ models/ views/ layouts/ main.php site/ index.php themes/ basic/ views/ .htaccess layouts/ main.php site/ index.php fancy/ views/ .htaccess layouts/ main.php site/ index.php
In the application configuration, if we configure
return array( 'theme'=>'basic', ...... );
then the basic
theme will be in effect, which means the application's layout will use the one under the directory themes/basic/views/layouts
, and the site's index view will use the one underthemes/basic/views/site
. In case a view file is not found in the theme, it will fall back to the one under theprotected/views
directory.
1. 主题挂件
Starting from version 1.1.5, views used by a widget can also be themed. In particular, when we callCWidget::render() to render a widget view, Yii will attempt to search under the theme folder as well as the widget view folder for the desired view file.
To theme the view xyz
for a widget whose class name is Foo
, we should first create a folder named Foo
(same as the widget class name) under the currently active theme's view folder. If the widget class is namespaced (available in PHP 5.3.0 or above), such as \app\widgets\Foo
, we should create a folder namedapp_widgets_Foo
. That is, we replace the namespace separators with the underscore characters.
We then create a view file named xyz.php
under the newly created folder. To this end, we should have a filethemes/basic/views/Foo/xyz.php
, which will be used by the widget to replace its original view, if the currently active theme is basic
.
2. 自定义全局挂件
Note: this feature has been available since version 1.1.3.
When using a widget provided by third party or Yii, we often need to customize it for specific needs. For example, we may want to change the value of CLinkPager::maxButtonCount from 10 (default) to 5. We can accomplish this by passing the initial property values when calling CBaseController::widget to create a widget. However, it becomes troublesome to do so if we have to repeat the same customization in every place we useCLinkPager.
$this->widget('CLinkPager', array( 'pages'=>$pagination, 'maxButtonCount'=>5, 'cssFile'=>false, ));
Using the global widget customization feature, we only need to specify these initial values in a single place, i.e., the application configuration. This makes the customization of widgets more manageable.
To use the global widget customization feature, we need to configure the widgetFactory as follows:
return array( 'components'=>array( 'widgetFactory'=>array( 'widgets'=>array( 'CLinkPager'=>array( 'maxButtonCount'=>5, 'cssFile'=>false, ), 'CJuiDatePicker'=>array( 'language'=>'ru', ), ), ), ), );
In the above, we specify the global widget customization for both CLinkPager and CJuiDatePicker widgets by configuring the CWidgetFactory::widgets property. Note that the global customization for each widget is represented as a key-value pair in the array, where the key refers to the wiget class name while the value specifies the initial property value array.
Now, whenever we create a CLinkPager widget in a view, the above property values will be assigned to the widget, and we only need to write the following code in the view to create the widget:
$this->widget('CLinkPager', array( 'pages'=>$pagination, ));
We can still override the initial property values when necessary. For example, if in some view we want to setmaxButtonCount
to be 2, we can do the following:
$this->widget('CLinkPager', array( 'pages'=>$pagination, 'maxButtonCount'=>2, ));
3. 皮肤
Note: The skin feature has been available since version 1.1.0.
While using a theme we can quickly change the outlook of views, we can use skins to systematically customize the outlook of the widgets used in the views.
A skin is an array of name-value pairs that can be used to initialize the properties of a widget. A skin belongs to a widget class, and a widget class can have multiple skins identified by their names. For example, we can have a skin for the CLinkPager widget and the skin is named as classic
.
In order to use the skin feature, we first need to modify the application configuration by configuring theCWidgetFactory::enableSkin property to be true for the widgetFactory
application component:
return array( 'components'=>array( 'widgetFactory'=>array( 'enableSkin'=>true, ), ), );
Please note that in versions prior to 1.1.3, you need to use the following configuration to enable widget skinning:
return array( 'components'=>array( 'widgetFactory'=>array( 'class'=>'CWidgetFactory', ), ), );
We then create the needed skins. Skins belonging to the same widget class are stored in a single PHP script file whose name is the widget class name. All these skin files are stored under protected/views/skins
, by default. If you want to change this to be a different directory, you may configure the skinPath
property of thewidgetFactory
component. As an example, we may create under protected/views/skins
a file namedCLinkPager.php
whose content is as follows,
<?php return array( 'default'=>array( 'nextPageLabel'=>'>>', 'prevPageLabel'=>'<<', ), 'classic'=>array( 'header'=>'', 'maxButtonCount'=>5, ), );
In the above, we create two skins for the CLinkPager widget: default
and classic
. The former is the skin that will be applied to any CLinkPager widget that we do not explicitly specify its skin
property, while the latter is the skin to be applied to a CLinkPager widget whose skin
property is specified as classic
. For example, in the following view code, the first pager will use the default
skin while the second the classic
skin:
<?php $this->widget('CLinkPager'); ?> <?php $this->widget('CLinkPager', array('skin'=>'classic')); ?>
If we create a widget with a set of initial property values, they will take precedence and be merged with any applicable skin. For example, the following view code will create a pager whose initial values will bearray('header'=>'', 'maxButtonCount'=>6, 'cssFile'=>false)
, which is the result of merging the initial property values specified in the view and the classic
skin.
<?php $this->widget('CLinkPager', array( 'skin'=>'classic', 'maxButtonCount'=>6, 'cssFile'=>false, )); ?>
Note that the skin feature does NOT require using themes. However, when a theme is active, Yii will also look for skins under the skins
directory of the theme's view directory (e.g.WebRoot/themes/classic/views/skins
). In case a skin with the same name exists in both the theme and the main application view directories, the theme skin will take precedence.
If a widget is using a skin that does not exist, Yii will still create the widget as usual without any error.
Info: Using skin may degrade the performance because Yii needs to look for the skin file the first time a widget is being created.
Skin is very similar to the global widget customization feature. The main differences are as follows.
Skin is more related with the customization of presentational property values;
A widget can have multiple skins;
Skin is themeable;
Using skin is more expensive than using global widget customization.
以上就是Yii框架官方指南系列44——专题:Theming(主题)的内容,更多相关内容请关注PHP中文网(www.php.cn)!

热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

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

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

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

Dreamweaver CS6
视觉化网页开发工具

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

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

Yii框架中间件:为应用程序提供多重数据存储支持介绍中间件(middleware)是Yii框架中的一个重要概念,它为应用程序提供了多重数据存储支持。中间件的作用类似于一个过滤器,它能够在应用程序的请求和响应之间插入自定义代码。通过中间件,我们可以对请求进行处理、验证、过滤,然后将处理后的结果传递给下一个中间件或最终的处理程序。Yii框架中的中间件使用起来非常

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

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

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

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

在Yii框架中,控制器(Controllers)扮演着处理请求的重要角色。除了处理常规的页面请求之外,控制器还可以用于处理Ajax请求。本文将介绍在Yii框架中处理Ajax请求的方法,并提供代码示例。在Yii框架中,处理Ajax请求可以通过以下步骤进行:第一步,创建一个控制器(Controller)类。可以通过继承Yii框架提供的基础控制器类yiiwebCo

在现代的Web应用程序开发中,调试工具是不可或缺的。它们可以帮助开发者查找和解决应用程序的各种问题。Yii框架作为一款流行的Web应用程序框架,自然也提供了一些调试工具。本文将重点介绍Yii框架中的调试工具,并讨论它们如何帮助我们分析和调试应用程序。GiiGii是Yii框架的代码生成器。它可以自动生成Yii应用程序的代码,如模型、控制器和视图等。使用Gii,
