Theme
API reference › yii › base › Theme
Theme represents an application theme.
When 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
$pathMapthat 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.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$basePath | string | The root path of this theme. | Theme |
$baseUrl | string| | The base URL (without ending slash) for this theme. | Theme |
$behaviors read-only | array<Behavior<static>> | List of behaviors attached to this component. | Component |
$pathMap | array| | The mapping between view directories and their corresponding themed versions. Default: null. | Theme |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | BaseObject |
__call() | Calls the named method which is not a class method. | Component |
__clone() | This method is called after the object is created by cloning an existing one. | Component |
__get() | Returns the value of a component property. | Component |
__isset() | Checks if a property is set, i.e. defined and not null. | Component |
__set() | Sets the value of a component property. | Component |
__unset() | Sets a component property to be null. | Component |
applyTo() | Converts a file to a themed file if possible. | Theme |
attachBehavior() | Attaches a behavior to this component. | Component |
attachBehaviors() | Attaches a list of behaviors to the component. | Component |
behaviors() | Returns a list of behaviors that this component should behave as. | Component |
canGetProperty() | Returns a value indicating whether a property can be read. | Component |
canSetProperty() | Returns a value indicating whether a property can be set. | Component |
className() static | Returns the fully qualified name of this class. | BaseObject |
detachBehavior() | Detaches a behavior from the component. | Component |
detachBehaviors() | Detaches all behaviors from the component. | Component |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | Component |
getBasePath() | Theme | |
getBaseUrl() | Theme | |
getBehavior() | Returns the named behavior object. | Component |
getBehaviors() | Returns all behaviors attached to this component. | Component |
getPath() | Converts a relative file path into an absolute one using $basePath. | Theme |
getUrl() | Converts a relative URL into an absolute URL using $baseUrl. | Theme |
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | Component |
hasMethod() | Returns a value indicating whether a method is defined. | Component |
hasProperty() | Returns a value indicating whether a property is defined for this component. | Component |
init() | Initializes the object. | BaseObject |
off() | Detaches an existing event handler from this component. | Component |
on() | Attaches an event handler to an event. | Component |
setBasePath() | Theme | |
setBaseUrl() | Theme | |
trigger() | Triggers an event. | Component |
Property details
Section titled “Property details”$basePath
Section titled “$basePath ”public string $basePathThe root path of this theme.
All resources of this theme are located under this directory.
$baseUrl
Section titled “$baseUrl ”public string|null $baseUrlThe base URL (without ending slash) for this theme.
All resources of this theme are considered to be under this base URL.
$pathMap
Section titled “$pathMap ”public array|null $pathMap = nullThe 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 Application::$basePath to $basePath will be used.
Method details
Section titled “Method details”applyTo()
Section titled “applyTo() ”public function applyTo(string $path): stringConverts a file to a themed file if possible.
If there is no corresponding themed file, the original file will be returned.
Parameters
| Parameter | Type | Description |
|---|---|---|
$path | string | The file to be themed |
Returns
string: The themed file, or the original file if the themed version is not available.
Throws
InvalidConfigException: if$basePathis not set
getBasePath()
Section titled “getBasePath() ”public function getBasePath(): stringReturns
string: The root path of this theme. All resources of this theme are located under this directory.
See also
getBaseUrl()
Section titled “getBaseUrl() ”public function getBaseUrl(): string|nullReturns
string|null: The base URL (without ending slash) for this theme. All resources of this theme are considered to be under this base URL.
getPath()
Section titled “getPath() ”public function getPath(string $path): stringConverts a relative file path into an absolute one using $basePath.
Parameters
| Parameter | Type | Description |
|---|---|---|
$path | string | The relative file path to be converted. |
Returns
string: The absolute file path
Throws
InvalidConfigException: if$basePathis not set
getUrl()
Section titled “getUrl() ”public function getUrl(string $url): stringConverts a relative URL into an absolute URL using $baseUrl.
Parameters
| Parameter | Type | Description |
|---|---|---|
$url | string | The relative URL to be converted. |
Returns
string: The absolute URL
Throws
InvalidConfigException: if$baseUrlis not set
setBasePath()
Section titled “setBasePath() ”public function setBasePath(string $path): mixedParameters
| Parameter | Type | Description |
|---|---|---|
$path | string | The root path or path alias of this theme. All resources of this theme are located under this directory. |
Returns
mixed
See also
setBaseUrl()
Section titled “setBaseUrl() ”public function setBaseUrl(string|null $url): mixedParameters
| Parameter | Type | Description |
|---|---|---|
$url | string| | The base URL or path alias for this theme. All resources of this theme are considered to be under this base URL. |
Returns
mixed