- First you have some globally accessible array of key->value pairs which contain the data to be entered into templates
+First, create a template object:
- Then you have some template files
+ $tem = new Tem();
-tem_set($key, $value)
-tem_get($key)
-tem_run($file/template)
-tem_echo($file/template) { echo tem_run($file); }
+Then, load a template file (note: this can be done after you put the values in)
-This should work for simple templates such as: foo: '~foo~'
+ $tem->load('template.html');
+ # or
+ $tem->load_str("""
+ <head><title>~title html~</title></head>
+ <body><table>
+ <th>Letter</th><th>Is For</th>
+ <!--~alphabet_table {~-->
+ <td>~letter html~</td><td>~is_for html~</td>
+ <!--~end~-->
+ </table></body></html>""");
+Then give it some data:
- It gets trickier when you have bits in your template that need to be repeated
- (with different tags each time) and others perhaps not displayed at all.
+ $tem->set('title', 'Example Template Output');
+ $tem->set('alphabet_table', array(
+ array('letter' => 'A', 'is_for' => '<A>pple'),
+ array('letter' => 'B', 'is_for' => '<B>anana')));
- foobar.html:
+Then you can get/print the output:
-<table>
-<tr><th>foo</th><th>bar</th></tr>
-<!--~foobar_row {~--><tr><td>~foo~</td><td><input value="~bar~"></tr><!--~}~-->
-</table>
+ echo $tem->run();
-tem_load('foobar.html');
+And you should see this:
-
- the main template (with the sub-templates like foobar_row replaced with a single tag ie:
-
-<table>
-<tr><th>foo</th><th>bar</th></tr>
-~foobar_row~
-</table>
-
- Main data structure:
- key/value pairs
- template file (if template string is empty, it will be read from here)
- template string
- sub templates:
- key: foobar_row
- value: <tr><td>~foo.....html~</td><td><input value="~bar.attr~"></tr>
+ <head><title>Example Template Output</title></head>
+ <body><table>
+ <th>Letter</th><th>Is For</th>
+ <td>A</td><td>>A<pple</td>
+ <td>B</td><td>>B<anana</td>
+ </table></body></html>