W3cubDocs

/Yii 2.0

Class yii\base\Theme

Inheritance yii\base\Theme » yii\base\Component » yii\base\Object
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:

  1. It first looks for a key in $pathMap that is a substring of the given view file path;
  2. If such a key exists, the corresponding value will be used to replace the corresponding part in the view file path;
  3. 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.
  4. 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
$basePath string The root path of this theme. 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

Public Methods

Method Description Defined By
__call() Calls the named method which is not a class method. yii\base\Object
__clone() This method is called after the object is created by cloning an existing one. yii\base\Component
__construct() Constructor. yii\base\Object
__get() Returns the value of an object property. yii\base\Object
__isset() Checks if a property is set, i.e. defined and not null. yii\base\Object
__set() Sets value of an object property. yii\base\Object
__unset() Sets an object property to null. yii\base\Object
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\Object
canSetProperty() Returns a value indicating whether a property can be set. yii\base\Object
className() Returns the fully qualified name of this class. yii\base\Object
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 $basePath. yii\base\Theme
getUrl() Converts a relative URL into an absolute URL using $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\Object
hasProperty() Returns a value indicating whether a property is defined. yii\base\Object
init() Initializes the object. yii\base\Object
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

$basePath public property

The root path of this theme. All resources of this theme are located under this directory.

public string getBasePath ( )
public void setBasePath ( $path )

$baseUrl public property

The base URL (without ending slash) for this theme. All resources of this theme are considered to be under this base URL.

public string getBaseUrl ( )
public void setBaseUrl ( $url )

$pathMap public property

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 array $pathMap = null

Method Details

applyTo() public method

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 $basePath is not set

getBasePath() public method

See also $pathMap.

public string getBasePath ( )
return string

The root path of this theme. All resources of this theme are located under this directory.

getBaseUrl() public method

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.

getPath() public method

Converts a relative file path into an absolute one using $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 $basePath is not set

getUrl() public method

Converts a relative URL into an absolute URL using $baseUrl.

public string getUrl ( $url )
$url string

The relative URL to be converted.

return string

The absolute URL

throws yii\base\InvalidConfigException

if $baseUrl is not set

setBasePath() public method

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.

setBaseUrl() public method

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.

© 2008–2017 by Yii Software LLC
Licensed under the three clause BSD license.
http://www.yiiframework.com/doc-2.0/yii-base-theme.html