Yii:: Change Theme Dynamically / On the Fly

Posted: April 21, 2012 in Php, Yii Framework

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);
        }

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;”>
                        &nbsp;&nbsp;
                        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’,
                    ),
),

 

OK THATS ALL.. Hope your code success..

 

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s