Written by Marco Conti Saturday, 26 June 2010 12:31
Joomla has the ability to assign any template to any given page (as long as that page is linked from a menu) which is a very useful feature. However, often this trick does not work very well with sh404SEF turned on. At least not with the version currently in stable release (A new Beta version is coming out and I'll bring you a full review - hopefully in print and video - as soon as I have a chance to test it).
The other day I had a need to create an alternate template I could assign to a single page in the site. I needed a full set of modules to appear on the left column where normally the navigation and several other modules reside. Doing this is possible with a number of extensions, mostly modules like NoNumber.nl Advanced Module Manager, Metamod and others. However, in some cases swapping the template itself can be a more manageable solution (interestingly, the developer of Metamod has an extension called "MetaTemplate" that may have helped as well). In any case, for various reasons it was the solution I needed.
The coding was easy. I simply cloned the template, renamed its folder and renamed its name in the XML file. Then I found the left column code in the index.php file (some templates these days use frameworks and keep that code in a sub-directory, so watch for it) and I changed the position name "left" to "left_alt" in the jdoc:include that generates the position in Joomla templates.
Original Code:
<jdoc:include type="modules" name="left" style="xhtml" />
New Code
<jdoc:include type="modules" name="left_alt" style="xhtml" />
I also kept most of the links referring to the CSS and JS files of the master template unchanged (i.e. pointing to templates/master_template/css/filename.css), giving me the ability to edit the master template's css files and have those changes reflect in the alternate template. Otherwise, each future template edit would have had to be duplicated in the alternate template for consistency. That's a pretty neat trick if you ask me.
So far so good. (by the way, if the above is Greek (or geek) to you, I will probably make a "10 Minute Joomla! Tips" screencast about this topic soon, so bear with me).
Everything went fine in the staging site, but when I moved the change to the live site something disturbing happened. The template refused to load. Likie all good staging sites, SEF URLs are kept off.
The first thing that came to mind as the culprit was sh404sef and I was right. Problem was that no matter what I did: refresh the cache, purge URLs, dance around the room wearing nothing but my underwear and singing opera, made a difference. The template was not going to load.
Without SEF URLs this would have been a 2 minute job. With SEF URLs turned out to be an ordeal.
However, I remembered that in the really old days the Joomla demo sported a module that allowed a front end user to change the template on the fly with any template loaded in the template manager. That module is long gone from the core but I remembered that it added a parameter to the URL to accomplish that feat. I just didn't remember the syntax. I tried temp=name, tp=name, tp&name and so on. I just missed the right syntax by a hair. A quick Google search returned the right parameter:
index.php?Template=template_name
Sure enough it worked like a charm in the staging site. The URL looked something like this:
index.php?option=com_content&id=243&view=article&template=template_alt
(for you eagle eyed, I edited the URL to shorten it a bit)
Note the & (ampersand) needed to insert the snippet in the Joomla URL. Also, the template syntax can be inserted pretty much anywhere in the URL as long as it's after "index.php?" and an & is used.
However, in the live site once again sh404sef would not hear it of loading the requested template.
Ultimately, I tried so many ways to make it load that I am not 100% sure which one really worked, but by the time I'll shoot my podcast on the topic I am hoping to be more positive about the right technique (or, hopefully, the new sh404sef beta will solve this issue). But for now what I think finally worked for me was to:
index.php?option=com_content&id=123&view=article&template=template_alt
To my relief, the alternate template loaded and the "Human Readable" url looked something like this:
http://domain.com/article-title?template=template_alt
I tried getting rid of the last part of the URL but at some point I decided that it was too much effort for the result I was going to get. There may be still a solution for that, but not today.
I also tested the link to make sure it would not break again. I purged the SEF URLs, I refreshed the cache and pretty much used every trick I know to break it. As of today it's still holding strong. The only thing I have not tried was turning sh404 off and then on again. But I'll try that in the near future on a staging site.
I hope this little tutorial and recount of my "struggle" will help someone in the same bind. Changing templates on certain pages is a powerful feature of Joomla. If used smartly it can make challenging site design much easier to implement. But paired with most SEF URL schemes it can create problems. Hopefully the above post will help you solve them and come up with your own elegant solutions.
Post them here if you do. Peace.
*cc*
| Next > |
|---|
Conticreative offers Individual and Corporate training (in person or online) on Joomla, Wordpress, Zen Cart and other leading Open Source scripts.