About Sitellite       Screenshots       Downloads       Forge      Documentation       Community       Support

You are here: Home / Documentation / Dynamically Switching Templates

Dynamically Switching Templates

Step 2: Determining the template to display

The code to determine the template to display should be included on every page request, so we'll want to put it into inc/conf/properties.php, which is a file that is automatically included prior to all page requests for the purpose of declaring application properties or preprocessing instructions. There are several examples already in this file by default. This is the functional equivalent to the conf/properties.php file in each app, except that this one is loaded for all apps and page requests, while the conf/properties.php file for a specific app is only loaded if that app is called.

The code we'll be including in here is as follows:

<?php

$template_set = session_get ('template_switcher');
if (! empty ($template_set)) {
	page_template_set ($template_set);
}

?>

Let's step through this code briefly:

First we retrieve the session variable 'template_switcher' using the session_get() function defined in saf.Session in the Sitellite Application Framework (SAF), which will contain the name of a specific template set if the visitor has chosen one. Then we check if it's not empty, in which case we know the user has made a selection. If so, we call page_template_set(), which is defined in saf.Misc.Document in SAF, to set the template to the one chosen by the visitor.

Step 3: Writing the template switcher

Finally, let's write the code to actually set the template when the visitor makes a selection. In a file called inc/app/templateswitcher/boxes/select/index.php, write:

<?php

session_set ('template_switcher', $parameters['tpl']);
header ('Location: ' . $_SERVER['HTTP_REFERER']);
exit;

?>

That's all there is to it. You should now be able to switch templates on your web site on the fly. Now go install or create some new templates!

Pop Quiz: Why hasn't our template switcher caused the wrong template to display for the Sitellite CMS administrative interface? Hint: Look at the file inc/app/cms/boxes/access.php and think "order of operations".



Page 1: Step 1: A template selector
Page 2: Step 2: Determining the template to display

All Tutorials

Members

Note: You can use your SitelliteForge.com account here and vice versa.

Username

Password

Forgot your password?

Not a member? Click here to register

Sitellite 5 Beta


Copyright © 2008, SIMIAN systems Inc.
All rights reserved. Privacy policy
Some of the icons on this site were created by the Gnome Project.