• twitter image
  • facebook image
  • youtube image
  • linkedin image
Language: CMS Made Simple Czech CMS Made Simple France CMS Made Simple Spain CMS Made Simple Hungary CMS Made Simple Russia CMS Made Simple Netherlands

All times are UTC




Post new topic This topic is locked, you cannot edit posts or make further replies.  [ 34 posts ]  Go to page 1, 2, 3  Next
Author Message
 Post subject: Template Switcher
PostPosted: Wed Jan 30, 2008 6:29 pm 
Offline
Power Poster
Power Poster
User avatar

Joined: Fri Feb 02, 2007 4:31 pm
Posts: 2395
Location: Comox Valley, BC
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:
<?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:
<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

_________________
Come play in the Sandbox at my CMS Made Simple demo site: http://www.cmsmsdemo.com.


Top
 Profile  
 
 Post subject: Re: Template Switcher
PostPosted: Wed Jan 30, 2008 9:58 pm 
Offline
Support Guru
Support Guru
User avatar

Joined: Mon Jul 04, 2005 5:12 pm
Posts: 4814
Location: Ferrara, Italy
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:
<?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

_________________
CMSMS Support Team
Italian Admin and Moderator

Plugins: Geolocate hostip, Multiple random image, Image rotator (beta), Content Pagination
Modules: ForumMadeSimple (Howto), TranslationManager
Multilingual: MLE is not CMSMS


Last edited by alby on Mon Apr 21, 2008 12:56 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Template Switcher
PostPosted: Wed Jan 30, 2008 10:12 pm 
Offline
Power Poster
Power Poster
User avatar

Joined: Fri Feb 02, 2007 4:31 pm
Posts: 2395
Location: Comox Valley, BC
Excellent!

Thanks, alby.

Nullig

_________________
Come play in the Sandbox at my CMS Made Simple demo site: http://www.cmsmsdemo.com.


Top
 Profile  
 
 Post subject: Re: Template Switcher
PostPosted: Wed Jan 30, 2008 11:08 pm 
Offline
Dev Team Member
Dev Team Member
User avatar

Joined: Tue Oct 19, 2004 6:44 pm
Posts: 6609
Location: Fernie British Columbia, Canada
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
--
if you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
----------------
Don't make me angry..... you won't like me when I'm angry....


Top
 Profile  
 
 Post subject: Re: Template Switcher
PostPosted: Thu Apr 17, 2008 12:31 pm 
Offline
Forum Members
Forum Members

Joined: Fri Jan 11, 2008 12:04 pm
Posts: 60
Quote:
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.


Top
 Profile  
 
 Post subject: Re: Template Switcher
PostPosted: Thu Apr 17, 2008 2:34 pm 
Offline
Power Poster
Power Poster
User avatar

Joined: Fri Feb 02, 2007 4:31 pm
Posts: 2395
Location: Comox Valley, BC
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

_________________
Come play in the Sandbox at my CMS Made Simple demo site: http://www.cmsmsdemo.com.


Top
 Profile  
 
 Post subject: Re: Template Switcher
PostPosted: Fri Apr 18, 2008 8:30 am 
Offline
Forum Members
Forum Members

Joined: Sun Aug 12, 2007 4:01 pm
Posts: 41
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


Top
 Profile  
 
 Post subject: Re: Template Switcher
PostPosted: Fri Apr 18, 2008 2:44 pm 
Offline
Power Poster
Power Poster
User avatar

Joined: Fri Feb 02, 2007 4:31 pm
Posts: 2395
Location: Comox Valley, BC
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

_________________
Come play in the Sandbox at my CMS Made Simple demo site: http://www.cmsmsdemo.com.


Top
 Profile  
 
 Post subject: Re: Template Switcher
PostPosted: Mon Apr 21, 2008 11:12 am 
Offline
Forum Members
Forum Members

Joined: Fri Jan 11, 2008 12:04 pm
Posts: 60
When I try to use the Template Switcher I get this:

Quote:
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


Top
 Profile  
 
 Post subject: Re: Template Switcher
PostPosted: Mon Apr 21, 2008 12:00 pm 
Offline
Support Guru
Support Guru
User avatar

Joined: Mon Jul 04, 2005 5:12 pm
Posts: 4814
Location: Ferrara, Italy
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

_________________
CMSMS Support Team
Italian Admin and Moderator

Plugins: Geolocate hostip, Multiple random image, Image rotator (beta), Content Pagination
Modules: ForumMadeSimple (Howto), TranslationManager
Multilingual: MLE is not CMSMS


Last edited by alby on Mon Apr 21, 2008 12:58 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Template Switcher
PostPosted: Mon Apr 21, 2008 9:14 pm 
Offline
Forum Members
Forum Members

Joined: Fri Jan 11, 2008 12:04 pm
Posts: 60
Thank you so much - it works perfect


Top
 Profile  
 
 Post subject: Re: Template Switcher
PostPosted: Sat May 24, 2008 1:39 pm 
Offline
Forum Members
Forum Members
User avatar

Joined: Fri Jun 23, 2006 12:55 pm
Posts: 188
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.

_________________
http://tiger-inc.com


Top
 Profile  
 
 Post subject: Re: Template Switcher
PostPosted: Wed May 28, 2008 7:52 pm 
Offline
Forum Members
Forum Members
User avatar

Joined: Mon Apr 07, 2008 4:14 pm
Posts: 174
Location: Olympia, WA
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

_________________
I'm using:
     • CMSMS 1.3.1 "Havana"
     • IIS 5 on Windows XP Pro
     • PHP 5
     • MySQL 5


Top
 Profile  
 
 Post subject: Re: Template Switcher
PostPosted: Wed May 28, 2008 8:10 pm 
Offline
Support Guru
Support Guru
User avatar

Joined: Mon Jul 04, 2005 5:12 pm
Posts: 4814
Location: Ferrara, Italy
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

_________________
CMSMS Support Team
Italian Admin and Moderator

Plugins: Geolocate hostip, Multiple random image, Image rotator (beta), Content Pagination
Modules: ForumMadeSimple (Howto), TranslationManager
Multilingual: MLE is not CMSMS


Top
 Profile  
 
 Post subject: Re: Template Switcher
PostPosted: Wed May 28, 2008 9:19 pm 
Offline
Forum Members
Forum Members
User avatar

Joined: Mon Apr 07, 2008 4:14 pm
Posts: 174
Location: Olympia, WA
I get it.. How stupid of me not to notice that at first.. hahaha.. thanks..

_________________
I'm using:
     • CMSMS 1.3.1 "Havana"
     • IIS 5 on Windows XP Pro
     • PHP 5
     • MySQL 5


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic This topic is locked, you cannot edit posts or make further replies.  [ 34 posts ]  Go to page 1, 2, 3  Next

All times are UTC


Who is online

Users browsing this forum: No registered users


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
A2 Hosting