When View renders a view file, it will check the [[View::theme|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 [[View::theme|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.
/** * @inheritdoc */ public function init() { parent::init(); Yii::$app->assetManager->bundles['yii\\bootstrap\\BootstrapAsset'] = ['sourcePath' => '@vova07/themes/admin/assets', 'css' => ['css/bootstrap.min.css']]; Yii::$app->assetManager->bundles['yii\\bootstrap\\BootstrapPluginAsset'] = ['sourcePath' => '@vova07/themes/admin/assets', 'js' => ['js/bootstrap.min.js']]; Yii::$container->set('yii\\grid\\CheckboxColumn', ['checkboxOptions' => ['class' => 'simple']]); }
/** * * @return BaseTheme|boolean */ public function getTheme() { if ($this->_theme === false || $this->_theme instanceof BaseTheme) { return $this->_theme; } if ($this->active === null) { if (($id = Yii::$app->user->id) !== null) { $this->active = Yii::$app->cache->get([self::THEME_KEY, $id]); } else { $this->active = Yii::$app->request->cookies->getValue(self::THEME_KEY, false); } if ($this->active === false && $this->default !== null) { $this->active = $this->default; } } if (isset($this->themes[$this->active])) { $theme = $this->themes[$this->active]; if (is_string($theme) && strpos($theme, '\\') === false) { $theme = ['class' => BaseTheme::className(), 'pathMap' => [Yii::$app->getBasePath() => [$theme]]]; } elseif (is_array($theme) && !isset($theme['class'])) { $theme['class'] = BaseTheme::className(); } $this->_theme = $this->themes[$this->active] = Yii::createObject($theme); } else { $this->_theme = false; } return $this->_theme; }
public function applyTo($path) { return parent::applyTo($path); $pathMap = $this->pathMap; if (empty($pathMap)) { if (($basePath = $this->getBasePath()) === null) { throw new InvalidConfigException('The "basePath" property must be set.'); } $pathMap = [Yii::$app->getBasePath() => [$basePath]]; } $path = FileHelper::normalizePath($path); foreach ($pathMap as $from => $tos) { $from = FileHelper::normalizePath(Yii::getAlias($from)) . DIRECTORY_SEPARATOR; if (strpos($path, $from) === 0) { $n = strlen($from); foreach ((array) $tos as $to) { $to = FileHelper::normalizePath(Yii::getAlias($to)) . DIRECTORY_SEPARATOR; $file = $to . substr($path, $n); if (is_file($file)) { return $file; } } } } return $path; }
/** * @inheritdoc */ public function init() { parent::init(); Yii::$app->assetManager->bundles['yii\\bootstrap\\BootstrapAsset'] = ['css' => ['css/bootstrap.min.css']]; Yii::$app->assetManager->bundles['yii\\bootstrap\\BootstrapPluginAsset'] = ['js' => ['js/bootstrap.min.js']]; Yii::$app->assetManager->bundles['yii\\web\\JqueryAsset'] = ['js' => ['jquery.min.js']]; Yii::$app->assetManager->bundles['yii\\jui\\JuiAsset'] = ['js' => ['jquery-ui.min.js']]; }
/** * Getter for pathMap. */ public function init() { parent::init(); if (!is_array($this->pathMap)) { $this->pathMap = []; } $this->pathMap = $this->compilePathMap(ArrayHelper::merge(['$themedViewPaths' => $this->buildThemedViewPaths(), '$themedWidgetPaths' => '$themedViewPaths/widgets', Yii::$app->viewPath => '$themedViewPaths', __DIR__ . '/widgets/views' => '$themedWidgetPaths'], $this->getManager()->pathMap, $this->pathMap)); }
/** * @inheritDoc */ public function applyTo($path) { $ext = substr(strrchr($path, '.'), 1); if ($ext !== Yii::$app->getView()->defaultExtension) { $path = str_replace('.' . $ext, '.' . Yii::$app->getView()->defaultExtension, $path); } return parent::applyTo($path); }
/** * @inheritdoc */ public function init() { parent::init(); /*Yii::$container->set('yii\grid\CheckboxColumn', [ 'checkboxOptions' => [ 'class' => 'simple' ] ]);*/ }
public function init() { if (property_exists(\Yii::$app->controller, 'theme')) { $theme = \Yii::$app->controller->theme; $this->pathMap = array('@app/views' => '@wwwroot/themes/' . $theme); $this->baseUrl = '@www/themes/' . $theme; } parent::init(); }
public function init() { parent::init(); if (!isset($this->theme)) { $this->theme = 'default'; } $this->basePath = '@app/themes/' . $this->theme; $this->baseUrl = '@web/themes/' . $this->theme; $this->pathMap = ['@frontend/views' => '@app/themes/' . $this->theme]; }
/** * @throws InvalidConfigException */ public function init() { if (!empty($this->current)) { if (empty($this->themes)) { throw new InvalidConfigException('The "themes" property must be set.'); } $this->defaultLayout = null; $this->validateTheme($this->current); $this->changeTheme($this->current); } else { $this->setBasePath('@app'); } parent::init(); }
/** * @inheritdoc */ public function applyTo($path) { $autoPath = $this->autoFindModuleView($path); if ($autoPath !== null && file_exists($autoPath)) { return $autoPath; } // Web Resource e.g. image if (substr($path, 0, 5) === '@web/') { $themedFile = str_replace('@web', $this->getBasePath(), $path); if (file_exists($themedFile)) { return str_replace('@web', $this->getBaseUrl(), $path); } else { return $path; } } return parent::applyTo($path); }
/** * @inheritdoc */ public function init() { parent::init(); /* Yii::$app->assetManager->bundles['yii\bootstrap\BootstrapAsset'] = [ 'sourcePath' => '@alias/themes/site/assets', 'css' => [ 'css/bootstrap.min.css' ] ]; Yii::$app->assetManager->bundles['yii\bootstrap\BootstrapPluginAsset'] = [ 'sourcePath' => 'alias/themes/site/assets', 'js' => [ 'js/bootstrap.min.js' ] ]; */ }
public function init() { parent::init(); /* Yii::$app->assetManager->bundles['yii\bootstrap\BootstrapAsset'] = [ 'sourcePath' => '@common/themes/markito/assets', 'css' => [ 'css/style.less' ] ]; Yii::$app->assetManager->bundles['yii\bootstrap\BootstrapPluginAsset'] = [ 'sourcePath' => '@common/themes/site/assets', 'js' => [ 'js/bootstrap.min.js' ] ]; Yii::$container->set('yii\grid\CheckboxColumn', [ 'checkboxOptions' => [ 'class' => 'simple' ] ]);*/ }
public function init() { parent::init(); }
public function init() { parent::init(); // The path for images directly accessed using the img tag Yii::setAlias("@images", "@web/images"); }
/** * @inheritdoc */ public function init() { parent::init(); $this->assetManager->bundles['yii\\bootstrap\\BootstrapAsset'] = ['sourcePath' => '@app/themes/site/assets', 'css' => []]; $this->assetManager->bundles['yii\\bootstrap\\BootstrapPluginAsset'] = ['sourcePath' => '@app/themes/site/assets', 'js' => ['js/bootstrap.min.js']]; }
public function init() { parent::init(); $this->bodyClass = $this->options . ' ' . $this->skin . ' ' . $this->layout; }
/** * @inheritdoc */ public function init() { parent::init(); Yii::$app->assetManager->bundles['yii\\bootstrap\\BootstrapAsset'] = ['sourcePath' => '@vova07/themes/site', 'css' => ['css/bootstrap.min.css', 'js/jquery-ui-1.11.2/jquery-ui.css']]; Yii::$app->assetManager->bundles['yii\\bootstrap\\BootstrapPluginAsset'] = ['sourcePath' => '@vova07/themes/site', 'js' => ['js/bootstrap.min.js', 'js/jquery-ui-1.11.2/jquery-ui.js', 'https://rawgit.com/dinbror/bpopup/master/jquery.bpopup.min.js']]; }
/** * @inheritdoc */ public function init() { parent::init(); Yii::$app->assetManager->bundles['yii\\bootstrap\\BootstrapAsset'] = ['sourcePath' => '@vova07/themes/admin', 'css' => ['css/bootstrap.min.css', 'js/jquery-ui-1.11.2/jquery-ui.css']]; Yii::$app->assetManager->bundles['yii\\bootstrap\\BootstrapPluginAsset'] = ['sourcePath' => '@vova07/themes/admin', 'js' => ['js/bootstrap.min.js', 'js/jquery-ui-1.11.2/jquery-ui.js', 'js/custom.js']]; }
public function init() { parent::init(); Yii::$container->set('yii\\grid\\CheckboxColumn', ['checkboxOptions' => ['class' => 'simple']]); $this->registerTranslations(); }