


Yii Framework Official Guide Series 44 - Topic: Theming (theme)
Theming is a systematic way to customize the appearance of web pages in web applications. By adopting a new theme, the overall look and feel of a web application can be changed instantly and dramatically.
In Yii, each theme is represented by a directory, including view files, layout files and related resource files, such as images, CSS files, JavaScript files, etc. The name of the theme is its directory name. All themes are placed in the same directory WebRoot/themes
. At any time, only one theme can be active.
Tip: The default theme root directory
WebRoot/themes
can be configured to other values. Just configure the properties basePath and baseUrl of the themeManager application component to the values you want.
To activate a theme, set the Web application's theme property to the name you want. It can be configured in the application configuration or modified in the controller action during execution.
Note: Topic names are case-sensitive. If you try to start a theme that does not exist,
yii:
:app()->theme
will returnnull
.
The content in the theme directory is organized in the same way as the application base path directory. For example, all view files must be located under views
, layout view files under views/layouts
, and system view files under views/system
. For example, if we want to replace the create
view file of PostController
with the classic
theme, we will save the new view file as WebRoot/themes/classic /views/post/create.php
.
For the controller view files in the module, the corresponding theme view files will be placed in the views
directory. For example, if the above PostController
is in a module named forum
, we should save the create
view file as WebRoot/themes/classic/ views/forum/post/create.php
. If the forum
module is nested in another module named support
, then the view file should be WebRoot/themes/classic/views/support/forum/post/create. php
.
Note: Since the
views
directory may contain security-sensitive data, it should be configured to prevent access by network users.
When we call render or renderPartial to display a view, the corresponding view file and layout file will be found in the currently activated theme. If found, these files will be rendered. Otherwise, it will fall back to the default location specified by viewPath and layoutPath.
Below is an example of directory organization for an application with two themesbaseurl attribute, we can generate the following url for this image file,
yii">Copy after loginTips: In the view of a theme, we often need to link other theme resource files. For example, we might want to display an image file in the
images
directory under the theme. Using the baseurl attribute of the currently activated theme, we can generate the following url for this image file,
##
yii: :app()->theme->baseUrl . '/images/FileName.gif'Copy after login
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
return array( 'theme'=>'basic', ...... );
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 under
themes/basic/views/site. In case a view file is not found in the theme, it will fall back to the one under the
protected/views directory.
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 named
app_widgets_Foo. That is, we replace the namespace separators with the underscore characters.
xyz.php under the newly created folder. To this end, we should have a file
themes/basic/views/Foo/xyz.php, which will be used by the widget to replace its original view, if the currently active theme is
basic.
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)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Yii is a high-performance MVC framework based on PHP. It provides a very rich set of tools and functions to support the rapid and efficient development of web applications. Among them, the RESTful API function of the Yii framework has attracted more and more attention and love from developers, because using the Yii framework can easily build high-performance and easily scalable RESTful interfaces, providing strong support for the development of web applications. . Introduction to RESTfulAPI RESTfulAPI is a

Yii framework middleware: providing multiple data storage support for applications Introduction Middleware (middleware) is an important concept in the Yii framework, which provides multiple data storage support for applications. Middleware acts like a filter, inserting custom code between an application's requests and responses. Through middleware, we can process, verify, filter requests, and then pass the processed results to the next middleware or final handler. Middleware in the Yii framework is very easy to use

With the rapid development of web applications, modern web development has become an important skill. Many frameworks and tools are available for developing efficient web applications, among which the Yii framework is a very popular framework. Yii is a high-performance, component-based PHP framework that uses the latest design patterns and technologies, provides powerful tools and components, and is ideal for building complex web applications. In this article, we will discuss how to use Yii framework to build web applications. Install Yii framework first,

Steps to implement web page caching and page chunking using the Yii framework Introduction: During the web development process, in order to improve the performance and user experience of the website, it is often necessary to cache and chunk the page. The Yii framework provides powerful caching and layout functions, which can help developers quickly implement web page caching and page chunking. This article will introduce how to use the Yii framework to implement web page caching and page chunking. 1. Turn on web page caching. In the Yii framework, web page caching can be turned on through the configuration file. Open the main configuration file co

In recent years, with the rapid development of the game industry, more and more players have begun to look for game strategies to help them pass the game. Therefore, creating a game guide website can make it easier for players to obtain game guides, and at the same time, it can also provide players with a better gaming experience. When creating such a website, we can use the Yii framework for development. The Yii framework is a web application development framework based on the PHP programming language. It has the characteristics of high efficiency, security, and strong scalability, and can help us create a game guide more quickly and efficiently.

Yii framework middleware: Add logging and debugging capabilities to applications [Introduction] When developing web applications, we usually need to add some additional features to improve the performance and stability of the application. The Yii framework provides the concept of middleware that enables us to perform some additional tasks before and after the application handles the request. This article will introduce how to use the middleware function of the Yii framework to implement logging and debugging functions. [What is middleware] Middleware refers to the processing of requests and responses before and after the application processes the request.

In the Yii framework, controllers play an important role in processing requests. In addition to handling regular page requests, controllers can also be used to handle Ajax requests. This article will introduce how to handle Ajax requests in the Yii framework and provide code examples. In the Yii framework, processing Ajax requests can be carried out through the following steps: The first step is to create a controller (Controller) class. You can inherit the basic controller class yiiwebCo provided by the Yii framework

In modern web application development, debugging tools are indispensable. They help developers find and solve various problems with their applications. As a popular web application framework, the Yii framework naturally provides some debugging tools. This article will focus on the debugging tools in the Yii framework and discuss how they help us analyze and debug applications. GiiGii is a code generator for the Yii framework. It can automatically generate code for Yii applications, such as models, controllers, views, etc. Using Gii,
