Super Simple Template System for PHP: Salsa Template

One of the most messy things when designing a PHP web application is the page display. Embedding application logic code with your HTML can make future maintenance of your project very unappealing, especially large complex web applications. MVC frameworks exist to help you separate your logic code from the HTML code. However using a MVC framework usually involves investing time to learn how to use the framework. Now there is a better way, I introduce the Salsa PHP Template System.

The Salsa PHP Template System is super simple template (or theme) system for PHP web applications. It's inspired by the way Drupal handles it's theme system and borrows exactly 8 lines of code from Drupal 6.x.

Usage is simple and requires including one small library of functions in your applications:


Place your HTML layouts in arbitrarily named folder underneath "themes". The name of the folder is the name of the theme that is used to render your HTML. If no name is used when rendering HTML Salsa will always fall back to and look for a default theme under a folder named "themes/default".

The HTML can then be rendered using a single function call:

$page = render_template($file_name);

Any file that is part of a theme can be referenced by using the function "template_file". For example a template file located under "themes/fancy/page.tpl.php" can be rendered using:

$file = template_file("page.tpl.php", "fancy");
$page = render_template($file);

Variables can referenced by your theme files during rendering by passing all the variables in an array when calling the render_template function:

$vars = array();
$vars['title'] = "Salsa Template Example Usage";
$vars['heading'] = "An Example Page";
$vars['copyright'] = '©' . date("Y");

$file = template_file("page.tpl.php", "fancy");
$page = render_template($file, $vars);

Salsa will extract and make avaialble all the variables you need in order to dynamically generate a view:


<title><?php print $title; ?></title>
<h4><?php print $heading; ?></h4>
<p><?php print $copyright; ?></p>

For more ways on how to use Salsa, including some advanced features take a look at the examples folder included with the archive.

Salsa PHP Template System14.12 KB
PacyWorld Button
Powered by Pacy