About Sitellite       Screenshots       Downloads       Forge      Documentation       Community       Support

You are here: Home / Dynamically Switching Templates

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.



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

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.