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