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