Saturday, June 5, 2010

RTemplate for PHP: A PHP template class


Licensing

All the code here is covered by the PHPGuru license. A short summary is that for any sort of commercial use there is a small one-off licensing fee to pay, and for charity and educational use there is a reduced license fee.
You can read more about licensing here (where there's also a link to Google Checkout).

RTemplate for PHP: A PHP template class

Download the RTemplate archive Download the zip file
The example file shows you a few examples of RTemplate. And the source shows you how to achieve them.

Properties


$debug

Determines whether to show debug information or not. For normal running set this to false. For your tes environment, set it to true

$useCache

Simple boolean which controls whether to use the cache or not. I would suggest this is automatically false for development environments.

$path

The path to the directory where your templates are kept.

$cachePath

The path to your cache directory. This defaults to /tmp/. For Windows environments you will want to change this.

Methods


Constructor(string template)

The constructor. Pass it the name of the template file as the only argument.

SetCachePath(string path)

Sets the path to use as the cache. This defaults to /tmp, so you may want to change it on non-unix environments.

SetPath(string path)

Sets the path to use for the template files.

Set(string name, mixed value)

Sets a variable which you can use in the template.

Display()

Displays the template file.

Example

A small example of using RTemplate. Also see the example in the archive.

/**
    * This is some example data. It's a 2d array, indexed first numerically, and then by
    * "column name". Just like the structure returned by the PEAR::DB getAll method.
    */
    $fredriko[0]['first']  = 'fred';       $fredriko[1]['first']  = 'barney';
    $fredriko[0]['second'] = 'Barney';     $fredriko[1]['second'] = 'Jim';
    $fredriko[0]['third']  = 'time lucky'; $fredriko[1]['third']  = 'Joon';
    $fredriko[0]['fourth'] = 'Orvill';     $fredriko[1]['fourth'] = 'Joomla';
    $fredriko[0]['fifth']  = 'Loop';       $fredriko[1]['fifth']  = 'Kid';
    
    $fredriko[2]['first']  = 'Riko';  $fredriko[3]['first']  = 'Orvill';
    $fredriko[2]['second'] = 'life';  $fredriko[3]['second'] = 'Juniper';
    $fredriko[2]['third']  = 'Hop';   $fredriko[3]['third']  = 'Thing';
    $fredriko[2]['fourth'] = 'Goa';   $fredriko[3]['fourth'] = 'King';
    $fredriko[2]['fifth']  = 'Funky'; $fredriko[3]['fifth']  = 'Flop';
    
    $tpl = new RTemplate('example.html');
    $tpl->debug = true;
    $tpl->SetPath('/path/to/templates');
    
    $tpl->useCache = true;
    $tpl->SetCachePath('/tmp');

    $tpl->Set('ifVar', 1); // Used as the if condition value
    $tpl->Set('myVar', 'A simple string');
    $tpl->Set('title', 'The title');
    $tpl->Set('fredriko', $fredriko);
    
    $tpl->Display();
?>

Note on {php ... } blocks

Because of their tendency to mess up caching, these are no longer supported. You don't need them anyway - you can achieve the same effect by setting the result of the PHP code you want to run as a variable. The added boon here is that the template can then be cached.

No comments:

Post a Comment