Class yii\base\Theme
| Inheritance | yii\base\Theme » yii\base\Component » yii\base\BaseObject |
|---|---|
| Implements | yii\base\Configurable |
| Available since version | 2.0 |
| Source Code | https://github.com/yiisoft/yii2/blob/master/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.
Public Properties
| Property | Type | Description | Defined By |
|---|---|---|---|
| $pathMap | array | The mapping between view directories and their corresponding themed versions. | yii\base\Theme |
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __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() | Converts a file to a themed file if possible. | 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() | Converts a relative file path into an absolute one using \yii\base\basePath. | yii\base\Theme |
| getUrl() | Converts a relative URL into an absolute URL using \yii\base\baseUrl. | 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 |
Property Details
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 \yii\base\basePath will be used.
Method Details
Converts a file to a themed file if possible.
If there is no corresponding themed file, the original file will be returned.
| public string applyTo ( $path ) | ||
| $path | string | The file to be themed |
| return | string | The themed file, or the original file if the themed version is not available. |
|---|---|---|
| throws | yii\base\InvalidConfigException | if \yii\base\basePath is not set |
See also $pathMap.
| public string getBasePath ( ) | ||
| return | string | The root path of this theme. All resources of this theme are located under this directory. |
|---|---|---|
| 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. |
|---|---|---|
Converts a relative file path into an absolute one using \yii\base\basePath.
| public string getPath ( $path ) | ||
| $path | string | The relative file path to be converted. |
| return | string | The absolute file path |
|---|---|---|
| throws | yii\base\InvalidConfigException | if \yii\base\basePath is not set |
Converts a relative URL into an absolute URL using \yii\base\baseUrl.
| public string getUrl ( $url ) | ||
| $url | string | The relative URL to be converted. |
| return | string | The absolute URL |
|---|---|---|
| throws | yii\base\InvalidConfigException | if \yii\base\baseUrl is not set |
See also $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. |