Template Switcher

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
User avatar
Nullig
Power Poster
Power Poster
Posts: 2380
Joined: Fri Feb 02, 2007 4:31 pm
Location: Comox Valley, BC

Template Switcher

Post by Nullig »

I recently needed to provide a method of changing templates from the frontend to allow my client the option of viewing several layouts, so they could see what they would look like before selecting the one they wished to use. I created 3 templates, with attached stylesheets and put links to the template switcher within each template.

If anyone is interested, here is the code I used.

I created a file called changetemplate.php with the following code:

Code: Select all

<?php

$my_host = "your host";
$my_user = "your cms db user";
$my_pass = "your cms db password";
$my_data = "your database name";

$template_id = $_GET["template_id"];

$template_array = array("20", "21", "22");

$my_dbconn = mysql_connect("$my_host", "$my_user", "$my_pass") or die("Could not connect: " . mysql_error());
mysql_select_db("$my_data",$my_dbconn);

if (in_array($template_id, $template_array)) {

mysql_query("UPDATE cms_content SET template_id = '$template_id'");

}

mysql_close ($my_dbconn);

header( 'Location: /index.php' ) ;

?>
As you can see, the database info is hard-coded into the file (copied from the config.php file). For your site, you need to put the appropriate info into these variables, as well as set the template array to the appropriate template ids. Also, the table "cms_content" may need to be changed to match the table name in your installation.

You also need to put some links on your page to call the template switcher. I put mine in the templates, either above the header or below the footer using links like:

Code: Select all

<p>
<a href="http://your.domain.com/changetemplate.php?template_id=20">Style 1</a>
<a href="http://your.domain.com/changetemplate.php?template_id=21">Style 2</a>
<a href="http://your.domain.com/changetemplate.php?template_id=22">Style 3</a>
</p>
It's not perfect, but it's the best way I could find to accomplish this. Any suggestions for improvement would be appreciated.

Nullig
alby

Re: Template Switcher

Post by alby »

Nullig wrote: It's not perfect, but it's the best way I could find to accomplish this. Any suggestions for improvement would be appreciated.
If changetemplate.php is in the same directory of config.php you can have config variables (not hardcoded) with:

Code: Select all

<?php
$template_array = array("20", "21", "22");


/* DON'T EDIT BELOW */
if (file_exists(getcwd().'/config.php'))
{
  require_once getcwd().'/config.php';

  $my_dbconn = mysql_connect($config['db_hostname'], $config['db_username'], $config['db_password']) or die('Could not connect: '. mysql_error());
  mysql_select_db($config['db_name'], $my_dbconn);

  $template_id = intval($_GET['template_id']);

  if (in_array($template_id, $template_array)) {
    mysql_query("UPDATE ". $config['db_prefix'] ."content SET template_id = $template_id");
  }
  else
  {
    exit('Template not allowed!');
  }

  mysql_close ($my_dbconn);

  header('Location: '. $config['root_url'] .'/index.php') ;
}
else
{
  exit("Don't find config!");
}
?>
Alby
Last edited by alby on Mon Apr 21, 2008 12:56 pm, edited 1 time in total.
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 »

Excellent!

Thanks, alby.

Nullig
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm
Location: Fernie British Columbia, Canada

Re: Template Switcher

Post by calguy1000 »

I've yet to look at it in depth, but good going.


Note: this solution will not work properly if different templates have different numbers of content blocks.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
ravonet
Forum Members
Forum Members
Posts: 59
Joined: Fri Jan 11, 2008 12:04 pm

Re: Template Switcher

Post by ravonet »

For your site, you need to put the appropriate info into these variables, as well as set the template array to the appropriate template ids.
Where do I find template IDs? - I could really use this template switcher, great work.
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 »

If you go to layout/Templates in Admin, when you move your mouse over the template names, the status bar will show you the IDs.

Nullig
ameagher
Forum Members
Forum Members
Posts: 39
Joined: Sun Aug 12, 2007 4:01 pm

Re: Template Switcher

Post by ameagher »

Hi All,

From your experience is it likely there would be a performance problem if this was used for many(dozens/hundreds) of templates.

I guess I'm asking how scalable this would be?

Thanks,
Anthony
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 »

I don't think there would be much of a hit on performance - it's just checking a simple array. Perhaps if it was in the thousands, it might take a few seconds.

Nullig
ravonet
Forum Members
Forum Members
Posts: 59
Joined: Fri Jan 11, 2008 12:04 pm

Re: Template Switcher

Post by ravonet »

When I try to use the Template Switcher I get this:
Not Found

The requested URL /index.php was not found on this server.
Apache/2.2.3 (Debian) Server at 'my domain' Port 80
- I use alby's version of the changetemplate.php with the ID's form my own templates
- I placed the changetemplate.php in the root-folder like config.php
- I've placed the links inside my templates

But still I get the above error - any ideas?

I'm testing the template switcher in a subfolder on my domain - can this cause any problems? (I have just checked the links in the templates and they seams fine)

They looks like this: http://my.domain.com/temp/testing/chang ... late_id=15
alby

Re: Template Switcher

Post by alby »

ravonet wrote: I'm testing the template switcher in a subfolder on my domain - can this cause any problems? (I have just checked the links in the templates and they seams fine)
Yes, modify script with:

header('Location: '. $config['root_url'] .'/index.php') ;


Alby
Last edited by alby on Mon Apr 21, 2008 12:58 pm, edited 1 time in total.
ravonet
Forum Members
Forum Members
Posts: 59
Joined: Fri Jan 11, 2008 12:04 pm

Re: Template Switcher

Post by ravonet »

Thank you so much - it works perfect
User avatar
duclet
Forum Members
Forum Members
Posts: 187
Joined: Fri Jun 23, 2006 12:55 pm

Re: Template Switcher

Post by duclet »

Why do all of that? If you just need to have an easy template switch, make the structure of your site flexible and use CSS wisely. On my site, I allow my visitors to change the template simply by storing a cookie of the CSS that is associated with a specific design to use. No need to write a hack or anything like that and each user can have their own design instead of everything using the same design. Take a look at my site for what I mean. If you want further explanation, I can further explained it.
leifnicholz

Re: Template Switcher

Post by leifnicholz »

Hi,

This hack is amazing!! I'm using Alby's code and I just have one question.. What causes the "Template Not Allowed" message to appear? I tried to figure out the code but I can't see why some templates aren't allowed..

Thanks,
Leif
alby

Re: Template Switcher

Post by alby »

leifnicholz wrote: This hack is amazing!! I'm using Alby's code and I just have one question.. What causes the "Template Not Allowed" message to appear? I tried to figure out the code but I can't see why some templates aren't allowed..
this happens if your template_id is not in $template_array (in top you list templates allowed)

Alby
leifnicholz

Re: Template Switcher

Post by leifnicholz »

I get it.. How stupid of me not to notice that at first.. hahaha.. thanks..
Locked

Return to “Tips and Tricks”