目錄
 for a widget whose 類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 
3. 皮肤
首頁 後端開發 php教程 Yii框架官方指南系列44-專題:Theming(主題)

Yii框架官方指南系列44-專題:Theming(主題)

Feb 16, 2017 am 09:31 AM



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下。例如,如果我們要替換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目錄可能包含安全敏感數據,應當配置以防止被網路使用者存取。

當我們呼叫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 li .

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',
    ......
);
登入後複製

one under the directory 

themes/basic /views/layouts

, and the site's index view will use the one under

themes/basic/views/site. In case a view file is not found in the theme, it will fall back to the one undertec the proproftec views directory.1. 主題掛飾Starting from version 1.1.5, views used by a widget can also be themed. In particular, when tempweallCWidgetrwill the CWidgetr. search under theme folder as well as the widget view folder for the desired view file.To theme the view 

xyz

 for a widget whose 類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 

appwidgetsFoo

, weets create a fota. 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 endswephp under the newly created folder. To this end, we under the newly created folder. To this endsweweet 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.<p>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.</p> <p><br></p> <p></p> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">$this-&gt;widget('CLinkPager', array(     'pages'=&gt;$pagination,     'maxButtonCount'=&gt;5,     'cssFile'=&gt;false, ));</pre><div class="contentsignin">登入後複製</div></div> <p></p> <p>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.</p> <p>To use the global widget customization feature, we need to configure the widgetFactory as follows:</p> <p><br></p> <p></p> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">return array(     'components'=&gt;array(         'widgetFactory'=&gt;array(             'widgets'=&gt;array(                 'CLinkPager'=&gt;array(                     'maxButtonCount'=&gt;5,                     'cssFile'=&gt;false,                 ),                 'CJuiDatePicker'=&gt;array(                     'language'=&gt;'ru',                 ),             ),         ),     ), );</pre><div class="contentsignin">登入後複製</div></div> <p></p> <p>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.</p> <p>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:</p> <p><br></p> <p></p> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">$this-&gt;widget('CLinkPager', array(     'pages'=&gt;$pagination, ));</pre><div class="contentsignin">登入後複製</div></div> <p></p> <p>We can still override the initial property values when necessary. For example, if in some view we want to set<code>maxButtonCount 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(
    &#39;default&#39;=>array(
        'nextPageLabel'=>'&gt;&gt;',
        'prevPageLabel'=>'&lt;&lt;',
    ),
    '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.

如果一個小部件使用的皮膚不存在,Yii 仍然會照常創建該小部件,不會出現任何錯誤。

資訊: 使用皮膚可能會降低效能,因為 Yii 需要第一次查找皮膚檔案正在創建一個小部件。

皮膚與全局小部件自訂功能非常相似。主要區別如下。

  • 皮膚更多的是與展示屬性值的定制相關;

  • 一個widget可以有多個皮膚;

  • 皮膚是可主題化的;全局小部件定制貴。

  •  以上就是Yii框架官方指南系列44-專題:Theming(主題)的內容,更多相關內容請關注PHP中文網(www.php.cn)!


本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

Yii框架中的RESTful API開發 Yii框架中的RESTful API開發 Jun 21, 2023 pm 12:34 PM

Yii是一款基於PHP的高效能MVC框架,它提供了非常豐富的工具和功能,支援快速、有效率地開發Web應用程式。其中,Yii框架的RESTfulAPI功能得到了越來越多開發者的關注和喜愛,因為使用Yii框架可以非常方便地構建出高性能、易擴展的RESTful接口,為Web應用的開發提供了強有力的支持。 RESTfulAPI簡介RESTfulAPI是一種基於

Yii框架中間件:為應用程式提供多重資料儲存支持 Yii框架中間件:為應用程式提供多重資料儲存支持 Jul 28, 2023 pm 12:43 PM

Yii框架中間件:為應用程式提供多重資料儲存支援介紹中間件(middleware)是Yii框架中的重要概念,它為應用程式提供了多重資料儲存支援。中間件的作用類似於一個過濾器,它能夠在應用程式的請求和回應之間插入自訂程式碼。透過中間件,我們可以對請求進行處理、驗證、過濾,然後將處理後的結果傳遞給下一個中間件或最終的處理程序。 Yii框架中的中間件使用起來非常

PHP中如何使用Yii框架 PHP中如何使用Yii框架 Jun 27, 2023 pm 07:00 PM

隨著Web應用程式的快速發展,現代Web開發已成為一項重要技能。許多框架和工具可用於開發高效的Web應用程序,其中Yii框架就是一個非常流行的框架。 Yii是一個高效能、基於元件的PHP框架,它採用了最新的設計模式和技術,提供了強大的工具和元件,是建立複雜Web應用程式的理想選擇。在本文中,我們將討論如何使用Yii框架來建立Web應用程式。安裝Yii框架首先,

使用Yii框架實現網頁快取和頁面分塊的步驟 使用Yii框架實現網頁快取和頁面分塊的步驟 Jul 30, 2023 am 09:22 AM

使用Yii框架實現網頁快取和頁面分塊的步驟引言:在Web開發過程中,為了提升網站的效能和使用者體驗,常常需要對頁面進行快取和分塊處理。 Yii框架提供了強大的快取和佈局功能,可以幫助開發者快速實現網頁快取和頁面分塊,本文將介紹如何使用Yii框架進行網頁快取和頁面分塊的實作。一、網頁快取開啟網頁快取在Yii框架中,可以透過設定檔來開啟網頁快取。開啟主設定檔co

使用Yii框架創建遊戲攻略網站 使用Yii框架創建遊戲攻略網站 Jun 21, 2023 pm 01:45 PM

近年來,隨著遊戲產業的快速發展,越來越多的玩家開始尋找遊戲攻略來幫助遊戲過關。因此,創建遊戲攻略網站可以讓玩家更方便取得遊戲攻略,同時也能為玩家提供更好的遊戲體驗。在創建這樣一個網站時,我們可以使用Yii框架來進行開發。 Yii框架是一個基於PHP程式語言的Web應用開發框架。它具有高效、安全、擴展性強等特點,可以為我們更快速、高效地創建一個遊戲攻略

Yii框架中間件:為應用程式新增日誌記錄和偵錯功能 Yii框架中間件:為應用程式新增日誌記錄和偵錯功能 Jul 28, 2023 pm 08:49 PM

Yii框架中間件:為應用程式新增日誌記錄和偵錯功能【引言】在開發Web應用程式時,我們通常需要添加一些附加功能以提高應用程式的效能和穩定性。 Yii框架提供了中間件的概念,使我們能夠在應用程式處理請求之前和之後執行一些額外的任務。本文將介紹如何使用Yii框架的中間件功能來實作日誌記錄和除錯功能。 【什麼是中間件】中間件是指在應用程式處理請求之前和之後,對請求和回應做

在Yii框架中使用控制器(Controllers)處理Ajax請求的方法 在Yii框架中使用控制器(Controllers)處理Ajax請求的方法 Jul 28, 2023 pm 07:37 PM

在Yii框架中,控制器(Controllers)扮演著處理請求的重要角色。除了處理常規的頁面請求之外,控制器還可以用於處理Ajax請求。本文將介紹在Yii框架中處理Ajax請求的方法,並提供程式碼範例。在Yii框架中,處理Ajax請求可以透過以下步驟進行:第一步,建立一個控制器(Controller)類別。可以透過繼承Yii框架提供的基礎控制器類別yiiwebCo

Yii框架中的調試工具:分析和調試應用程式 Yii框架中的調試工具:分析和調試應用程式 Jun 21, 2023 pm 06:18 PM

在現代的Web應用程式開發中,調試工具是不可或缺的。它們可以幫助開發者找到和解決應用程式的各種問題。 Yii框架作為一款流行的Web應用程式框架,自然也提供了一些除錯工具。本文將重點介紹Yii框架中的調試工具,並討論它們如何幫助我們分析和調試應用程式。 GiiGii是Yii框架的程式碼產生器。它可以自動產生Yii應用程式的程式碼,如模型、控制器和視圖等。使用Gii,

See all articles