YII 主题
heming是一个在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 directorythemes/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. Theming Widgets 主题挂件
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.
创建一个Foo挂件的theme view xyz,我们应该在theme's view文件夹下创建Foo文件夹。
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. Customizing Widgets Globally
//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,
));
这个对pageSize无效。
参考:http://www.yiiframework.com/forum/index.php/topic/11510-cgridview-how-to-set-default-page-size/ http://danaluther.blogspot.com/2012/02/leveraging-widgets-widget-factory-and.html
YII 主题的更多相关文章
- yii主题
修改应用的配置文件(protected/config/main.php)中加入 return array( ’theme’=>’basic’, ); 所有的视图文件必须位于views下 ,布局视 ...
- YII 主题设置
节日不同,站点显示不同主题.就是解决问题. 也制作多套视图. 不是必需为全部页面设置主题,假设没有.就依照正常视图显示 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5 ...
- Yii 2.x 多主题 - 多语言 配置
语言:只要在原来模板的位置建立语言目录 多主题:要重新定义模板的根目录
- PhpStorm 8.x/9.x 快捷键设置/个性化设置,如何多项目共存?如何更换主题?
1."自定义"常用快捷键(设置成跟Eclipse差不多) 按照路径:File -> Settings -> Appearance & Behavior -> ...
- yii模块下面的组件
模块的定义就不写了,直接进入主题看目录和文件: application/modules/client/controllers/UserController.php <?php class Use ...
- yii开发一个web程序的基本流程
1. 创建目录结构.在前面的章节Creating First Yii Application写的yiic工具可以帮助我们快速完成这步. 2. 配置 application.就是修改applicatio ...
- Yii的学习(1)--安装配置
之前在sina博客写过Yii的文章,来到博客园之后,没再写过关于Yii的文章,正好端午假期没啥事,就结合以前的博客,Yii的官方文档,再加上最近的关于Yii的收获总结一下,写个系列~~ Yii是一个基 ...
- yii和wp做博客
第一步,安装yii和wp: 第二步,创建protected/components/ExceptionHandler.php文件 <?php class ExceptionHandler { pu ...
- yii 多模板
main.php: //替换所有模板 //加载文件名为first的模板 // 'theme'=>'theme1', 'components'=>array( ...
随机推荐
- SQL Server 2012 sa 用户登录 18456 错误
近期想研究下SQL SERVER2012 Enterprise版本号的数据库,听说功能非常强大. 我是在win7上安装的,安装的过程非常顺利,我在用"Windows 身份验证"时, ...
- spring mvc DispatcherServlet详解之一--request通过HandlerMaping获取控制器Controller过程
整个spring mvc的架构如下图所示: 现在来讲解DispatcherServletDispatcherServlet的第一步:获取控制器. HandlerMapping HandlerMappi ...
- (转载)Linux下安装配置MySQL+Apache+PHP+WordPress的详细笔记
Linux下安装配置MySQL+Apache+PHP+WordPress的详细笔记 Linux下配LMAP环境,花了我好几天的时间.之前没有配置过,网上的安装资料比较混乱,加上我用的版本问题,安装过程 ...
- 使用fastjson前台报406的问题解决方法
返回的json数据前台页面报406,而后台没有报错,下面为解决方法 <?xml version="1.0" encoding="UTF-8"?> & ...
- Unity3D 游戏的碰撞
首先创建两个精灵,然后都绑定上碰撞方法(这个是在上一篇文章的基本上): 不过 要注意一点就是碰撞器需要挂一个重力组件,不然无效 所以添加了差不多就能够实现物体碰撞了: 接下来技术写代码,让碰撞的时候进 ...
- PHP替换数据库的换行符
//php 有三种方法来解决 //1.使用str_replace 来替换换行 $str = str_replace(array("\r\n", "\r", &q ...
- VS2010在WIN7 64位系统下架设网站及路由器配置
步骤一:安装IIS 打开[控制面板]-[程序和功能],在左侧进入[打开或关闭windows功能],按照下图选择Internet信息项目下的子选项并安装: 步骤二:配置应用程序池 打开[控制面板]-[管 ...
- MySQL 5.6 解决InnoDB: Error: Table "mysql"."innodb_table_stats" not found.问题
在安装MySQL 5.6.30时,安装完成后,后台日志报如下警告信息:2016-05-27 12:25:27 7fabf86f7700 InnoDB: Error: Table "mysql ...
- 【转】iOS-Core-Animation-Advanced-Techniques(二)
原文: http://www.cocoachina.com/ios/20150104/10816.html 视觉效果和变换 (四)视觉效果 嗯,园和椭圆还不错,但如果是带圆角的矩形呢? 我们现在能做到 ...
- OC - 26.CAAnimationGroup
概述 简介 CAAnimationGroup又称组动画或动画组 将多个动画放到动画组中,并赋值给layer的animations属性,动画组中所有动画就会并发执行 注意事项 动画组中的动画不会被压缩, ...