- 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>
<body>
<h1>404 File Not Found</h1>
-<p>Sorry, ~filename.html~ doesn't seem to exist.</p>
+<p>Sorry, ~filename html~ doesn't seem to exist.</p>
</body>
</html>
<div class="calendar">
<table border="0" cellspacing="0" cellpadding="0" summary="">
<tr>
- <td colspan="7" class="year">~month_year.html~</td>
+ <td colspan="7" class="year">~month_year html~</td>
</tr>
<tr>
<th><div class="headerbox">Sun</div></th>
<th><div class="headerbox">Fri</div></th>
<th><div class="headerbox">Sat</div></th>
</tr>
- <!--~week start~-->
- <tr><!--~day start~--><!--~nonday start~-->
- <td class="nonday"><div class="daybox"> </div></td><!--~end~--><!--~empty_day start~-->
- <td class="empty_day"><div class="daybox">~day_number.html~</div></td><!--~end~--><!--~busy_day start~-->
- <td class="busy_day"><a href="~day_page.attr~">~day_number.html~</a></td><!--~end~--><!--~empty_weekend start~-->
- <td class="empty_weekend"><div class="daybox">~day_number.html~</div></td><!--~end~--><!--~busy_weekend start~-->
- <td class="busy_weekend"><a href="~day_page.attr~">~day_number.html~</a></td><!--~end~--><!--~end~-->
+ <!--~week {~-->
+ <tr><!--~day {~--><!--~nonday {~-->
+ <td class="nonday"><div class="daybox"> </div></td><!--~}~--><!--~empty_day {~-->
+ <td class="empty_day"><div class="daybox">~day_number html~</div></td><!--~}~--><!--~busy_day {~-->
+ <td class="busy_day"><a href="~day_page attr~">~day_number html~</a></td><!--~}~--><!--~empty_weekend {~-->
+ <td class="empty_weekend"><div class="daybox">~day_number html~</div></td><!--~}~--><!--~busy_weekend {~-->
+ <td class="busy_weekend"><a href="~day_page attr~">~day_number html~</a></td><!--~}~--><!--~}~-->
</tr>
- <!--~end~-->
+ <!--~}~-->
</table>
</div>
</body>
<h1>testing code/wfpl/session.php</h1>
-<!--~block start~-->
+<!--~block {~-->
<div style="border: 2px solid black; padding: 10px; margin-top: -2px">
- <h2>~message.html~</h2>
+ <h2>~message html~</h2>
<p>
<table cellpadding="5" cellspacing="0" border="1" summary="">
<tr><td colspan="4" style="font-weight: bold">wfpl_sessions table</td></tr>
- <tr><td>id</td><td>session_key</td><td>length</td><td>expires</td></tr><!--~wfpl_sessions_row start~-->
- <tr><td>~id.html~</td><td>~session_key.html~</td><td>~length.html~</td><td>~expires.html~</td></tr><!--~end~-->
+ <tr><td>id</td><td>session_key</td><td>length</td><td>expires</td></tr><!--~wfpl_sessions_row {~-->
+ <tr><td>~id html~</td><td>~session_key html~</td><td>~length html~</td><td>~expires html~</td></tr><!--~}~-->
</table>
</p>
<p>
<table cellpadding="5" cellspacing="0" border="1" summary="">
<tr><td colspan="4" style="font-weight: bold">wfpl_session_data table</td></tr>
- <tr><td>id</td><td>session_id</td><td>name</td><td>value</td></tr><!--~wfpl_session_data_row start~-->
- <tr><td>~id.html~</td><td>~session_id.html~</td><td>~name.html~</td><td>~value.html~</td></tr><!--~end~-->
+ <tr><td>id</td><td>session_id</td><td>name</td><td>value</td></tr><!--~wfpl_session_data_row {~-->
+ <tr><td>~id html~</td><td>~session_id html~</td><td>~name html~</td><td>~value html~</td></tr><!--~}~-->
</table>
</p>
</div>
-<!--~end~-->
+<!--~}~-->
</body>
</html>
sub-template. It marks a section of template which the php can decide how
many times to display (including none.) -->
- <!--~login start~-->
+ <!--~login {~-->
<h3>Please Log In Below</h3>
<form method="get" action="tem_test.php">
putting it in a text file or e-mail then leave the encoding off and
just put ~user~. -->
- <td><input type="text" name="user" value="~user.attr~"></td>
+ <td><input type="text" name="user" value="~user attr~"></td>
</tr>
<tr>
<!-- there's another template tag named pass -->
- <td><input type="password" name="pass" value="~pass.attr~"></td>
+ <td><input type="password" name="pass" value="~pass attr~"></td>
</tr>
<tr>
<!-- and here's the mark for the end of the login sub-template. -->
- <!--~end~-->
+ <!--~}~-->
<!-- Now for a more complex example of sub-templates. This whole table is in
a sub-template so php can decide to display the whole bit or not. First the
marker for the begining of the sub-template -->
- <!--~foobar_table start~-->
+ <!--~foobar_table {~-->
<h3>All your foobar:</h3>
<table summary="">
in this table, so we make a template with just one row, and mark that row
as a sub-template named foobar_row -->
- <!--~foobar_row start~-->
+ <!--~foobar_row {~-->
<tr>
<!-- sorry that the following tag looks a bit like a filename. It isn't.
It's simply asking PHP for a value called "foo" encoded as html -->
- <td>~foo.html~</td>
+ <td>~foo html~</td>
- <td><input value="~bar.attr~"></td>
+ <td><input value="~bar attr~"></td>
</tr>
<!-- Here's the end of foobar_row -->
- <!--~end~-->
+ <!--~}~-->
<tr>
<td></td>
</table>
<!-- and the end of foobar_table -->
- <!--~end~-->
+ <!--~}~-->
</body>
</html>
</head>
<body>
-<!--~main start~-->
+<!--~main {~-->
<div id="wfpl_progress_form">
<form action="http://~host~:~port~/~filename~" enctype="multipart/form-data" target="wfpl_progress_post_response" method="post">
<p>Upload a file: <input type="file" name="wfpl_uploader_file" id="wfpl_uploader_file"></p>
<div id="wfpl_progress_bar"></div>
</div>
</div>
-<!--~end~-->
+<!--~}~-->
</body>
</html>