Creative Web Specialist - An online portfolio and resource site of Warren Buckley

Latest Posts

May31th

A CSS Style Switcher for Umbraco CMS

Author
Warren Buckley

Hello all,
Well hopefully this will be a useful solution to the Umbraco CMS community. I have worked on a CSS style switcher that allows the user to change the look of your site with a different CSS stylesheet. For example you may have two coloured themes that you want your user to be able to decide which they want to use when browsing your site.

This solution requires two files, a VB.NET usercontrol and an XSLT file.

The VB.NET file checks if the style querystring exists if so it creates the cookie of StyleSwitch with the value from the style querystring otherwise it sets a default value. If your intrested take a look at the code in usercontrol, it is heavily commented and should be easy to understand.

The XSLT does the important part of loading the correct stylesheet depending on the cookie value. Using an XSL:Choose statement with the umbraco.library we are able to read the cookie value and load diffferent stylesheets depending on the cookie value.

This solution is very flexible as it is also possible to load in different JavaScript files as well to compliment the CSS stylesheets. For example I plan to use this style switcher in a future release of my Creative Website Wizard Package for Umbraco to load in different layouts and themes. However just simply changing the CSS will not change the font colour used in sIFR3 as this is set with a JavaScript config file. So I will need to load a new JavaScript file along with the CSS file when doing the theme switches.

If you want to add more styles to the switcher just simply add another XSL:When statement and follow by example, it is fairly straight forward, but if you have any problems or questions about using this please leave a comment and I will help you out.


Note: Kudos to John Hodgkinson to help me refine this. The package was created with Per Ploug Hansen’s v0.4 package creator and as usual rename .zip to .umb

How to install and use

  1. Goto Developers section and right click Macros and select Import Package
  2. Follow the wizard to install the package.
  3. After installing close the wizard window and goto the developers section again to see the new changes
  4. The installer has installed two macros one called [XSLT] CSS Switcher and [Usercontrol] CSS Switcher Cookie and uploads the relevant files for these macros.
  5. Goto XSLT and open the CSSSwitcher.xslt
  6. Ammend the paths to the CSS and remove the text about “I am using style one” etc….
  7. Insert both macros into the head section of your HTML document in the template/s
  8. Last step is to create links to allow your visitor to switch styles - see the example below

Example of using switch style links
<ul>
<li><a href=”?style=default” mce_href=”?style=default”>Default style</a></li>
<li><a href=”?style=1″ mce_href=”?style=1″>Style one</a></li>
<li><a href=”?style=2″ mce_href=”?style=2″>Style two</a></li>
</ul>

Finally any comments, feedback or concerns please leave a comment and I will get back to you.
Thanks,
Warren

11 comments
15th May, 2008
Thanks Warren, that helps heaps.

About your package, it’s just the launching pad I need.
15th May, 2008
I have just checked out your site Lola and to me I can see any visual changes when changins stylesheets or themes.

Warren
04th February, 2009
Hi, does this work in umbraco 4? I'm just getting a runtime error when i insert the macro
04th February, 2009
If I can expand on that, the actual error is:

Value cannot be null.
Parameter name: url
04th February, 2009
Hi Garry this is very unlikely this will work for V4. The package XML structure has changed and this most likely why it has failed.

Check the macros are in the developer section.

Additionally check the XSLT and .NET usercontrol was copied if not.

Unzip the package file and manually put them in the correct locations.

Any more questions let me know.

Warren
04th February, 2009
Hi Warren, everything seems to be ok, I have it working on umbraco 3 but as soon as I insert the macro for the [usercontrol] css switcher cookie - it throws this error.

When i manually unzipped the folder to see if had inserted the files in the correct place - theres only 3 files inside with long integer names - are these supposed to be named like this?

cheers for the advice
04th February, 2009
Hi Gary.
I havent looked at the code for this for ages. I would recommend opening the .ascx in visual studio and do some debugging.

Also this querystring in Umbraco is a godsend.

?umbDebugShowTrace=true

Re the long file names I may have used a beta of Per's package creator for V3, which is now built into the core of V4, in the package section. Which gives filenames unique names to make sure no conflicts.

Warren
04th February, 2009
Hi Warren,

I found the error to be related to "Response.Redirect(URL)" it states...

Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: url

Do you know of anything I can ammend here to avoid this error being thrown? Im not really an asp developer so Im not sure what Im doin, new to umbraco too! Cheers.
04th February, 2009
Woo, actually uncommented a line and deleted another and got it working. If anyone wants the fix, here it is (for Umbraco 4)

Original code:

iRnd = RandomClass.Next(250)
'Response.Redirect(URL & "?rnd=" & iRnd)
Response.Redirect(URL)

Fixed code:

iRnd = RandomClass.Next(250)
Response.Redirect(URL & "?rnd=" & iRnd)

and thanks very much Warren you've saved my bacon
04th February, 2009
Sorry Garry I really dont have the time currently to look into this at the moment.

Feel free to post on the umbraco forum for someone to help you out or create a similar piece of functionality.

As soon as i get some more time I will take another look at this.

Warren

04th February, 2009
Hi Gary you must have posted that whilst I was writing my reply.

I'm glad you found the fix.

Warren
Post a comment »

Comment's have been disabled, due to getting hit badly with comment form spam

My Skill Set

This is a quick list of my skill set to date

  • XHTML
  • CSS
  • Photoshop (Slicing)
  • XSLT
  • ASP.NET
  • Umbraco .NET CMS

My del.icio.us

My Last.FM

I am listening to:

  • Ain't Pretending by CowbellOh Girl29 Jul 2010, 10:37
  • Just Want Your Love by CowbellOh Girl29 Jul 2010, 10:33
  • Oh Girl by CowbellOh Girl29 Jul 2010, 10:31
Warren Buckley is an umbraco MVP 2008/2009 Warren Buckley is an umbraco level 1 certified developer

Photo of Warren Buckley Warren Buckley is a Creative Web Specialist based in Leigh on Sea, Essex , UK.