From fa1b95982a05ca860ada827b9fbdc9def1ac8952 Mon Sep 17 00:00:00 2001 From: Kevin Chabowski Date: Wed, 21 Sep 2011 00:15:45 +0200 Subject: Added documentation, an example program and fixed some bugs... --- stupid_template_engine.php | 382 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 288 insertions(+), 94 deletions(-) (limited to 'stupid_template_engine.php') diff --git a/stupid_template_engine.php b/stupid_template_engine.php index bd6efea..2e9162f 100644 --- a/stupid_template_engine.php +++ b/stupid_template_engine.php @@ -1,5 +1,14 @@ 0 ? mk_ast($code) : array()); } +/* + * Function: parse + * Parsing a STE T/PL template. + * You only need this function, if you want to manually transcompile a template. + * + * Parameters: + * $code - The STE T/PL code. + * + * Returns: + * An abstract syntax tree, whic can be used with . + */ function parse($code) { /* Precompiling... */ @@ -224,78 +244,6 @@ function parse($code) return mk_ast($code); } -define("MODE_SOURCE", 0); -define("MODE_TRANSCOMPILED", 1); - -interface StorageAccess -{ - public function load($tpl, &$mode); - public function save($tpl, $data, $mode); -} - -class FilesystemStorageAccess implements StorageAccess -{ - protected $sourcedir; - protected $transcompileddir; - - public function __construct($src, $transc) - { - $this->sourcedir = $src; - $this->transcompileddir = $transc; - } - - public function load($tpl, &$mode) - { - $src_fn = $this->sourcedir . "/" . $tpl; - $transc_fn = $this->transcompileddir . "/" . $tpl . ".php"; - - if($mode == MODE_SOURCE) - { - $content = @file_get_contents($src_fn); - if($content === False) - throw new \Exception("Template not found."); - return $content; - } - - $src_stat = @stat($src_fn); - $transc_stat = @stat($transc_fn); - - if(($src_stat === False) and ($transc_stat === False)) - throw new \Exception("Template not found."); - else if($transc_stat === False) - { - $mode = MODE_SOURCE; - return file_get_contents($src_fn); - } - else if($src_stat === False) - { - include($transc_fn); - return $transcompile_fx; - } - else - { - if($src_stat["mtime"] > $transc_stat["mtime"]) - { - $mode = MODE_SOURCE; - return file_get_contents($src_fn); - } - else - { - include($transc_fn); - return $transcompile_fx; - } - } - } - - public function save($tpl, $data, $mode) - { - $fn = (($mode == MODE_SOURCE) ? $this->sourcedir : $this->transcompileddir) . "/" . $tpl . (($mode == MODE_TRANSCOMPILED) ? ".php" : ""); - @mkdir(dirname($fn), 0777, True); - file_put_contents($fn, ""); - chmod($fn, 0777); /* FIXME: Remove this line after debugging... */ - } -} - function indent_code($code) { return implode( @@ -630,7 +578,7 @@ $ste_builtins = array( $code .= "\$${loopname}_array = \$ste->get_var_by_name(\$${loopname}_arrayvar);\n"; $code .= "if(!is_array(\$${loopname}_array))\n\t\$${loopname}_array = array();\n"; - $code .= "\$${loopname}_counter = 0;\n"; + $code .= "\$${loopname}_counter = -1;\n"; $loopbody = "\$${loopname}_counter++;\n\$ste->set_var_by_name(\$${loopname}_valuevar, \$${loopname}_value);\n"; if(!empty($ast->params["key"])) @@ -651,11 +599,11 @@ $ste_builtins = array( }, "break" => function($ast) { - return "\$ste->break_loop();\n"; + return "throw new \\ste\\BreakException();\n"; }, "continue" => function($ast) { - return "\$ste->continue_loop();\n"; + return "throw new \\ste\\ContinueException();\n"; }, "block" => function($ast) { @@ -749,7 +697,7 @@ $ste_builtins = array( function escape_text($text) { - return addcslashes($text, "\r\n\t\$\0..\x1f\\'\x7f..\xff"); + return addcslashes($text, "\r\n\t\$\0..\x1f\\'\"\x7f..\xff"); } function _transcompile($ast) /* The real transcompile function, does not add boilerplate code. */ @@ -789,24 +737,169 @@ function _transcompile($ast) /* The real transcompile function, does not add boi $ste_transc_boilerplate = "\$outputstack = array('');\n\$outputstack_i = 0;\n"; +/* + * Function: transcompile + * Transcompiles an abstract syntax tree to PHP. + * + * Parameters: + * $ast - The abstract syntax tree to transcompile. + * + * Returns: + * PHP code. The PHP code is an anonymous function expecting a instance as its parameter and returns a string (everything that was not pached into a section). + */ function transcompile($ast) /* Transcompile and add some boilerplate code. */ { global $ste_transc_boilerplate; return "function(\$ste)\n{\n" . indent_code($ste_transc_boilerplate . _transcompile($ast) . "return array_pop(\$outputstack);") . "\n}"; } +/* + * Constants: Template modes + * + * MODE_SOURCE - The Templates source + * MODE_TRANSCOMPILED - The transcompiled template + */ +const MODE_SOURCE = 0; +const MODE_TRANSCOMPILED = 1; + +/* + * Class: StorageAccess + * An interface. + * A StorageAccess implementation is used to access the templates from any storage. + * This means, that you are not limited to store the Templates inside directories, you can also use a database or something else. + */ +interface StorageAccess +{ + /* + * Function: load + * Loading a template. + * + * Parameters: + * $tpl - The name of the template. + * &$mode - Which mode is preferred? One of the