heming是一个在Web应用程序里定制网页外观的系统方式。通过采用一个新的主题,网页应用程序的整体外观可以立即和戏剧性的改变。

在Yii,每个主题由一个目录代表,包含view文件,layout文件和相关的资源文件,如图片, CSS文件, JavaScript文件等。主题的名字就是他的目录名字。全部主题都放在在同一目录WebRoot/themes下 。在任何时候,只有一个主题可以被激活。

提示:默认的主题根目录WebRoot/themes可被配置成其他的。只需要配置themeManager应用部件的属性basePathbaseUrl为你所要的值。

要激活一个主题,设置Web应用程序的属性theme为你所要的名字。可以在application configuration中配置或者在执行过程中在控制器的动作里面修改。

注:主题名称是区分大小写的。如果您尝试启动一个不存在的主题, yii: :app()->theme将返回null 。

主题目录里面内容的组织方式和application base path目录下的组织方式一样。例如,所有的view文件必须位于views下 ,布局view文件在views/layouts下 ,和系统view文件在views/system下。例如,如果我们要替换PostControllercreate 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目录可能包含安全敏感数据,应当配置以防止被网络用户访问。

当我们调用renderrenderPartial显示视图,相应的view文件以及布局文件将在当前激活的主题里寻找。如果发现,这些文件将被render渲染。否则,就后退到viewPathlayoutPath 所指定的预设位置寻找。

baseurl属性,我们就可以为此图像文件生成如下url,

  1. yii">

提示:在一个主题的视图,我们经常需要链接其他主题资源文件。例如,我们可能要显示一个在主题下images目录里的图像文件。使用当前激活主题的baseurl属性,我们就可以为此图像文件生成如下url,

  1. yii: :app()->theme->baseUrl . '/images/FileName.gif'

Below is an example of directory organization for an application with two themes basic and fancy.

  1. WebRoot/
  2. assets
  3. protected/
  4. .htaccess
  5. components/
  6. controllers/
  7. models/
  8. views/
  9. layouts/
  10. main.php
  11. site/
  12. index.php
  13. themes/
  14. basic/
  15. views/
  16. .htaccess
  17. layouts/
  18. main.php
  19. site/
  20. index.php
  21. fancy/
  22. views/
  23. .htaccess
  24. layouts/
  25. main.php
  26. site/
  27. index.php

In the application configuration, if we configure

  1. return array(
  2. 'theme'=>'basic',
  3. ......
  4. );

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.

  1. $this->widget('CLinkPager', array(
  2. 'pages'=>$pagination,
  3. 'maxButtonCount'=>5,
  4. 'cssFile'=>false,
  5. ));

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:

  1. return array(
  2. 'components'=>array(
  3. 'widgetFactory'=>array(
  4. 'widgets'=>array(
  5. 'CLinkPager'=>array(
  6. 'maxButtonCount'=>5,
  7. 'cssFile'=>false,
  8. ),
  9. 'CJuiDatePicker'=>array(
  10. 'language'=>'ru',
  11. ),
  12. ),
  13. ),
  14. ),
  15. );

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:

  1. $this->widget('CLinkPager', array(
  2. 'pages'=>$pagination,
  3. ));

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:

  1. $this->widget('CLinkPager', array(
  2. 'pages'=>$pagination,
  3. 'maxButtonCount'=>2,
  4. ));
    这个对pageSize无效
    参考:http://www.yiiframework.com/forum/index.php/topic/11510-cgridview-how-to-set-default-page-size/
  5.  
  6. http://danaluther.blogspot.com/2012/02/leveraging-widgets-widget-factory-and.html

YII 主题的更多相关文章

  1. yii主题

    修改应用的配置文件(protected/config/main.php)中加入 return array( ’theme’=>’basic’, ); 所有的视图文件必须位于views下 ,布局视 ...

  2. YII 主题设置

    节日不同,站点显示不同主题.就是解决问题. 也制作多套视图. 不是必需为全部页面设置主题,假设没有.就依照正常视图显示 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5 ...

  3. Yii 2.x 多主题 - 多语言 配置

    语言:只要在原来模板的位置建立语言目录 多主题:要重新定义模板的根目录

  4. PhpStorm 8.x/9.x 快捷键设置/个性化设置,如何多项目共存?如何更换主题?

    1."自定义"常用快捷键(设置成跟Eclipse差不多) 按照路径:File -> Settings -> Appearance & Behavior -> ...

  5. yii模块下面的组件

    模块的定义就不写了,直接进入主题看目录和文件: application/modules/client/controllers/UserController.php <?php class Use ...

  6. yii开发一个web程序的基本流程

    1. 创建目录结构.在前面的章节Creating First Yii Application写的yiic工具可以帮助我们快速完成这步. 2. 配置 application.就是修改applicatio ...

  7. Yii的学习(1)--安装配置

    之前在sina博客写过Yii的文章,来到博客园之后,没再写过关于Yii的文章,正好端午假期没啥事,就结合以前的博客,Yii的官方文档,再加上最近的关于Yii的收获总结一下,写个系列~~ Yii是一个基 ...

  8. yii和wp做博客

    第一步,安装yii和wp: 第二步,创建protected/components/ExceptionHandler.php文件 <?php class ExceptionHandler { pu ...

  9. yii 多模板

    main.php: //替换所有模板 //加载文件名为first的模板 //       'theme'=>'theme1', 'components'=>array(           ...

随机推荐

  1. JS为Select下拉框添加输入功能

    JavaScript使用parentNode.nextSibling.value实现的本功能,实际上你会发现网页上有两个控件元素,一个是Select,一个是input,使用CSS将input覆盖于se ...

  2. android开发之调试技巧 分类: android 学习笔记 2015-07-18 21:30 140人阅读 评论(0) 收藏

    我们都知道,android的调试打了断点之后运行时要使用debug as->android application 但是这样的运行效率非常低,那么我们有没有快速的方法呢? 当然有. 我们打完断点 ...

  3. jQuery 插件开发 笔记

    JQuery 插件开发: 类级别开发,开发新的全局函数 对象级别开发,给Jquery对象开发新方法 一.类级别开发 -定义全局方法 jQuery.foo = function() { alert('T ...

  4. 【Android】android镜像翻转

    Android镜像翻转指的是将屏幕进行水平的翻转,达到所有内容显示都会反向的效果,就像是在镜子中看到的界面一样.这种应用的使用场景相对比较受限,主要用在一些需要使用Android手机界面进行镜面投影的 ...

  5. codevs 3044 矩形面积求并 (扫描线)

    /* 之前一直偷懒离散化+暴力做着题 今天搞一下扫描线 自己按照线段树的一般写法写的有些问题 因为不用于以前的区间sum so 题解搬运者23333 Orz~ 去掉了打标记的过程 同时更新区间的时候先 ...

  6. 什么是php命名空间

    php命名空间是在5.3版本后加入的,命名空间反过来就是空间命名,在这里的空间命名就像window下的文件夹命名,命名空间用关键字namespace来定义.在这里用文件夹举三个例子,比如相对于test ...

  7. Index Full Scan vs Index Fast Full Scan-1103

    [Oracle] Index Full Scan vs Index Fast Full Scan作者:汪海 (Wanghai) 日期:14-Aug-2005 出处:http://spaces.msn. ...

  8. JDBC标准事物编程模式

    事物简介: 事物是一种数据库中保证交易可靠的机制,JDBC支持数据库中事物的概念,默认情况下事物是默认提交的. 事物的特性: 1.事物必须是原子工作单元,对于其数据的修改,要么都执行,要么都不执行2. ...

  9. 一种实现C++反射功能的想法(三)

    如何实现类型名跟类型的对应, 我们很容易想到map, 没错, 就是使用map实现的. std::map<std::string, .....>, 等下, 第二部分该填什么类型, 一个函数指 ...

  10. nginx location

    1. “= ”,字面精确匹配, 如果匹配,则跳出匹配过程.(不再进行正则匹配) 2. “^~ ”,最大前缀匹配,如果匹配,则跳出匹配过程.(不再进行正则匹配) 3. 不带任何前缀:最大前缀匹配,举例如 ...