JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Fix db_get_value after mysql->mysqli upgrade
[wfpl.git] / dwt.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 require_once(__DIR__.'/'.'file.php');
9
10 function dwt_reset() {
11         $GLOBALS['_dwt_keys'] = array('<!-- TemplateEndEditable -->');
12         $GLOBALS['_dwt_values'] = array('');
13 }
14
15 function dwt_init() {
16         if(isset($GLOBALS['_dwt_keys']) && isset($GLOBALS['_dwt_values'])) {
17                 return;
18         }
19         dwt_reset();
20 }
21
22 function dwt_load_str($str) {
23         $GLOBALS['_dwt_template'] = $str;
24         dwt_init();
25 }
26
27 function dwt_load($filename) {
28         dwt_load_str(read_whole_file($filename));
29 }
30
31 function dwt_set_raw($name, $value) {
32         $index = dwt_find_raw($name);
33         if($index) {
34                 $GLOBALS['_dwt_keys'][$index] = $name;
35                 $GLOBALS['_dwt_values'][$index] = $value;
36         } else {
37                 $GLOBALS['_dwt_keys'][] = $name;
38                 $GLOBALS['_dwt_values'][] = $value;
39         }
40 }
41
42 function dwt_set($name, $value) {
43         dwt_set_raw("<!-- TemplateBeginEditable name=\"$name\" -->", $value);
44 }
45
46 # returns index into arrays
47 function dwt_find_raw($name) {
48         for($i = 0; $i < count($GLOBALS['_dwt_keys']); ++$i) {
49                 if($GLOBALS['_dwt_keys'][$i] == $name) {
50                         return $i;
51                 }
52         }
53         return null;
54 }
55
56 # returns index into arrays
57 function dwt_find($name) {
58         return dwt_find_raw("<!-- TemplateBeginEditable name=\"$name\" -->");
59 }
60
61 function dwt_append_raw($name, $value) {
62         $index = dwt_find_raw($name);
63         if($index !== null) {
64                 $GLOBALS['_dwt_values'][$index] .= $value;
65         } else {
66                 dwt_set_raw($name, $value);
67         }
68 }
69
70 function dwt_append($name, $value) {
71         dwt_append_raw("<!-- TemplateBeginEditable name=\"$name\" -->", $value);
72 }
73
74 function dwt_prepend_raw($name, $value) {
75         $index = dwt_find_raw($name);
76         if($index !== null) {
77                 $GLOBALS['_dwt_values'][$index] = $value . $GLOBALS['_dwt_values'][$index];
78         } else {
79                 dwt_set_raw($name, $value);
80         }
81 }
82
83 function dwt_prepend($name, $value) {
84         dwt_prepend_raw("<!-- TemplateBeginEditable name=\"$name\" -->", $value);
85 }
86
87 function dwt_get_raw($name) {
88         $index = dwt_find_raw($name);
89         if($index !== null) {
90                 return $GLOBALS['_dwt_values'][$index];
91         } else {
92                 return false;
93         }
94 }
95
96 function dwt_get($name) {
97         return dwt_get_raw("<!-- TemplateBeginEditable name=\"$name\" -->");
98 }
99
100 function dwt_output($filename = null) {
101         if($filename !== null) {
102                 dwt_load($filename);
103         }
104         print(str_replace($GLOBALS['_dwt_keys'], $GLOBALS['_dwt_values'], $GLOBALS['_dwt_template']));
105 }