Username:
Password:
    Forgot your password?
Member Login

Sitellite Design Templates

Notes

Chat Loading chat status
  • Please subscribe to chat.
  • Older messages can be viewed in the chat archive.

Subscribe  |  The Lounge  |  Share Lesson

Chapter 6: Dynamically Switching Templates

A common whiz-bang feature you'll see on the cool peoples' weblogs is the ability to select the template you'd like to view their site with. While this isn't the most useful feature for most businesses (except perhaps design companies), it's still fun to be able to do. Below I'll outline the steps necessary to add this capability to any Sitellite-based web site.

Caveat: If you've ever browsed through our list of Sitellite-driven web sites, you'll notice that no two Sitellite sites look the same. As such, not all Sitellite templates are created equal, and you'll have to test to be sure the templates you install on your Sitellite-driven site are compatible with one another, and with your content.

Step 1: A template selector

In order to provide the template list to the visitor, we'll need to find out which templates are installed. We can do this with the following code:

<?php

loader_import ('saf.File.Directory');

$list = array ();
foreach (Dir::fetch ('inc/html') as $set) {
	if (strpos ($set, '.') === 0 || $set == 'admin' || $set == 'CVS') {
		continue;
	}
	$list[] = $set;
}

?>

Drop this into the file inc/app/templateswitcher/boxes/list/index.php and add an access.php file into the boxes folder below with the following contents:

; <?php /*

sitellite_access = public
sitellite_status = approved
sitellite_action = on

; */ ?>

We now have a list of templates installed on our site. However, we know that template sets can be given proper names in their own config.ini.php files, so let's present that to the user instead of the directory name. Thus, our code becomes:

<?php

loader_import ('saf.File.Directory');

$list = array ();
foreach (Dir::fetch ('inc/html') as $set) {
	if (strpos ($set, '.') === 0 || $set == 'admin' || $set == 'CVS') {
		continue;
	}

	$name = false;

	if (@file_exists ('inc/html/' . $set . '/config.ini.php')) {
		// parse the config.ini.php and look for the set_name
		// value.
		$info = parse_ini_file ('inc/html/'. $set . '/config.ini.php');
		if (isset ($info['set_name'])) {
			$name = $info['set_name'];
		}
	}
	if ($name == false) {
		// if all else fails, make the default folder name
		// more presentable.
		$name = ucwords (str_replace ('_', ' ', $set));
	}

	$list[$set] = $name;
}

?>

Now we've got a list of template sets and their proper names ready for displaying to the user. Let's dump that into a template now and see how it looks on our site:

<?php

loader_import ('saf.File.Directory');

$list = array ();
foreach (Dir::fetch ('inc/html') as $set) {
	if (strpos ($set, '.') === 0 || $set == 'admin' || $set == 'CVS') {
		continue;
	}

	$name = false;

	if (@file_exists ('inc/html/' . $set . '/config.ini.php')) {
		// parse the config.ini.php and look for the set_name
		// value.
		$info = parse_ini_file ('inc/html/'. $set . '/config.ini.php');
		if (isset ($info['set_name'])) {
			$name = $info['set_name'];
		}
	}
	if ($name == false) {
		// if all else fails, make the default folder name
		// more presentable.
		$name = ucwords (str_replace ('_', ' ', $set));
	}

	$list[$set] = $name;
}

echo template_simple ('list.spt', $list);

?>

And the template, which we'll save to inc/app/templateswitcher/html/list.spt:

<ul>
{loop obj}
	<li>
		<a href="{site/prefix}/index/templateswitcher-select-action?tpl={loop/_key}">
			{loop/_value}
		</a>
	</li>
{end loop}
</ul>

We can now insert our template selection list into our templates with the following XT code:

<h2><xt:intl>Select a template</xt:intl></h2>
<xt:box name="templateswitcher/list">
	<ul>
		<li><a href="#">Template One</a></li>
		<li><a href="#">Template Two</a></li>
	</ul>
</xt:box>

Or you can include the selection list in a sidebar with the alias "templateswitcher/list" under the Properties tab.

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".

Chapter 7: XT Tips & Caveats »