Theming

I want my site to be able to use multiple themes. I checked the documentation, cookbook and some other resources but the information that i found was either vague or not helpful at all regarding this matter.

inside testapp/protected/themes

i see a folder called classic which i assume is a theme, i made a copy of the classic folder inside the theme directory and renamed it to bluebird,

so far i think those should be 2 themes that the application could possibly use…

i then copied the contents of testapp/protected/views to the views folder inside both classic and bluebird… then deleted the content from testapp/protected/views…

i then edited protected/config/main.php as follows


return array(

	'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',

	'name'=>'YiiSNS',

	'defaultController'=>'site',

	'charset'=>'utf-8',

	'language'=>'en_us',

	'theme'=>'classic',..........................

		'themeManager'=>array(

			'basePath'=>dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'..',

			'baseUrl'=>'/themes',

		),.........

);

i then tried to access the site but then got this error


CException

Description


SiteController cannot find the requested view "index".

Source File


C:\wamp\www\yii\framework\web\CController.php(710)

Could someone please tell me what am i doing wrong?

i am not sure about the details as im still new to yii, but im pretty sure yii is still looking for the file in

testapp/protected/views/[site]/index.php in order to display the homepage content.

yii uses the views files as the files that make up the visual parts of the pages content.

i think you do still need the views folder even if you use themes.

here ill debug your error message:

CException

Description

SiteController cannot find the requested view "index".

:testapp/protected/controllers/SiteController.php can’t find the file testapp/protected/views/[site]/index.php

Source File

C:\wamp\www\yii\framework\web\CController.php(710)

:in the file called CController.php (ControllerController i assume)

The view folder is still present in the protected folder, It’s just that i moved the php files to /themes/themename/views

If what you say is the case, then my question should be how do i get yii to search the themes folder for view files instead of the protected/views folder…

(PS. i think the C before the class names stands for class like CController means ClassController :) ).

that i do not know, have myself been trying to figure out, eventually went with a master layout file in the protected/views/ folder that contains all the other view files.

Thank you anyway. After searching through the core files i got to understand what was going on and got it to work.

could you show me your source code about use Multiple Theme ?

I haven’t actually implemented it yet so i cant show you the source but i can give you a pretty good idea of what to do…

first thing create a child of CController in your components directory so you should have something like this




class BaseController extends CController

{

  public funtion init()

  {

    $thid->_setTheme();

  }

  public funtion _setTheme()

  {

    $defaultTheme = 'classic';


    if(isset($_POST['theme']))

    {

      $theme = $_POST['theme'];

      //set theme cookie to $theme here ?

    }else if($_COOKIE['theme']))

    {

      $theme = $_COOKIE['theme'];

    }else{

      $theme = $defaultTheme;

    }

    Yii::app()->setTheme($theme);

  }

}



and make all your controllers children of BaseController like




class SiteController extends BaseController

{

  public function actionIndex()

  {


  }

}

copy the contents of protected/views to themes/classic/views

then you can remove the files from protected/views but i don’t recommend removing the actual folders

you can then make the default theme classic, then you can make copies of classic and rename them to represent other themes…

That’s not elegant or tested code but it should give you an idea of what to do. It’s not necessary to set the theme property in the config file unless you really just want to use 1 theme.

If you are having trouble just ask and i’ll try to give a better explanation.