Class yii\base\Theme
继承 | yii\base\Theme » yii\base\Component » yii\base\BaseObject |
---|---|
实现 | yii\base\Configurable |
可用版本自 | 2.0 |
源码 | https://github.com/yiichina/yii2/blob/api/framework/base/Theme.php |
Theme represents an application theme.
When yii\base\View renders a view file, it will check the active theme to see if there is a themed version of the view file exists. If so, the themed version will be rendered instead.
A theme is a directory consisting of view files which are meant to replace their non-themed counterparts.
Theme uses $pathMap to achieve the view file replacement:
- It first looks for a key in $pathMap that is a substring of the given view file path;
- If such a key exists, the corresponding value will be used to replace the corresponding part in the view file path;
- It will then check if the updated view file exists or not. If so, that file will be used to replace the original view file.
- If Step 2 or 3 fails, the original view file will be used.
For example, if $pathMap is ['@app/views' => '@app/themes/basic']
,
then the themed version for a view file @app/views/site/index.php
will be
@app/themes/basic/site/index.php
.
It is possible to map a single path to multiple paths. For example,
'pathMap' => [
'@app/views' => [
'@app/themes/christmas',
'@app/themes/basic',
],
]
In this case, the themed version could be either @app/themes/christmas/site/index.php
or
@app/themes/basic/site/index.php
. The former has precedence over the latter if both files exist.
To use a theme, you should configure the theme property of the "view" application component like the following:
'view' => [
'theme' => [
'basePath' => '@app/themes/basic',
'baseUrl' => '@web/themes/basic',
],
],
The above configuration specifies a theme located under the "themes/basic" directory of the Web folder that contains the entry script of the application. If your theme is designed to handle modules, you may configure the $pathMap property like described above.
For more details and usage information on Theme, see the guide article on theming.
公共属性
属性 | 类型 | 描述 | 被定义在 |
---|---|---|---|
$basePath | string | 这个主题的根路径。 此主题的所有资源都位于此目录下。 | yii\base\Theme |
$baseUrl | string | The base URL (without ending slash) for this theme. | yii\base\Theme |
$behaviors | yii\base\Behavior[] | List of behaviors attached to this component | yii\base\Component |
$pathMap | array | The mapping between view directories and their corresponding themed versions. | yii\base\Theme |
公共方法
方法 | 描述 | 被定义在 |
---|---|---|
__call() | Calls the named method which is not a class method. | yii\base\Component |
__clone() | This method is called after the object is created by cloning an existing one. | yii\base\Component |
__construct() | Constructor. | yii\base\BaseObject |
__get() | Returns the value of a component property. | yii\base\Component |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\base\Component |
__set() | Sets the value of a component property. | yii\base\Component |
__unset() | Sets a component property to be null. | yii\base\Component |
applyTo() | 如果可能,将文件转换为主题文件。 如果没有对应的主题文件,将返回原始文件。 | yii\base\Theme |
attachBehavior() | Attaches a behavior to this component. | yii\base\Component |
attachBehaviors() | Attaches a list of behaviors to the component. | yii\base\Component |
behaviors() | Returns a list of behaviors that this component should behave as. | yii\base\Component |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\base\Component |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\base\Component |
className() | Returns the fully qualified name of this class. | yii\base\BaseObject |
detachBehavior() | Detaches a behavior from the component. | yii\base\Component |
detachBehaviors() | Detaches all behaviors from the component. | yii\base\Component |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\base\Component |
getBasePath() | yii\base\Theme | |
getBaseUrl() | yii\base\Theme | |
getBehavior() | Returns the named behavior object. | yii\base\Component |
getBehaviors() | Returns all behaviors attached to this component. | yii\base\Component |
getPath() | 使用 $basePath 将相对文件路径转换为绝对路径。 | yii\base\Theme |
getUrl() | 使用 $baseUrl 将相对 URL 转换为绝对 URL。 | yii\base\Theme |
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | yii\base\Component |
hasMethod() | Returns a value indicating whether a method is defined. | yii\base\Component |
hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\base\Component |
init() | Initializes the object. | yii\base\BaseObject |
off() | Detaches an existing event handler from this component. | yii\base\Component |
on() | Attaches an event handler to an event. | yii\base\Component |
setBasePath() | yii\base\Theme | |
setBaseUrl() | yii\base\Theme | |
trigger() | Triggers an event. | yii\base\Component |
属性详情
这个主题的根路径。 此主题的所有资源都位于此目录下。
The base URL (without ending slash) for this theme. All resources of this theme are considered to be under this base URL.
The mapping between view directories and their corresponding themed versions. This property is used by applyTo() when a view is trying to apply the theme. Path aliases can be used when specifying directories. If this property is empty or not set, a mapping yii\base\Application::$basePath to $basePath will be used.
方法详情
如果可能,将文件转换为主题文件。 如果没有对应的主题文件,将返回原始文件。
public string applyTo($path) | ||
$path | string | 要转换为主题的文件 |
return | string | 主题文件,如果主题版本不可用,则为原始文件。 |
---|---|---|
throws | yii\base\InvalidConfigException | 如果未设置 $basePath |
参见 $pathMap.
public string getBasePath() | ||
return | string | 这个主题的根路径。 此主题的所有资源都位于此目录下。 |
---|
public string getBaseUrl() | ||
return | string | The base URL (without ending slash) for this theme. All resources of this theme are considered to be under this base URL. |
---|
使用 $basePath 将相对文件路径转换为绝对路径。
public string getPath($path) | ||
$path | string | 要转换的相对文件路径。 |
return | string | 绝对文件路径 |
---|---|---|
throws | yii\base\InvalidConfigException | 如果未设置 $basePath |
使用 $baseUrl 将相对 URL 转换为绝对 URL。
public string getUrl($url) | ||
$url | string | 要转换的相对 URL。 |
return | string | 绝对 URL |
---|---|---|
throws | yii\base\InvalidConfigException | 如果未设置 $baseUrl |
参见 $pathMap.
public void setBasePath($path) | ||
$path | string | The root path or path alias of this theme. All resources of this theme are located under this directory. |
public void setBaseUrl($url) | ||
$url | string | The base URL or path alias for this theme. All resources of this theme are considered to be under this base URL. |