17
Aug
10
Create a front end theme switcher
So you wanna make a demo site for your customers to show all your template skills in one site. A simple theme switcher can come in handy. It's quite easy to implement this.
First add two new functions to your Page_Controller :
The first function themeSwitcher() will scan the themes folder for themes and create a dropdown menu with all templates. The exclude array and array_diff will exclude the dots and Apple DS_Store files and Dreamweaver _notes folders. You could use this array to add themes you want to exclude in the switch.
The Session::get('theme') will get the selected theme which will be set in the second function. The last part creates the dropdown field and form.
The second function switchTheme($data) will process the form and set the selected theme as session data, so the theme change will be remembered when you navigate through your site. I added an if/else to unset the theme session variable. This is done to prevent errors when 'Select Theme' is selected, which returns 0. The default theme that is set in your _config.php will be used by default.
The Director rule will bring you back to the page where you selected a new theme.
The last bit of code will be placed in the function init() of your Page_Controller before the Requirements rules.
Since the dropdown field created by Silverstripe populate the option values with integers, we need to translate those integers with the actual theme name. That's why we scan the theme folder again.
First we get the selected theme from the session variable, check if it is set to prevent errors and set the new theme with SSViewer. Since the $themes is an array we only need to pick the selected folder name with $themes[$theme].
Now you only need to place the form somewhere in your templates by using the variable $themeSwitcher and you are able to change your themes with a simple form.
Note: remember to add $themeSwitcher in all your themes you have in your dropdown field.
About the Author
Name: Martijn van Nieuwenhoven
Website: http://www.mediavacature.nl
Martijn van Nieuwenhoven runs the largest Dutch Jobboard for Media, Marcom and Graphic related jobs. He also develops websites and applications for small business companies based in The Netherlands.
Comments (10)
-
I have added a css file trying to put this in line.
I want to remove the the word theme or be able to change it???
I would like the Switch button to the right of the dropdown. Any css help here please?Posted by Steve, 14/12/2009 9:31am (8 months ago)
-
Hey...That is really interesting and helpful post.
Thanks for sharing the idea.
Thanks,
Aslam MultaniPosted by Aslam Multani, 05/10/2009 12:53pm (10 months ago)
-
You should be able to that with just plain css?
Posted by Martijn, 27/08/2009 5:40am (11 months ago)
-
Thanks Martijn, I don't have a problem with making the modal box - I'm just not sure how to modify the form template - where is it?
Posted by paul bolger, 26/08/2009 8:43pm (11 months ago)
-
Hi Paul,
You could add some Literal fields to the form to add more information.
Google some Jquery + Modal Popup to find out how to make a Modal dialog box.Posted by Martijn, 26/08/2009 6:34am (11 months ago)
-
Thanks Martijn, that works well. I'm pretty new to Silverstripe - I'd like to change the actual form template (put the switcher into a modal popup box and include more explanation). Would you mind pointing me in the right direction? I've looked in every 'templates' directory I can find but it's not making a great deal of sense so far. I'm thinking making it into a module would be a good idea.
Posted by paul bolger, 26/08/2009 3:11am (11 months ago)
-
I solved this with adding a stylesheet in myssite/css. and add this in Page.php
Function init(){
Requirements::css("mysite/css/themeswitcher.css");
}Posted by Martijn, 24/08/2009 4:55am (11 months ago)
-
I got this working after a bit of mucking around. I don't quite understand how the form is being generated though. How would you go about adding css to affect the selection form in all the themes?
Posted by paul bolger, 23/08/2009 8:14pm (11 months ago)
-
This will come in handy. Thanks for posting this!
Posted by Ryan C., 21/08/2009 9:44am (11 months ago)
-
This is very useful. Thanks for posting. :)
Posted by Lamin Barrow, 19/08/2009 1:55am (11 months ago)
RSS feed for comments on this page RSS feed for all comments