From 64697f0c5586dfadb6201b266501d5117d9c5790 Mon Sep 17 00:00:00 2001 From: mohrt Date: Wed, 15 Nov 2000 17:08:52 +0000 Subject: [PATCH] commit changes --- README | 126 +++++++++++++++++++++++++++++++++++++++--- Smarty.class.php | 90 +++++++++++++++++++++++++----- libs/Smarty.class.php | 90 +++++++++++++++++++++++++----- 3 files changed, 271 insertions(+), 35 deletions(-) diff --git a/README b/README index 3a4e22f6..b1fb762a 100644 --- a/README +++ b/README @@ -79,6 +79,8 @@ CLASS VARIABLES: (default value in parenthesis) the scripts are initially compiled. (true) + $template_dir Name of directory containing templates + $compile_dir_ext Extention to give the name of the compile directory. For example, if your templates are stored in a directory named @@ -86,7 +88,9 @@ CLASS VARIABLES: (default value in parenthesis) files will be kept in "templates_c" in the same directory. (_c) - $tpl_file_ext the extention used on template files. (.tpl) + $tpl_file_ext The extention used on template files. (.tpl) + All other files in the template directory + without this extention are ignored. $max_recursion_depth The maximum recursion depth for template files includes. This is to help catch an @@ -104,9 +108,10 @@ CLASS VARIABLES: (default value in parenthesis) $right_delimiter The right delimiter of the template syntax. (}) - $registered_functions Names of the custom template functions. - To add your own, add them to the array - here, and add the actual functions to the + $registered_functions Names of the custom template functions + that Smarty will recognize. + To add your own, add them to this array + and add the actual functions to the smarty.functions.php file. (array( "htmlesc","urlesc","default","config" );) @@ -189,6 +194,17 @@ Hello, my name is {htmlesc $Name} {include footer.tpl} +INCLUDE LOGIC: + +Smarty supports including other template files. + +* you must supply the relative path to the + included template file from the template + in use. Example: in index.tpl, the file + header.tpl is included from the same directory: + + {include header.tpl} + IF/ELSE LOGIC: Smarty supports if/else logic like so: @@ -245,6 +261,100 @@ Sections can be nested, like so: {/section} {/section} +SPECIAL FUNCTIONALITY: + +There are some special functions that determine the +current loop iteration of a section. These are + +rownum: current row, first row starting with 1 +index: current row, first row starting with 0 +odd: true if value is odd +even: true if value is even +mod: true if value is divisible by X + +Examples: + +{section name=month $var} + {if $month.rownum eq 4} + {* in 4th row of section loop *} + {/if} + {if $month.rownum.even} + {* current rownum is even *} + {/if} + {if $month.rownum.odd} + {* current rownum is odd *} + {/if} + {if $month.rownum.even.3} + {* each even row, grouped by 3. + so rows 1,2,3 are true (even), + 4,5,6 are odd (false), etc *} + {/if} + {if $month.rownum.odd.3} + {* each odd row, grouped by 3. + so rows 1,2,3 are true (odd), + 4,5,6 are even (false), etc *} + {/if} + {if $month.rownum.mod.4} + {* true if current row is divisible by 4 *} + {/if} +{/section} + +THE {strip} TAG: + +A special tag called {strip} can be used to strip out +extra space, tabs or newlines at the beginning and end +of each template row within {strip} and {/strip}. +For example: + + + + + {section name=row $weekday} + {$row.weekday}  + {/section} + + + + +This will return unwanted results because of +the extra tabs and newlines contained within the +tags. Normally to remedy this, you must run all +the lines together in the template like so: + + + +{section name=row $weekday}{$row.weekday} {/section} + + + + +As you can see, this quickly makes the template unreadable. +An alternate solution is to use the {strip} tag like so: + + + + {strip} + + {section name=row $weekday} + {$row.weekday}  + {/section} + + {/strip} + + + + +What this will do is strip out tabs, spaces and newlines +at the beginning and end of each line before outputting +the results. This helps keep the template file readable +without affecting the output. Only text between {strip} +and {/strip} is affected. + +{ldelim} AND {rdelim} TAGS: + +These are used in the template to output the literal +left and right delimiters. Normally these are +"{" and "}" if you are using the default delimiters. CUSTOM FUNCTIONS: @@ -258,8 +368,7 @@ functions that come bundled with Smarty, which are the following: {config "file.conf","var"} prints the value of a variable from a config file. -For this config function, config files are kept in a "config" directory. -Config files also must be in the following syntax: +For this "config" function, config files must be in the following syntax: var = "value" @@ -274,7 +383,7 @@ index.conf ---------- title = "My Homepage" -Name = "Gomer Pyle" +Name = "Gomer" creating your own: @@ -282,8 +391,7 @@ creating your own: To create your own custom functions, do the following: * edit the smarty.functions.php file, add your custom function. Be sure - the function name is prepended with "smarty_". The template engine - will print anything that is returned from this function. + the function name is prepended with "smarty_". * edit the smarty.class.php file, add your custom function name to the array (leave off the smarty_ prefix.) * make a mention of your custom function in a template, like so: diff --git a/Smarty.class.php b/Smarty.class.php index cb912959..09ba7e35 100644 --- a/Smarty.class.php +++ b/Smarty.class.php @@ -17,6 +17,8 @@ class Smarty // application is entered into production and // initially compiled. Leave set to true // during development. + + var $template_dir = "templates"; // name of directory for templates var $compile_dir_ext = "_c"; // the directory extention where // compiled templates are placed @@ -35,7 +37,14 @@ class Smarty // registered template functions // NOTE: leave off the "smarty_" prefix on the actual PHP function name - var $registered_functions = array( "htmlesc","urlesc","default","config" ); + var $registered_functions = array( "htmlesc", + "urlesc", + "default", + "configload", + "configclear", + "configprint", + "configset" + ); // internal vars var $errorMsg = false; // error messages @@ -266,11 +275,11 @@ class Smarty if(!($template_contents = $this->_read_file($filepath))) return false; - if(preg_match("/^(.+)\/([^\/]+)$/",$compilepath,$match)) + /* if(preg_match("/^(.+)\/([^\/]+)$/",$compilepath,$match)) { $ctpl_file_dir = $match[1]; $ctpl_file_name = $match[2]; - } + } */ if(!$this->allow_php) { @@ -290,9 +299,33 @@ class Smarty $ld = preg_quote($this->left_delimiter,"/"); $rd = preg_quote($this->right_delimiter,"/"); - $search[] = "/^".$ld."\*[^\}]+\*".$rd."$/U"; // remove template comments + $search[] = "/^".$ld."\*.*\*".$rd."$/U"; // remove template comments $replace[] = ""; - $search[] = "/(\\\$[\w\d]+)\.([\w\d]+)/"; // replace section vars with php vars + $search[] = "/(\\\$[\w\d\_]+)\.rownum\.even\.([\d]+)/"; // replace section rownum.even.digit + $replace[] = "((\\1_secvar / \\2) % 2)"; + $search[] = "/(\\\$[\w\d\_]+)\.rownum\.odd\.([\d]+)/"; // replace section rownum.even.digit + $replace[] = "!((\\1_secvar / \\2) % 2)"; + $search[] = "/(\\\$[\w\d\_]+)\.index\.even\.([\d]+)/"; // replace section index.even.digit + $replace[] = "!((\\1_secvar) % \\2)"; + $search[] = "/(\\\$[\w\d\_]+)\.index\.odd\.([\d]+)/"; // replace section index.even.digit + $replace[] = "((\\1_secvar) % \\2)"; + $search[] = "/(\\\$[\w\d\_]+)\.rownum\.mod\.([\d]+)/"; // replace section rownum.even.digit + $replace[] = "!((\\1_secvar+1) % \\2)"; + $search[] = "/(\\\$[\w\d\_]+)\.index\.mod\.([\d]+)/"; // replace section rownum.even.digit + $replace[] = "!((\\1_secvar) % \\2)"; + $search[] = "/(\\\$[\w\d\_]+)\.rownum\.even/"; // replace section rownum.even + $replace[] = "!((\\1_secvar+1) % 2)"; + $search[] = "/(\\\$[\w\d\_]+)\.rownum\.odd/"; // replace section rownum.odd + $replace[] = "((\\1_secvar+1) % 2)"; + $search[] = "/(\\\$[\w\d\_]+)\.index\.even/"; // replace section index.even + $replace[] = "!((\\1_secvar) % 2)"; + $search[] = "/(\\\$[\w\d\_]+)\.index\.odd/"; // replace section index.odd + $replace[] = "((\\1_secvar) % 2)"; + $search[] = "/(\\\$[\w\d\_]+)\.rownum/"; // replace section rownum + $replace[] = "\\1_secvar+1"; + $search[] = "/(\\\$[\w\d\_]+)\.index/"; // replace section index + $replace[] = "\\1_secvar"; + $search[] = "/(\\\$[\w\d\_]+)\.([\w\d\_]+)/"; // replace section vars $replace[] = "\$\\2[\\1_secvar]"; $search[] = "/\beq\b/i"; // replace eq with == $replace[] = "=="; @@ -306,15 +339,24 @@ class Smarty $replace[] = ">="; $search[] = "/\bne(q)?\b/i"; // replace ne or neq with != $replace[] = "!="; - $search[] = "/^".$ld."if ([^\}]+)".$rd."$/i"; // replace if tags + $search[] = "/\band\b/i"; // replace and with && + $replace[] = "&&"; + $search[] = "/\bmod\b/i"; // replace mod with % + $replace[] = "%"; + $search[] = "/\bor\b/i"; // replace or with || + $replace[] = "||"; + $search[] = "/\bnot\b/i"; // replace not with ! + $replace[] = "!"; + $search[] = "/^".$ld."if (.*)".$rd."$/Ui"; // replace if tags $replace[] = ""; $search[] = "/^".$ld."\s*else\s*".$rd."$/i"; // replace else tags $replace[] = ""; $search[] = "/^".$ld."\s*\/if\s*".$rd."$/i"; // replace /if tags $replace[] = ""; - $search[] = "/^".$ld."\s*include\s*\"?([^\s\}]+)\"?".$rd."$/i"; // replace include tags - $replace[] = ""; - $search[] = "/^".$ld."\s*section\s+name\s*=\s*\"?([\w\d]+)\"?\s+\\\$([^\}\s]+)\s*".$rd."$/i"; // replace section tags + $search[] = "/^".$ld."\s*include\s*\"?([^\s]+)\"?".$rd."$/i"; // replace include tags + /* $replace[] = ""; */ + $replace[] = "template_dir.$this->compile_dir_ext."/\\1\"); ?>"; + $search[] = "/^".$ld."\s*section\s+name\s*=\s*\"?([\w\d\_]+)\"?\s+\\\$([^\s]+)\s*".$rd."$/i"; // replace section tags $replace[] = ""; $search[] = "/^".$ld."\s*\/section\s*".$rd."$/i"; // replace /section tags $replace[] = ""; @@ -322,14 +364,19 @@ class Smarty if(count($this->registered_functions) > 0) { $funcs = implode("|",$this->registered_functions); - $search[] = "/^".$ld."\s*(".$funcs.")\s*([^\}]*)".$rd."$/i"; + // user functions without args + $search[] = "/^".$ld."\s*(".$funcs.")\s*".$rd."$/i"; + $replace[] = ""; + // user functions with args + $search[] = "/^".$ld."\s*(".$funcs.")\s+(.*)".$rd."$/i"; $replace[] = ""; } - $search[] = "/^".$ld."\s*(\\\$[^\}\s\.]+)\s*".$rd."$/"; // replace vars + $search[] = "/^".$ld."\s*(\\\$[^\s]+)\s*".$rd."$/"; // replace vars $replace[] = ""; // collect all the tags in the template - preg_match_all("/".$ld."[^\}]+".$rd."/U",$template_contents,$match); + + preg_match_all("/".$ld.".*".$rd."/U",$template_contents,$match); $template_tags = $match[0]; $template_tags_modified = preg_replace($search,$replace,$template_tags); @@ -338,9 +385,26 @@ class Smarty for($tagloop=0; $tagloopleft_delimiter,$template_contents); + $template_contents = preg_replace("/{rdelim}/",$this->right_delimiter,$template_contents); + if(!($this->_write_file($compilepath,$template_contents))) return false; - + return true; } diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index cb912959..09ba7e35 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -17,6 +17,8 @@ class Smarty // application is entered into production and // initially compiled. Leave set to true // during development. + + var $template_dir = "templates"; // name of directory for templates var $compile_dir_ext = "_c"; // the directory extention where // compiled templates are placed @@ -35,7 +37,14 @@ class Smarty // registered template functions // NOTE: leave off the "smarty_" prefix on the actual PHP function name - var $registered_functions = array( "htmlesc","urlesc","default","config" ); + var $registered_functions = array( "htmlesc", + "urlesc", + "default", + "configload", + "configclear", + "configprint", + "configset" + ); // internal vars var $errorMsg = false; // error messages @@ -266,11 +275,11 @@ class Smarty if(!($template_contents = $this->_read_file($filepath))) return false; - if(preg_match("/^(.+)\/([^\/]+)$/",$compilepath,$match)) + /* if(preg_match("/^(.+)\/([^\/]+)$/",$compilepath,$match)) { $ctpl_file_dir = $match[1]; $ctpl_file_name = $match[2]; - } + } */ if(!$this->allow_php) { @@ -290,9 +299,33 @@ class Smarty $ld = preg_quote($this->left_delimiter,"/"); $rd = preg_quote($this->right_delimiter,"/"); - $search[] = "/^".$ld."\*[^\}]+\*".$rd."$/U"; // remove template comments + $search[] = "/^".$ld."\*.*\*".$rd."$/U"; // remove template comments $replace[] = ""; - $search[] = "/(\\\$[\w\d]+)\.([\w\d]+)/"; // replace section vars with php vars + $search[] = "/(\\\$[\w\d\_]+)\.rownum\.even\.([\d]+)/"; // replace section rownum.even.digit + $replace[] = "((\\1_secvar / \\2) % 2)"; + $search[] = "/(\\\$[\w\d\_]+)\.rownum\.odd\.([\d]+)/"; // replace section rownum.even.digit + $replace[] = "!((\\1_secvar / \\2) % 2)"; + $search[] = "/(\\\$[\w\d\_]+)\.index\.even\.([\d]+)/"; // replace section index.even.digit + $replace[] = "!((\\1_secvar) % \\2)"; + $search[] = "/(\\\$[\w\d\_]+)\.index\.odd\.([\d]+)/"; // replace section index.even.digit + $replace[] = "((\\1_secvar) % \\2)"; + $search[] = "/(\\\$[\w\d\_]+)\.rownum\.mod\.([\d]+)/"; // replace section rownum.even.digit + $replace[] = "!((\\1_secvar+1) % \\2)"; + $search[] = "/(\\\$[\w\d\_]+)\.index\.mod\.([\d]+)/"; // replace section rownum.even.digit + $replace[] = "!((\\1_secvar) % \\2)"; + $search[] = "/(\\\$[\w\d\_]+)\.rownum\.even/"; // replace section rownum.even + $replace[] = "!((\\1_secvar+1) % 2)"; + $search[] = "/(\\\$[\w\d\_]+)\.rownum\.odd/"; // replace section rownum.odd + $replace[] = "((\\1_secvar+1) % 2)"; + $search[] = "/(\\\$[\w\d\_]+)\.index\.even/"; // replace section index.even + $replace[] = "!((\\1_secvar) % 2)"; + $search[] = "/(\\\$[\w\d\_]+)\.index\.odd/"; // replace section index.odd + $replace[] = "((\\1_secvar) % 2)"; + $search[] = "/(\\\$[\w\d\_]+)\.rownum/"; // replace section rownum + $replace[] = "\\1_secvar+1"; + $search[] = "/(\\\$[\w\d\_]+)\.index/"; // replace section index + $replace[] = "\\1_secvar"; + $search[] = "/(\\\$[\w\d\_]+)\.([\w\d\_]+)/"; // replace section vars $replace[] = "\$\\2[\\1_secvar]"; $search[] = "/\beq\b/i"; // replace eq with == $replace[] = "=="; @@ -306,15 +339,24 @@ class Smarty $replace[] = ">="; $search[] = "/\bne(q)?\b/i"; // replace ne or neq with != $replace[] = "!="; - $search[] = "/^".$ld."if ([^\}]+)".$rd."$/i"; // replace if tags + $search[] = "/\band\b/i"; // replace and with && + $replace[] = "&&"; + $search[] = "/\bmod\b/i"; // replace mod with % + $replace[] = "%"; + $search[] = "/\bor\b/i"; // replace or with || + $replace[] = "||"; + $search[] = "/\bnot\b/i"; // replace not with ! + $replace[] = "!"; + $search[] = "/^".$ld."if (.*)".$rd."$/Ui"; // replace if tags $replace[] = ""; $search[] = "/^".$ld."\s*else\s*".$rd."$/i"; // replace else tags $replace[] = ""; $search[] = "/^".$ld."\s*\/if\s*".$rd."$/i"; // replace /if tags $replace[] = ""; - $search[] = "/^".$ld."\s*include\s*\"?([^\s\}]+)\"?".$rd."$/i"; // replace include tags - $replace[] = ""; - $search[] = "/^".$ld."\s*section\s+name\s*=\s*\"?([\w\d]+)\"?\s+\\\$([^\}\s]+)\s*".$rd."$/i"; // replace section tags + $search[] = "/^".$ld."\s*include\s*\"?([^\s]+)\"?".$rd."$/i"; // replace include tags + /* $replace[] = ""; */ + $replace[] = "template_dir.$this->compile_dir_ext."/\\1\"); ?>"; + $search[] = "/^".$ld."\s*section\s+name\s*=\s*\"?([\w\d\_]+)\"?\s+\\\$([^\s]+)\s*".$rd."$/i"; // replace section tags $replace[] = ""; $search[] = "/^".$ld."\s*\/section\s*".$rd."$/i"; // replace /section tags $replace[] = ""; @@ -322,14 +364,19 @@ class Smarty if(count($this->registered_functions) > 0) { $funcs = implode("|",$this->registered_functions); - $search[] = "/^".$ld."\s*(".$funcs.")\s*([^\}]*)".$rd."$/i"; + // user functions without args + $search[] = "/^".$ld."\s*(".$funcs.")\s*".$rd."$/i"; + $replace[] = ""; + // user functions with args + $search[] = "/^".$ld."\s*(".$funcs.")\s+(.*)".$rd."$/i"; $replace[] = ""; } - $search[] = "/^".$ld."\s*(\\\$[^\}\s\.]+)\s*".$rd."$/"; // replace vars + $search[] = "/^".$ld."\s*(\\\$[^\s]+)\s*".$rd."$/"; // replace vars $replace[] = ""; // collect all the tags in the template - preg_match_all("/".$ld."[^\}]+".$rd."/U",$template_contents,$match); + + preg_match_all("/".$ld.".*".$rd."/U",$template_contents,$match); $template_tags = $match[0]; $template_tags_modified = preg_replace($search,$replace,$template_tags); @@ -338,9 +385,26 @@ class Smarty for($tagloop=0; $tagloopleft_delimiter,$template_contents); + $template_contents = preg_replace("/{rdelim}/",$this->right_delimiter,$template_contents); + if(!($this->_write_file($compilepath,$template_contents))) return false; - + return true; }