简体   繁体   中英

Yii: Theme support for components?

I was wondering if yii components are also supporting the theme feature? In my environment right now a component is only considering files within the component/views/ folder.
Now that I am also using themes it would be nice to tell the component to look for the view under the themes/themeName/ folder.

Using the method below I can work around this but it certainly doesn't feel like this is the yii-way to do it.

protected function renderContent()
{
    $view = './../../../themes/'.Yii::app()->theme->name.'/views/viewName';
    $this->render($view);
}  

Do you know of a more elegant solution to achieve this?

There isn't any theming on components. Mainly because they're not intended to be rendering content for anything. Nothing wrong with that though, sometimes it's required.

Easiest solution is probably to just make it more readable, using path aliases always helps:

protected function renderContent()
{
    $view = 'webroot.themes.'.Yii::app()->theme->name.'.views.viewName';
    $this->render($view);
}  

Or you could add a method the component, or extend CComponent to get it across all components if you want it:

public function getViewsPath(){
    return 'webroot.themes.'.Yii::app()->theme->name.'.views';
}

Or you could set a path alias:

Yii::setPathOfAlias('theme','webroot.themes.'.Yii::app()->theme->name);

Then you could use that anywhere in your application provided you run it at an early enough point in the process.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM