change theme in yii framework

Step to change the YII PHP framework theme………….

1. Add this code to protected/components/Controller.php

public $breadcrumbs=array();

public function beforeAction($action)
{
$dynamicTheme = ‘classic’;

if(isset($_POST[‘mytheme’]))
{
$dynamicTheme = $_POST[‘mytheme’];
//Yii::app()->request->cookies[‘dynamicTheme’] = new CHttpCookie(‘dynamicTheme’, $dynamicTheme);
$cookie = new CHttpCookie(‘dynamicTheme’, $dynamicTheme);
$cookie->expire = time()+60*60*24*180;
Yii::app()->request->cookies[‘dynamicTheme’] = $cookie;
}

if(isset(Yii::app()->request->cookies[‘dynamicTheme’]->value))
{
$dynamicTheme = Yii::app()->request->cookies[‘dynamicTheme’]->value;
}
else
{
//Yii::app()->request->cookies[‘dynamicTheme’] = new CHttpCookie(‘dynamicTheme’, $dynamicTheme);
$cookie = new CHttpCookie(‘dynamicTheme’, $dynamicTheme);
$cookie->expire = time()+60*60*24*180;
Yii::app()->request->cookies[‘dynamicTheme’] = $cookie;
}

//$dynamicTheme = (isset(Yii::app()->request->cookies[‘dynamicTheme’]->value)) ? Yii::app()->request->cookies[‘dynamicTheme’]->value : ‘classic’;
Yii::app()->theme=$dynamicTheme;

return parent::beforeAction($action);
}

2. Add this code anywhere you want. This code for select list of theme:

<form name=”form1″ id=”form1″ method=’post’ action=”” style=”margin:0;padding:0;”>

Theme:
params[‘arrTheme’];
foreach($arrTheme as $value=>$display)
{
$dynamicTheme = (isset(Yii::app()->request->cookies[‘dynamicTheme’]->value)) ? Yii::app()->request->cookies[‘dynamicTheme’]->value : ”;
$s = ”;
if($value == $dynamicTheme) $s = ‘selected’;
echo “$display”;
}
?>

3. Add theme list parameter on your protected/config/main.php

‘params’=>array(
// this is used in contact page
‘adminEmail’=>’webmaster@example.com’,
’arrTheme’=>array(
’classic’=>’Default’,
’white’=>’White’,
),
),

Comments

comments