YII 主題

heming是一個在Web應用程序裏定製網頁外觀的系統方式。經過採用一個新的主題,網頁應用程序的總體外觀能夠當即和戲劇性的改變。php

在Yii,每一個主題由一個目錄表明,包含view文件,layout文件和相關的資源文件,如圖片, CSS文件, JavaScript文件等。主題的名字就是他的目錄名字。所有主題都放在在同一目錄WebRoot/themes下 。在任什麼時候候,只有一個主題能夠被激活。css

提示:默認的主題根目錄WebRoot/themes可被配置成其餘的。只須要配置themeManager應用部件的屬性basePathbaseUrl爲你所要的值。html

要激活一個主題,設置Web應用程序的屬性theme爲你所要的名字。能夠在application configuration中配置或者在執行過程當中在控制器的動做裏面修改。api

注:主題名稱是區分大小寫的。若是您嘗試啓動一個不存在的主題, 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 。app

注:因爲views目錄可能包含安全敏感數據,應當配置以防止被網絡用戶訪問。yii

當咱們調用renderrenderPartial顯示視圖,相應的view文件以及佈局文件將在當前激活的主題裏尋找。若是發現,這些文件將被render渲染。不然,就後退到viewPathlayoutPath 所指定的預設位置尋找。ide

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
相關文章
相關標籤/搜索