JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
clean up include paths and docs
[wfpl.git] / test / tem_test.php
1 <?php
2
3 # Copyright (C) 2005 Jason Woofenden  PUBLIC DOMAIN
4
5 # This file (along with its html template) demonstrates, documents and tests
6 # the template system.
7
8 # First we'll need the functions in wfpl/template.php:
9 require_once(__DIR__.'/../'.'template.php');
10
11 function tem_test_main() {
12         # Now grab the template (you might want to take a look at this file)
13         tem_load('tem_test.php.html');
14         # This creates a template object to store all sorts of things, and then reads
15         # in the template file and scans through it for sub-templates. Sub templates
16         # are parts of the template that may appear any number of times in the output
17         # (even zero times) such as a table row meant to hold a database record.
18
19         # This is probably not the best example, but this template contains tables for
20         # login, and for displaying some fake database content. For this silly example
21         # I decide which I'm going to display by checking if the user has submitted a
22         # username.
23         if(!isset($_REQUEST['user'])) {
24                 # tem_set() gives a key/value pair to template.php. When the template is
25                 # output (or sub-templates are run with tem_sub) any occurences of ~user~
26                 # will be replaced with 'bert'.
27                 tem_set('user', 'bert');
28
29                 # The template file contains a sub-template called 'login'. By default,
30                 # sub-templates do not display at all. They display once for each time you
31                 # call tem_sub()
32                 tem_sub('login');
33
34                 # This runs the template and prints the output. Running the template is
35                 # simply replacing all ~key~ tags with the associated value. The values are
36                 # set with tem_set() and tem_sub().
37                 tem_output();
38
39                 exit(0);
40         }
41
42         # Below is an example of using a sub-sub-template many times
43
44         # first set some values to be displayed in the row:
45         tem_set('foo', '*&^@$<>"');
46         tem_set('bar', 'one*&^@$<>"');
47
48         # Now run the row. This runs the sub-template for the row, and appends the data
49         # for the 'foobar_row' entry in the main key/value list. 
50         tem_sub('foobar_row');
51
52         # and a couple more times:
53         tem_set('foo', '"""""****"""""');
54         tem_set('bar', 'two*&^"');
55         tem_sub('foobar_row');
56         tem_set('foo', '<<<<<<&&&&&&&&amp;>>>>>');
57         tem_set('bar', 'threeeeeeee*&^@$<>"eeeeeeeeeeee');
58         tem_sub('foobar_row');
59
60         # Now we have a 'foobar_row' in the main keyval array with three rows of html in it.
61
62         # in the template foobar_row is within a bigger sub-template called
63         # 'foobar_table'. The only reason for this is so that we can have that table
64         # not display at all when we're displaying the login. This is a silly use of
65         # the templates, but I wanted to demonstrate and test a simple use of a
66         # sub-template within a sub-template.
67         tem_sub('foobar_table');
68
69
70         # Now run the main template (the body of the template file)
71         tem_output();
72 }
73
74 # if this file were in a wfpl site proper, this would get called automatically,
75 # but this is here so you can run it directly from the examples dircectory:
76 tem_test_main();