++++++++++++++++++++++
thorpesystems_template
++++++++++++++++++++++
this class provides simple template functionality to your php scripts. the templates can be broken down into two types of variables. this is my first attempt at creating a class using php, having only coded in php for the past 3 weeks. this is also the first time ive had to write a help file, so i hope my explinations are easy to follow.
any comments on the class would be much appreciated.
template blocks;
[tst:id=yourblock]
template vars;
{yourvar}
each template MUST start with a block. blocks run from there decleration down the page until they hit another block decleration or the end of the page. blocks can contain any number of template vars, and can be called any number of times. this allows blocks to be looped.
example;
-------
[tst:id=head]
<html>
<head>
<title>{title}</title>
</head>
<body>
<table>
[tst:id=user_details]
<td>
<tr>{name}</tr>
<tr>{mail}</tr>
</td>
[tst:id=footer]
</table>
</body>
</html>
this example show three blocks.
the head block;
--------------
<html>
<head>
<title>{title}</title>
</head>
<body>
<table>
the user_details block;
----------------------
<td>
<tr>{name}</tr>
<tr>{mail}</tr>
</td>
the footer block;
----------------
</table>
</body>
</html>
there is only three methods we need to call to create a template once the object has been instantiated.
load(string) method - loads a template file into memory.
parse(string,[array]) method - loads a block into the objects output, and replaces vars with there values.
render() method - renders the objects output to the screen
example;
-------
<?php
$tst = new thorpesystems_template(); // create the template object.
$tst->load('template.html'); // load a template into memory.
$tst->parse('head'); /*
parse a block with an id of head. passing no values for our vars.
if no values are passed to the vars contained within a block, the vars
will be stripped from the final output
*/
$tst->parse('user_details',array('name'=>'bob','mail'=>'bob@bob.com'); /*
parse a block with an id of user_details. passing the values name and mail
through an array
*/
$tst->parse('user_details',array('name'=>'bruce','mail'=>'bruce@bob.com');
$tst->parse('user_details',array('name'=>'chloe','mail'=>'chloe@bob.com');
$tst->parse('user_details',array('name'=>'simon','mail'=>'simon@bob.com'); /*
parse muliple blocks with an id of user_details. passing the values name and mail
through an array. this could also be done from within a loop.
*/
$tst->parse('foot'); // parse a block with an id of footer.
$tst->render(); // write the objects output to the screen.
?>
the above example would produce this html output;
<html>
<head>
<title></title>
</head>
<body>
<table>
<td>
<tr>bob</tr>
<tr>bob@bob.com</tr>
</td>
<td>
<tr>bruce</tr>
<tr>bruce@bob.com</tr>
</td>
<td>
<tr>chloe</tr>
<tr>chloe@bob.com</tr>
</td>
<td>
<tr>simon</tr>
<tr>simon@bob.com</tr>
</td>
</table>
</body>
</html>
note that the title is empty, as we did not pass any variables to the 'head' block.
a note on errors;
----------------
if you try to load a template that doesnt exist, the class will exit and display an error message.
if you try and parse a block that has not been defined in your template the class will exit and display an error message.
any blocks defined in your template, but not parsed from your script will be ignored, and will NOT render.
any variables defined in your template, but not parsed will be turned into blank strings. |