Page 1 of 3
Template Switcher
Posted: Wed Jan 30, 2008 6:29 pm
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
Re: Template Switcher
Posted: Wed Jan 30, 2008 9:58 pm
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
Re: Template Switcher
Posted: Wed Jan 30, 2008 10:12 pm
by Nullig
Excellent!
Thanks, alby.
Nullig
Re: Template Switcher
Posted: Wed Jan 30, 2008 11:08 pm
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.
Re: Template Switcher
Posted: Thu Apr 17, 2008 12:31 pm
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.
Re: Template Switcher
Posted: Thu Apr 17, 2008 2:34 pm
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
Re: Template Switcher
Posted: Fri Apr 18, 2008 8:30 am
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
Re: Template Switcher
Posted: Fri Apr 18, 2008 2:44 pm
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
Re: Template Switcher
Posted: Mon Apr 21, 2008 11:12 am
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
Re: Template Switcher
Posted: Mon Apr 21, 2008 12:00 pm
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
Re: Template Switcher
Posted: Mon Apr 21, 2008 9:14 pm
by ravonet
Thank you so much - it works perfect
Re: Template Switcher
Posted: Sat May 24, 2008 1:39 pm
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.
Re: Template Switcher
Posted: Wed May 28, 2008 7:52 pm
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
Re: Template Switcher
Posted: Wed May 28, 2008 8:10 pm
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
Re: Template Switcher
Posted: Wed May 28, 2008 9:19 pm
by leifnicholz
I get it.. How stupid of me not to notice that at first.. hahaha.. thanks..