So, plan to change themes dynamically on your page with Yii?? Here is what i do.
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);
}
{
$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=”<?php echo CHtml::normalizeUrl(array(‘index’))?>” style=”margin:0;padding:0;”>
Theme: <SELECT name=’mytheme’ onchange=”Javascript:document.form1.submit()”>
<?php
$arrTheme = Yii::app()->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 “<option value=’$value’ $s>$display</option>”;
}
?>
</SELECT>
</form>
Theme: <SELECT name=’mytheme’ onchange=”Javascript:document.form1.submit()”>
<?php
$arrTheme = Yii::app()->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 “<option value=’$value’ $s>$display</option>”;
}
?>
</SELECT>
</form>
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’,
),
),
// this is used in contact page
‘adminEmail’=>’webmaster@example.com’,
‘arrTheme’=>array(
‘classic’=>’Default’,
‘white’=>’White’,
),
),
OK THATS ALL.. Hope your code success..
No comments:
Post a Comment