JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
delete string_array.php (buggy, stupid)
[wfpl.git] / dwt.php
1 <?php
2
3 #  Copyright (C) 2007 Jason Woofenden
4 #
5 #  This program is free software: you can redistribute it and/or modify
6 #  it under the terms of the GNU General Public License as published by
7 #  the Free Software Foundation, either version 3 of the License, or
8 #  (at your option) any later version.
9 #  
10 #  This program is distributed in the hope that it will be useful,
11 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #  GNU General Public License for more details.
14 #  
15 #  You should have received a copy of the GNU General Public License
16 #  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 require_once(__DIR__.'/'.'file.php');
19
20 function dwt_reset() {
21         $GLOBALS['_dwt_keys'] = array('<!-- TemplateEndEditable -->');
22         $GLOBALS['_dwt_values'] = array('');
23 }
24
25 function dwt_init() {
26         if(isset($GLOBALS['_dwt_keys']) && isset($GLOBALS['_dwt_values'])) {
27                 return;
28         }
29         dwt_reset();
30 }
31
32 function dwt_load_str($str) {
33         $GLOBALS['_dwt_template'] = $str;
34         dwt_init();
35 }
36
37 function dwt_load($filename) {
38         dwt_load_str(read_whole_file($filename));
39 }
40
41 function dwt_set_raw($name, $value) {
42         $index = dwt_find_raw($name);
43         if($index) {
44                 $GLOBALS['_dwt_keys'][$index] = $name;
45                 $GLOBALS['_dwt_values'][$index] = $value;
46         } else {
47                 $GLOBALS['_dwt_keys'][] = $name;
48                 $GLOBALS['_dwt_values'][] = $value;
49         }
50 }
51
52 function dwt_set($name, $value) {
53         dwt_set_raw("<!-- TemplateBeginEditable name=\"$name\" -->", $value);
54 }
55
56 # returns index into arrays
57 function dwt_find_raw($name) {
58         for($i = 0; $i < count($GLOBALS['_dwt_keys']); ++$i) {
59                 if($GLOBALS['_dwt_keys'][$i] == $name) {
60                         return $i;
61                 }
62         }
63         return null;
64 }
65
66 # returns index into arrays
67 function dwt_find($name) {
68         return dwt_find_raw("<!-- TemplateBeginEditable name=\"$name\" -->");
69 }
70
71 function dwt_append_raw($name, $value) {
72         $index = dwt_find_raw($name);
73         if($index !== null) {
74                 $GLOBALS['_dwt_values'][$index] .= $value;
75         } else {
76                 dwt_set_raw($name, $value);
77         }
78 }
79
80 function dwt_append($name, $value) {
81         dwt_append_raw("<!-- TemplateBeginEditable name=\"$name\" -->", $value);
82 }
83
84 function dwt_prepend_raw($name, $value) {
85         $index = dwt_find_raw($name);
86         if($index !== null) {
87                 $GLOBALS['_dwt_values'][$index] = $value . $GLOBALS['_dwt_values'][$index];
88         } else {
89                 dwt_set_raw($name, $value);
90         }
91 }
92
93 function dwt_prepend($name, $value) {
94         dwt_prepend_raw("<!-- TemplateBeginEditable name=\"$name\" -->", $value);
95 }
96
97 function dwt_get_raw($name) {
98         $index = dwt_find_raw($name);
99         if($index !== null) {
100                 return $GLOBALS['_dwt_values'][$index];
101         } else {
102                 return false;
103         }
104 }
105
106 function dwt_get($name) {
107         return dwt_get_raw("<!-- TemplateBeginEditable name=\"$name\" -->");
108 }
109
110 function dwt_output($filename = null) {
111         if($filename !== null) {
112                 dwt_load($filename);
113         }
114         print(str_replace($GLOBALS['_dwt_keys'], $GLOBALS['_dwt_values'], $GLOBALS['_dwt_template']));
115 }
116
117 ?>