Multiple themes for symfony application
Hi,
After a long time i am posting my experience. This post describes the ways to implement multiple themes/skin in symfony application.I found many posts in symfony forum regarding themes and somebody suggested to use this plugin ysfDimensionsPlugin . This is really a great plugin and thanks to Yahoo for providing such a great plugin.There is no automatic installation for this plugin as it replaces 2 symfony core files. To install this plugin first you need to get the Tar file then extract it to your symfony application’s plugin directory.
Now take a back up of the files 2files by running these command. (path is specific to my system)
- cp /usr/local/lib/php/symfony/config/sfLoader.class.php /usr/local/lib/php/symfony/config/sfLoader.class.php.original
- cp /usr/local/lib/php/data/symfony/config/constants.php /usr/local/lib/php/data/symfony/config/constants.php.original
Now go to your application plugin directory
- cd /path/to/your/symfony/application/plugin/directory
- cp lib/config/sfLoader.class.php /usr/local/lib/php/symfony/config/sfLoader.class.php
- cp config/constants.php /usr/local/lib/php/data/symfony/config/constants.php
Aftre this you need to add the following code to symfony project’s config.php
$culture = if(!empty($_REQUEST['culture'])) ? $_REQUEST['culture'] : 'en';
$theme = if(!empty($_REQUEST['theme'])) ? $_REQUEST['theme'] : 'classic';// define dimensions
$dimension = array('culture' => $culture, 'theme' => $theme);
// no autoloading available this early in bootstrap, so require absolutely
require_once(dirname(__FILE__).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'plugins'.DIRECTORY_SEPARATOR.'ysfDimensionsPlugin'.DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'sfDimensions.class.php');
// set dimensions
sfDimensions::setDimension($dimension);
Now create a dimentions.yml in the project config. this will list all the possible themes you will have for an application
dimensions: culture: [en, fr, it, de] theme: [classic, corporate]
Now you are ready to play with this plugin.
How it works: This plugin takes the advantage of symfony configuration features and allows you to configure three parts of application (configuration, templates, actions) . Now i will tell you how to configure the configuration part and template part for different themes.
For example you have 3 themes ( 3 different layout.php files) for your application. place those files in the following way.
- apps/frontend/templates/theme1/layout.php
- apps/frontend/templates/theme2/layout.php
- apps/frontend/templates/theme3/layout.php
As every theme has its own css/js files you need to tell the application which one to pick in the following way.
- apps/frontend/config/theme1/view.yml
- apps/frontend/config/theme2/view.yml
- apps/frontend/config/theme3/view.yml
Now you have defined a seperate view.yml for each theme. Now you are done with multiple themes for single application. now clear your cache and test the output by passing a theme parameter in query string
- http://localhost/frontend_dev.php/?theme=theme1
- http://localhost/frontend_dev.php/?theme=theme2
- http://localhost/frontend_dev.php/?theme=theme3
That’s it… you got it…
This post only gives a brief info to start with this plugin. There are lots of customization you can to using this plugin. for example you can have culture based themes as well. Please check the plugin trac to know more about the plugin.
–
Asif
