Template Switcher

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
miss_d_bus
Forum Members
Forum Members
Posts: 121
Joined: Sun May 01, 2005 4:27 pm
Location: Kent, UK

Re: Template Switcher

Post by miss_d_bus »

I have wondered about this - the switcher changes the active template in the admin area rather than just changing what is seen so what happens if there are more than one person viewing the website at any one time, i.e. wanting different templates loaded?
Won't this be a problem?
User avatar
duclet
Forum Members
Forum Members
Posts: 187
Joined: Fri Jun 23, 2006 12:55 pm

Re: Template Switcher

Post by duclet »

That is why I prefer my cookie CSS method.
User avatar
Nullig
Power Poster
Power Poster
Posts: 2380
Joined: Fri Feb 02, 2007 4:31 pm
Location: Comox Valley, BC

Re: Template Switcher

Post by Nullig »

This "Tip" was a solution I used when I was developing a site for a customer. It allowed them to look at various options for the layout/design, so they could select the one they wanted. It wasn't really meant to be a "live" solution for ongoing use. For that, I think duclet's cookie method is far better.

Nullig
miss_d_bus
Forum Members
Forum Members
Posts: 121
Joined: Sun May 01, 2005 4:27 pm
Location: Kent, UK

Re: Template Switcher

Post by miss_d_bus »

Thanks for clarifying this. Its probably not suitable for my needs then but I can see it as a good application at the design stage.
Sonya

Re: Template Switcher

Post by Sonya »

My solution used here CMSMS templates (preview function for templates) doesn't change assigned templates in database and thus can handle multiple users previewing templates at the same time. I am not sure about caching, probably it has to be switched off on the previewed site.

The hack is placed directly in index.php, because I was not able to figure out what event could handle it  :-\.

Search comment line in index.php

Code: Select all

#If this is a case where a module doesn't want a template to be shown, just disable caching
and add the code before this line

Code: Select all

     ## hack ##
	$template_array = array(20, 21, 22);
	if (isset($_REQUEST['tpl_id']) && in_array(intval($_REQUEST['tpl_id']),$template_array)) {
		$pageinfo->template_id = intval($_REQUEST['tpl_id']);
		$_SESSION['tpl_id'] = intval($_REQUEST['tpl_id']);
	} elseif (isset($_SESSION['tpl_id']) && in_array(intval($_SESSION['tpl_id']),$template_array)) {
		$pageinfo->template_id = intval($_SESSION['tpl_id']);
	}
    ## end hack ##
The preview site is called with http://yoursite.com/index.php?tpl_id=21

I suppose there is no event sent at this point. Or?
Last edited by Sonya on Sun Sep 27, 2009 1:02 pm, edited 1 time in total.
Sonya

Re: Template Switcher

Post by Sonya »

duclet wrote: That is why I prefer my cookie CSS method.
Does this method involve changing of the whole template or just CSS?
ravonet
Forum Members
Forum Members
Posts: 59
Joined: Fri Jan 11, 2008 12:04 pm

Re: Template Switcher

Post by ravonet »

duclet: I would like further explanation on how you change styles. Thanks in advance.
User avatar
duclet
Forum Members
Forum Members
Posts: 187
Joined: Fri Jun 23, 2006 12:55 pm

Re: Template Switcher

Post by duclet »

You basically needs to have structure that flexible enough to handle different designs. For each design, create a stylesheet for it and attach to that single template. In order to differentiate the designs, set the ID of the body tag to the appropriate design using a stored cookie.

So example, you have a stylesheet called "One" with some styles that will show as long as the body has the ID of "one". The same for "two", and "three". Now, create a UDT that will handle the output of the ID. For the sake of simplicity, the UDT could just output the cookie for the currently selected stylesheet. The BODY tag should use that UDT to get the ID. A little JavaScript to change the ID and it should be all set.

You can take a look at my site to see the JavaScript.
ravonet
Forum Members
Forum Members
Posts: 59
Joined: Fri Jan 11, 2008 12:04 pm

Re: Template Switcher

Post by ravonet »

Thanks, I will have at look at it...
Zeynab
New Member
New Member
Posts: 9
Joined: Sun Jul 05, 2009 12:48 pm

Re: Template Switcher

Post by Zeynab »

Hi,

does anyone have the complete code for a WORKING style/template switcher for the current instance of CMSMS? Duclet's explanations unfortunately do nothing for me, I wouldn't know what a UDT is if it bit me in the nose...  :o
User avatar
pixelita
Power Poster
Power Poster
Posts: 388
Joined: Sun Sep 16, 2007 3:07 am
Location: Houston, Texas USA

Re: Template Switcher

Post by pixelita »

UDT=User Defined Tag.  Not sure what code should be in that tag to do what she (he?) is saying. 
Submit your site to the We Love CMSMS showcase
katea
Forum Members
Forum Members
Posts: 116
Joined: Wed Jun 10, 2009 12:25 am

Re: Template Switcher

Post by katea »

I opted for a swift jquery method.

Works a treat:

http://www.kelvinluck.com/2006/05/switc ... th-jquery/
Phoenixsong
New Member
New Member
Posts: 5
Joined: Thu Sep 10, 2009 4:38 am

Re: Template Switcher

Post by Phoenixsong »

Hello. Sorry to bump such an old topic, but I think this is the most appropriate place to get the help I need.

I needed a template switcher for my website, and when I found Sonya's method above I was delighted to see that it worked exactly as I needed it to. I added the hack to my site and used it successfully up until version 1.8.2. In December I upgraded to 1.9.1, and the hack stopped working. I could click on the link I used to change the template and it would reload the page, but the template would not change. This is still not working in 1.9.2.

You can see a dummy site that's still running 1.8.2 and still has the template switcher working here (links are at the bottom of the left nav in the blue and green layouts, and on the right side of the footer in the default CSS menu top layout). The 1.9.2 site is live, and the links are not present there so that visitors won't be confused by them, but you can test the same thing by visiting this link and appending "&tpl_id=#" to the URL, where valid template IDs for # are 25, 26 and 27 (if you just want to manipulate the URLs on the dummy 1.8.2 version, valid template IDs are 16, 23 and 24). Nothing happens when you use those URLs on the 1.9.2 site.

Under normal circumstances I would simply use the same template and switch the stylesheet itself, as mentioned by several other users above; it's a method I've used to great effect on plenty of static HTML sites before. And if you look at the site I linked to, you'll also see that two of the layouts are in fact identical save for colors and images. Those two could use the same template and simply change stylesheets; I currently have them using two separate templates, however, because as you'll see the third option on the dummy site is one of the default CMSMS layouts that uses a completely different template (and, more importantly, a different menu manager template). That's the primary reason for wanting to use different templates: some of my visitors prefer vertical navigation, and others prefer horizontal navigation, and since the horizontal nav with dropdown requires a different menu template, I can't use the same layout template for each design and so am forced to change the templates themselves with the switcher. (Unless there's a way to conditionally change the menu template used in a layout based on the stylesheet chosen? I suppose that would work as well, but am not quite sure how I'd go about that.)

So I guess my question is this: what has changed that would suddenly cause this to stop working between versions 1.8.2 and 1.9.1/1.9.2 (and possibly 1.9; I accidentally missed that upgrade, so I don't know whether it works there or not)? Has the method of handling templates changed or something? Is it simply a matter of adjusting a few terms or variables in the existing hack, or what?

I saw the announcement stating that modifications to core files are heavily discouraged and should not be supported/suggested, and so I'm also wondering if there's a way to use this without modifying index.php. I don't like the idea of modifying index.php either if I don't have to, but since I did have this functionality working for a while it would be a real shame to just up and not have it anymore just because of a quick version upgrade. If fixing the index.php hack itself isn't an option, does anyone know of a way that I could modify it and use it a bit more... safely? Say, as a UDT, or even as a module? I'm starting to learn PHP but haven't yet gone in-depth or taken a serious look at Smarty and how it's used to set up CMSMS specifically (though I will certainly get there!), so as of yet I haven't been able to puzzle out a way to do it myself. :/ Certainly just putting the above code as-is into a UDT and putting the UDT in the templates in question doesn't work, though I suspect that may have been a silly thing to try anyway.

If you need any further information, I'm happy to provide it. Thanks in advance.
Sonya

Re: Template Switcher

Post by Sonya »

A very dirty and quick walkaround without hacking into the core.

Create an UDT, call it switch and add code below into it:

Code: Select all

$template_array = array(15, 20, 21, 22);
global $gCms;

if (isset($_REQUEST['tpl_id']) && in_array(intval($_REQUEST['tpl_id']), $template_array)) {
  $tpl = $_REQUEST['tpl_id'];
  $_SESSION['tpl_id'] = $tpl;
} elseif (isset($_SESSION['tpl_id']) && in_array(intval($_SESSION['tpl_id']),$template_array)) {
  $tpl = $_SESSION['tpl_id'];
}

if ($tpl) {
    $tpl = isset($_REQUEST['tpl_id'])?$_REQUEST['tpl_id']:$_SESSION['tpl_id'];
    $page = get_pageid_or_alias_from_url();
    $contentops = cmsms()->GetContentOperations();
    $contentobj = $contentops->LoadContentFromAlias($page, true);
    $contentobj->SetTemplateId($tpl);
    unset($gCms->variables['template']);
    $top = $smarty->fetch("tpl_top:" . $tpl);
    $body = $smarty->fetch("tpl_body:" . $tpl);
    $head = $smarty->fetch("tpl_head:" . $tpl);
    $html = $top . $head . $body;
    $params['content'] = $html;
}
Adjust your template ids in array.

Then Go to Extensions » Event Manager, find an event ContentPostRender and assign UDT to it.

Be sure that this will have impact on the performance as the template is rendered twice. You can try to create your hack from the code above to avoid double rendering. I am not sure if there is any clean solution without a hack as I cannot see any Event that is fired prior to ContentPostRender.
Phoenixsong
New Member
New Member
Posts: 5
Joined: Thu Sep 10, 2009 4:38 am

Re: Template Switcher

Post by Phoenixsong »

Thanks for your prompt response, Sonya. I can set up the UDT as you mentioned, but it causes the following error when I reload the page (even without trying to change templates):

Code: Select all

Fatal error: Cannot redeclare curpageurl() (previously declared in /path/to/htdocs/site/lib/content.functions.php(975) : eval()'d code:4) in /path/to/htdocs/site/lib/content.functions.php(975) : eval()'d code on line 3
That looks like it's just because of the UDT I was using to set the URL of the current page in a variable, though (to allow the user to switch templates without being sent back to the home page), because if I remove the UDTs in question from the templates it works just fine. For now I think I'll just put the revised hack in index.php so that my users have the switcher functionality again, and later I'll tweak the other two UDTs so that they play nicely with this one (and see if I can't do something about the double-rendering, as you mentioned).

Thanks again! I really appreciate the help!

EDIT: Hmm, nope. Looks like I misunderstood what was being doubled up. The other UDTs still complain about being duplicated even when I put the hack directly into index.php. No matter—I'll just find another way to bring the user back to the same page. Thanks again!
Locked

Return to “Tips and Tricks”