If you look at the System > Configuration > General > Design -> Package and Theme sections, you’ll see that Magento provides “Add Exception” buttons most of the configuration parameters available. There is also a cryptic caption saying: “Match expressions in the same order as displayed in the configuration.”
But what does it mean? And what can we do with this?
Well, if you’re adventurous and hike into the code behind the exception options you will realize that Magento takes a regular expression string in “Matched Expression” and will compare it with the browser user agent, applying the “Value” set as the one to use instead of what is set for the default if it finds a match.
In other words, if the regular expression matches with the browser user agent apply the design exception set.
In yet more words, you can use this to check what browser customers are using to access your store and set a different package / template / skin / layout / theme accordingly. So, if you want to serve up a different theme for mobile browsers, this is where the exceptions come in handy.
Let’s look at how to set this up so Magento serves the iPhone theme for mobile device customers:
Here is the full contents of the “Matched Expression”:
iPhone|iPod|BlackBerry|Palm|Googlebot-Mobile|Mobile|mobile|mobi|Windows Mobile|Safari Mobile|Android|Opera Mini
This may not be an exhaustive list of mobile browser agents but you get the idea. To check for your specific user agent, you can use: whatsmyuseragent.com.
Bear in mind when entering strings that they are case sensitive.
So what the example in the screenshot is saying is: “If the user agent is one of the listed ones, serve up the iphone design that ships with Magento, otherwise just use the default”.
Depending on where you add your exception, you can create a different layout or skin or complete theme for your audience who use different devices to get to your site.
For example, you could have a cutting edge design as your default theme but offer an alternative, cut-down version for the odd IE6 user.
For the most part, however, you will use this feature to serve a mobile optimized version of your design.
Finally, what does that cryptic caption then mean? Well, it just means that if you have several exception rules, they will be matched and applied in the order they are listed.
If you have a good regular expression matching mobile devices or have implemented an interesting browser exception, please do share it in the comments.
Originally published on magebase.com. Copyright © 2010 Magebase - All Rights Reserved.