Jump to content


Template Switcher


  • You cannot reply to this topic
27 replies to this topic

#1 Nick_NP

    Etomite Forum Fan

  • Member
  • Pip
  • 72 posts

Posted 23 July 2005 - 09:23 AM

I have added a new snippet to the Snippet Library!

Snippet name: Template Switcher
Author: Nick_NP
Version: 1.0 (Final)
More info: <a href="http://www.etomite.com/browsesnippets.html?int_snipid=127">View this snippet</a>
Description:
Generates a drop-down menu from which users can choose which template to view your site with.

Please feel free to comment on this snippet, suggest improvements, or simply praise my work! ;)

WARNING : For this snippet to work as intended, you'll need to edit Etomite's index.php. Has only been tested with Etomite v0.6.

The code to be insterted is between the long lines (---------------------------), colour in a code block doesn't seem to work for me.... (Around line #737)

 	// get the template and start parsing!
  	$sql = "SELECT * FROM ".$this->dbConfig['dbase'].".".$this->dbConfig['table_prefix']."site_templates WHERE ".$this->dbConfig['dbase'].".".$this->dbConfig['table_prefix']."site_templates.id = '".$this->documentObject['template']."';";
  	$result = $this->dbQuery($sql);
  	$rowCount = $this->recordCount($result);
  	if($rowCount!=1) {
    $this->messageQuit("Incorrect number of templates returned from database", $sql);
  	}
  	$row = $this->fetchRow($result);

      // ----------------------------------------------------------------------
      // TemplateSwitcher Mod

      if ( ( substr($row['templatename'],0,1) == '_' ) && ( ( session_is_registered('user_templateid') ) || ( isset($_POST['templateid']) ) ) )
      {
        if ( isset($_POST['templateid']) )
           $newtemplate = $_POST['templateid'];
        else
           $newtemplate = $_SESSION['user_templateid'];

      $sql = "SELECT * FROM ".$this->dbConfig['dbase'].".".$this->dbConfig['table_prefix']."site_templates WHERE ".$this->dbConfig['dbase'].".".$this->dbConfig['table_prefix']."site_templates.id = '".$newtemplate."';";
      $result = $this->dbQuery($sql);
      $rowCount = $this->recordCount($result);
      if($rowCount!=1) {
      	$this->messageQuit("Incorrect number of templates returned from database", $sql);
      }
      $row = $this->fetchRow($result);
      }

      // ----------------------------------------------------------------------

      $documentSource = $row['content'];

  	// get snippets and parse them the required number of times
  	$this->snippetParsePasses = empty($this->snippetParsePasses) ? 3 : $this->snippetParsePasses;

And here's the snippet code :
session_start();
$output = "";

if ( isset($_POST['templateid']) )
{
   session_register( "user_templateid" );
   $_SESSION['user_templateid'] = $_POST['templateid'];
}

$query = "";
$query .= "select * from ".$etomite->dbConfig['dbase'].".".$etomite->dbConfig['table_prefix']."site_templates ";
$query .= "where ".$etomite->dbConfig['dbase'].".".$etomite->dbConfig['table_prefix']."site_templates.templatename like '\_%';";
$templatelist = $etomite->dbQuery($query);

$output .= '<form action="" method="post">';
$output .= '<label>theme » ';
$output .= '<select name="templateid">';

while ( $template = $etomite->fetchRow($templatelist) )
{
   $output .= '<option value="'.$template['id'].'" '.(($_SESSION['user_templateid']==$template['id'])?'selected':'').'>'.substr($template['templatename'],1).'</option>';
}

$output .= '</select>';
$output .= '</label>';
$output .= '<input type="submit" value="Go" />';
$output .= '</form>';

return $output;

Using TemplateSwitcher :

All templates that you want your users to be able to choose between must have a name starting with an underscore
ex : "_blue template" (chosen template will not be applied on pages whose template in Etomite does not start with an undescore)

Remember to include the snippet in all templates whose names start with an underscore!

Page caching must be turned OFF on all pages using the snippet! (sorry, haven't found a way around that...)

Enjoy!

Edited by Nick_NP, 23 July 2005 - 09:42 AM.


#2 Jelmer

    Loves Etomite Forums!

  • Member
  • PipPipPipPip
  • 1,173 posts

Posted 23 July 2005 - 09:32 AM

Whats the benefit of this instead of using ChangeTemplates?

#3 Nick_NP

    Etomite Forum Fan

  • Member
  • Pip
  • 72 posts

Posted 23 July 2005 - 09:48 AM

This does not do the same thing as ChangeTemplates.

ChangeTemplates : allows the site admin to "change every page using "X" template to use "Y" template".

TemplateSwitcher : allows somebody BROWSING YOUR SITE to select a template to view your site with.

I do have a demo url, it's very messy coz I haven't really finished the other templates. Also, I'm not sure how long this site will be hosted there (like the templates, the site is far from finished!). Anyways, it should give you the idea!

Click here for unfinished work!

#4 Jelmer

    Loves Etomite Forums!

  • Member
  • PipPipPipPip
  • 1,173 posts

Posted 23 July 2005 - 09:50 AM

Nice work!

#5 Nick_NP

    Etomite Forum Fan

  • Member
  • Pip
  • 72 posts

Posted 23 July 2005 - 09:57 AM

thank you! :-)

There seem to have been quite a few people after a snippet/mod like this but I think the idea of changing some of Etomite's code has put everybody off...

#6 Dean

    Loves Etomite Forums!

  • Admin
  • 4,758 posts
  • Gender:Male

Posted 23 July 2005 - 10:02 AM

Looks good, you may want to consider checking it on 061RC1

#7 Nick_NP

    Etomite Forum Fan

  • Member
  • Pip
  • 72 posts

Posted 23 July 2005 - 10:14 AM

Will do once I get round to trying out RC1!
( I can't imagine it'll be too difficult )

#8 Nick_NP

    Etomite Forum Fan

  • Member
  • Pip
  • 72 posts

Posted 25 July 2005 - 03:07 PM

Works with the Etomite v0.6.1!

The only change is the place where the extra code should be inserted into the index.php -> about 30 lines lower.

#9 mrruben5

    Likes Etomite Forums!

  • Member
  • PipPip
  • 255 posts

Posted 25 July 2005 - 08:54 PM

The javascript version is more nifty :P

#10 Nick_NP

    Etomite Forum Fan

  • Member
  • Pip
  • 72 posts

Posted 25 July 2005 - 11:20 PM

Javascript version?

#11 mrruben5

    Likes Etomite Forums!

  • Member
  • PipPip
  • 255 posts

Posted 27 July 2005 - 10:05 AM

http://www.cncdepot.net/flyingz/

CLick the little icons at the top :)

#12 Nick_NP

    Etomite Forum Fan

  • Member
  • Pip
  • 72 posts

Posted 27 July 2005 - 10:38 AM

Ah ok, I see what you mean now!

But again, this snippet goes a bit further than that.

The script used at that url just changes the page's css file, which is fine if that's all you want to be able to do.

If on the other hand you want users to be able to change between templates that can't be differentiated simply by their css, that use a wide range of template-specific images, that contain different content or different snippets, then TemplateSwitcher is your answer.

It also removes the need to include any javascript, does not use cookies, remains managable from within the Etomite manger, handles listing and naming the templates automatically and (I hope) makes it easier for people who know a bit about Etomite but nothing about javascript to include this feature on their websites. :-)

#13 Jelmer

    Loves Etomite Forums!

  • Member
  • PipPipPipPip
  • 1,173 posts

Posted 27 May 2007 - 07:53 PM

Has anyone tested this snippet with the 0614 codebase? I'm working on yet another Templates Demo site, and I'd rather have a template selector than a page for every template...

#14 Jelmer

    Loves Etomite Forums!

  • Member
  • PipPipPipPip
  • 1,173 posts

Posted 28 May 2007 - 09:24 AM

I tried building a snippet with the same functionality, but based on the standard 0614 codebase. At the beginning it worked, but I think I've made an error somewhere that has to do with sessions... Because now it points to the correct URL if you change the template, but it doesn't change the template!

Try it a few times on different pages please, you'll see what I mean. Demo: http://www.jelmer.org/demo/test.html

// TemplateSwitcher 2.0, for use with Etomite 0.6.1.4
$output="";

if ($rs = $etomite->getIntTableRows(
	$fields="*",
	$from="site_templates",
	$where="",
	$sort="templatename",
	$dir="ASC",
	$limit="",
	$push=true,
	$addPrefix=true))
{
	//some javascript for my form to work...
	$output.='<script language="javascript">
		function showpage(form)
		{ var item = form.templates.selectedIndex;
		choice = form.templates.options[item].value;
		if (choice!="x") top.location.href=(choice);
		};
		</script>';
	$output .="<strong>View this page with template: </strong>";
	$output .="<form><select name=\"templates\" onChange=\"showpage(this.form);\">";
	
	$number=count($rs);
	for ($x=0; $x<$number; $x++) {
		//creating the options and the links
		$output .= "<option value='[~$etomite->documentIdentifier~]?tpl=".$rs[$x]['id']."'";
		//if this is the current template, select it
		if($rs[$x]['id']==$_SESSION['tpl']) {$output.=" selected";}
		$output .=">";
		//adding title
		$output .= $rs[$x]['templatename'];
		//closing the option
		$output .= "</option>\n";
		}
	$output .="</select></form>";
}
else {
	$output="Oops something whent wrong!";
	}	

return $output;


#15 Cris D.

    Loves Etomite Forums!

  • Developers
  • PipPipPipPip
  • 1,104 posts
  • Gender:Male

Posted 28 May 2007 - 10:18 AM

Are your pages cached? Maybe I'm not reading this right, but this from the index.php says...
 if( ($_GET['tpl'] != "")
	  && ($template==$this->config['default_template'])
	  && (in_array($_GET['tpl'],$this->tpl_list)) )
	  {
		$template = strip_tags($_GET['tpl']);
		$_GET['tpl'] = "";
	  // if the session template has been set, use it
	  }
	  elseif( isset($_SESSION['tpl'])
	  && ($template==$this->config['default_template'])
	  && (in_array($_SESSION['tpl'],$this->tpl_list)) )
	  {
		$template = strip_tags($_SESSION['tpl']);
	  }
Therefore isn't this looking for $_GET['tpl'] rather than setting the $_SESSION['tpl'] which is set back to $_SESSION['tpl'] if $_GET is not set?

Edited by Cris D., 28 May 2007 - 10:34 AM.


#16 Jelmer

    Loves Etomite Forums!

  • Member
  • PipPipPipPip
  • 1,173 posts

Posted 28 May 2007 - 11:31 AM

If I replace the $_SESSION bit with $_GET, the selection part works alright, and the other thing indeed had something to do with one page still being cached. Sometimes it's just so easy!

#17 Psycho Mantis

    Etomite Forum Fan

  • Member
  • Pip
  • 113 posts

Posted 20 March 2009 - 03:05 PM

Has anyone tried this with Etomite 1.1?

#18 Cris D.

    Loves Etomite Forums!

  • Developers
  • PipPipPipPip
  • 1,104 posts
  • Gender:Male

Posted 20 March 2009 - 10:00 PM

Yep, it's still good as gold.
You will have to change this line to get it to work as expected:
if( ($_GET['tpl'] != "")
	  //&& ($template==$this->config['default_template'])
	  && (in_array($_GET['tpl'],$this->tpl_list)) )
I'm not fully aware of the implications of this change in the site, it may have an impact on creating new pages with a default template, but you can see the result here:
http://www.artimental.com/63.html (Etomite V1.1)

By the way, I am talking about Jelmer's code pasted in this thread, not the original index.php file hack.

Edited by Cris D., 20 March 2009 - 10:21 PM.


#19 Psycho Mantis

    Etomite Forum Fan

  • Member
  • Pip
  • 113 posts

Posted 23 March 2009 - 09:29 AM

Hi Cris,

Thanks for your reply.
Which line do you mean? In Jelmer's snippet code or in the index.php file? I couldn't figure out where to replace your patch.

Thanks!

#20 Cris D.

    Loves Etomite Forums!

  • Developers
  • PipPipPipPip
  • 1,104 posts
  • Gender:Male

Posted 23 March 2009 - 09:47 AM

The line was for Jelmers' code but in the index.php. However, just be aware, I have not been able to get Jelmer's javascript version to stay applied to a site for a whole session. If you have a look at the demo page above, you will see that it reverts back to the original template once another link is followed. I had a play with changing the code to make it persistent, but have not finished. Perhaps Jelmer has a persistant version. Otherwise, I have not tested the original version in eto 1.1.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users