2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
4 // Copyright (c) 2003 Laurent Bedubourg
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // Authors: Laurent Bedubourg <laurent.bedubourg@free.fr>
23 class PHPTAL_Generator
26 var $_str_buffer = "";
34 var $_macros = array();
35 var $_stacks = array();
36 var $_current_macro = false;
38 var $_gettext_required = false;
40 var $_headers = false;
42 function PHPTAL_Generator($fname, $func_name)
44 $this->_fname
= $fname;
45 $this->_funcName
= $func_name;
46 $this->appendln('<?php');
47 $this->doComment(str_pad("", 78, "-"));
48 $this->doComment("This code was generate by PHPTAL based on the template source file :");
49 $this->doComment("$fname");
50 $this->doComment(str_pad("", 78, "-"));
52 $this->appendln('function ', $func_name, '(&$__tpl__)');
55 $this->appendln('$__ctx__ =& $__tpl__->getContext();');
56 $this->appendln('$__out__ = new PHPTAL_OutputControl($__ctx__, $__tpl__->getEncoding());');
57 $this->appendln('$__ctx__->set("repeat", array());');
60 function setHeaders($h)
63 if ($this->_headers
) {
64 $this->doHeadersPrint();
68 function requireGettext()
70 $this->_gettext_required
= true;
75 if (!$this->_closed
) {
76 $this->_flushOutput();
77 $this->appendln('return $__out__->toString();');
80 foreach ($this->_macros
as $name=>$code) {
81 $this->_code
.= $code;
83 if ($this->_gettext_required
) {
84 $this->_code
= preg_replace('/^<\?php/sm',
85 '<?php require_once "GetText.php";',
90 $this->_closed
= true;
95 function execute($code)
97 $this->_flushOutput();
98 $this->appendln(trim($code), ";");
101 function doComment($str)
103 $this->_flushOutput();
104 $this->appendln('// ',$str);
107 function setSource($tagName, $line)
109 $this->doComment('TAG ' . $tagName . ' AT LINE ' . $line);
110 $this->appendln('$_src_tag = "'. $tagName. '"; ', '$_src_line = '. $line. ';');
116 $this->_flushOutput();
117 $this->appendln("do {");
121 function doEndDoWhile($condition)
124 $this->appendln("} while($condition);");
127 function doWhile($condition)
129 $this->_flushOutput();
130 $this->appendln("while ($condition) {");
135 function doIf($condition)
137 $this->_flushOutput();
138 $this->appendln("if ($condition) {");
142 function doElseIf($condition)
144 $this->_str_buffer
= "";
146 $this->appendln("else if ($condition) {");
153 $this->appendln("else {");
158 function doMacroDeclare($name)
160 $this->_flushOutput();
161 $this->_stacks
[] = $this->_code
;
163 $this->_current_macro
= $name;
164 $this->_tab_save
= $this->_tab
;
166 $this->appendln('function ', $this->_funcName
,'_',$name, '(&$__tpl__)');
167 $this->appendln('{');
169 $this->appendln('$__ctx__ =& $__tpl__->getContext();');
170 $this->appendln('$__out__ = new PHPTAL_OutputControl($__ctx__, $__tpl__->getEncoding());');
171 if ($this->_headers
) {
172 $this->doHeadersPrint();
176 function doHeadersPrint()
178 $this->_flushOutput();
179 $this->doIf('! $__tpl__->_headers');
180 $str = str_replace("'", "\\'", $this->_headers
);
181 $str = "'" . $str . "'";
182 $str = PHPTAL_ES_path_in_string($str, "'");
183 $this->appendln('$__tpl__->_headers = ' . $str .';');
187 function doMacroEnd()
189 $this->_flushOutput();
190 $this->appendln('return $__out__->toString();');
192 $this->_macros
[$this->_current_macro
] = $this->_code
;
193 $this->_code
= array_pop($this->_stacks
);
194 $this->_current_macro
= false;
195 $this->_tab
= $this->_tab_save
;
198 function doAffectResult($dest, $code)
200 if ($dest[0] != '$') { $dest = '$'.$dest; }
202 $this->appendln("$dest = $code;");
206 function doPrintString()
208 $args = func_get_args();
209 $this->_str_buffer
.= join("", $args);
210 // $this->appendln('$__out__->write(', $str, ');');
213 function doPrintVar($var, $structure=false)
215 $this->_flushOutput();
216 if ($var[0] != '$') {
220 $this->appendln('$__out__->writeStructure(', $var, ');');
222 $this->appendln('$__out__->write(', $var, ');');
226 function doPrintRes($code, $structure=false)
228 $this->_flushOutput();
230 $this->appendln('$__out__->writeStructure(', $code, ');');
232 $this->appendln('$__out__->write(', $code, ');');
236 function doPrintContext($path, $structure=false)
238 $code = sprintf('$__ctx__->get(\'%s\')', $path);
239 $this->doPrintRes($code, $structure);
242 // output buffering control
245 $this->_flushOutput();
246 $this->appendln('$__out__->pushBuffer();');
249 function doOBEnd($dest)
251 $this->_flushOutput();
253 $this->appendln($dest, ' =& $__out__->popBuffer();');
258 $this->_flushOutput();
259 $this->appendln('$__out__->popBuffer();');
264 $this->_flushOutput();
265 $this->appendln('$__out__->writeStructure($__out__->popBuffer());');
268 function doOBEndInContext($dest)
270 $this->doContextSet($dest, '$__out__->popBuffer()');
274 function doReference($dest, $source)
276 $this->_flushOutput();
277 if ($dest[0] != '$') { $dest = '$'.$dest; }
278 if ($source[0] != '$') { $source = '$'.$source; }
280 $this->appendln("$dest =& $source;");
283 function doUnset($var)
285 $this->appendln("unset($var);");
288 // create a new temporary variable (non context) and return its name
289 function newTemporaryVar()
291 return '$temp_' . $this->_temp_id++
;
294 function releaseTemporaryVar($name)
296 $this->doUnset($name);
301 function doContextSet($out, $code)
303 $this->_flushOutput();
305 { $out = '"'.$out.'"'; }
307 $this->appendln("\$__ctx__->setRef($out, $code);");
310 function doContextGet($out, $path)
312 $this->_flushOutput();
314 { $out = '$' . $out; }
316 $this->appendln("$out =& \$__ctx__->get(\"$path\");");
322 $this->appendln('}');
327 $this->_flushOutput();
333 $this->_flushOutput();
334 $this->_tab
= substr($this->_tab
, 4);
339 $args = func_get_args();
340 $str = join("", $args);
341 $this->_code
.= $this->_tab
. $str . "\n";
346 $args = func_get_args();
347 $str = join("", $args);
348 $this->_code
.= $this->_tab
. $str;
351 function _flushOutput()
353 if ($this->_str_buffer
== "") return;
354 $this->_str_buffer
= str_replace("'", "\\'", $this->_str_buffer
);
355 $this->_str_buffer
= "'" . $this->_str_buffer
. "'";
356 $this->_str_buffer
= PHPTAL_ES_path_in_string($this->_str_buffer
, "'");
357 $this->appendln('$__out__->writeStructure(', $this->_str_buffer
, ');');
358 $this->_str_buffer
= "";