From 1b16c09ce8d64d813f7357e786c486c39749c086 Mon Sep 17 00:00:00 2001 From: mohrt Date: Tue, 24 Apr 2001 15:16:14 +0000 Subject: [PATCH] update documenation, bug fixes --- CREDITS | 22 ++ FAQ | 22 +- NEWS | 11 +- README | 3 +- RELEASE_NOTES | 32 +-- Smarty.class.php | 55 +++-- Smarty_Compiler.class.php | 3 +- docs.sgml | 396 ++++++++++++++++++++++++--------- libs/Smarty.class.php | 55 +++-- libs/Smarty_Compiler.class.php | 3 +- 10 files changed, 425 insertions(+), 177 deletions(-) create mode 100644 CREDITS diff --git a/CREDITS b/CREDITS new file mode 100644 index 00000000..340460e5 --- /dev/null +++ b/CREDITS @@ -0,0 +1,22 @@ +Monte Ohrt : + Concepted compiling templates into PHP scripts, wrote initial "proof of + concept" implementation, and maintains documentation & code base. + +Andrei Zmievski : + Rewrote parser from scratch and maintains code base. + +Anne Holz : + Provided creative input with respect to web design. + +Frank Kromann : + Idea of custom function ability. + +A special thanks goes to the people that have contributed other templating +solutions to the PHP community which we learned a lot from. + +A special thanks goes to the members of the php-template mailing list and +the smarty mailing list, whom sharing and bringing many ideas to the table and +contributing source code, too numerous to list. + +Rasmus Lerdorf : For starting what eventually became +the coolest programming language ever. diff --git a/FAQ b/FAQ index a1c9664e..d160e569 100644 --- a/FAQ +++ b/FAQ @@ -37,6 +37,16 @@ A: You certainly can. Smarty's cache and these cache solutions have nothing in utmost importance, we would recommend using one of these with any PHP application, using Smarty or not. +Q: Why does smarty have a built in cache? Wouldn't it be better to handle this + in a separate class? +A: Smarty's caching functionality is tightly integrated with the template + engine, making it quite a bit more flexible than a simple caching wrapper. + For instance, you can cache select portions of a template page. Let's say + you have a polling box on your site. With Smarty, you can leave the poll + dynamic and cache the rest of the page. You can also pass templates + multiple cache ids, meaning that a template can have several caches + depending on URL, cookies, etc. + Q: Is Smarty faster than ? A: It could be. One of the strengths of Smarty is that it does not need to parse the template files on every hit to the server. Version 1.3.1 has @@ -46,8 +56,8 @@ A: It could be. One of the strengths of Smarty is that it does not need to The above comparison assumes that you are not using Smarty's built-in ability to cache templates. If you are, that makes this comparison pretty unfair since Smarty will basically be displaying static content instead of - generating templates, which will speed things up, especially for compilcated - templates. + generating templates, which will really speed things up especially for + complicated tests. Q: How can I be sure to get the best performance from Smarty? A: Be sure you set $compile_check=false once your templates are initially @@ -64,7 +74,7 @@ A: Certainly. You might want to change your tag delimiters from {} to something Q: Do you have a mailing list? A: Yes. Subscribe by sending an e-mail to subscribe-smarty@lists.ispi.net. This - is also archived at http://marc.theaimsgroup.com/ under www/smarty + is also archived at http://marc.theaimsgroup.com/?l=smarty&r=1&w=2 TROUBLESHOOTING @@ -124,12 +134,6 @@ Q: The template goes into an infinite loop when I include included templates that pass local variables A: This was fixed in 1.3.2 (new global attribute) -Q: How can I tell what compiled template file corresponds to to which source - file? (Smarty 1.4.0 and later have encoded compiled filenames) -A: Compiled template files have headers that tell which source file was used to - compile it. Grep for the pathname, or use "head -2 *" to see the first two - lines of each compiled file. - Q: My ISP did not setup the PEAR repository, nor will they set it up. How do I make Smarty run without it? A: The easiest thing to do is grab all of PEAR and install it locally for your diff --git a/NEWS b/NEWS index efbdb5aa..79e85153 100644 --- a/NEWS +++ b/NEWS @@ -1,17 +1,22 @@ Version 1.4.0 ------------- + - updated GLOBAL_ASSIGN to take SCRIPT_NAME from HTTP_SERVER_VARS + instead of global variable. You can also assign several variables + in one shot with an array. (Monte) - added template filters (Monte) - added RELEASE_NOTES file to distribution (Monte) + - moved CREDITS out of manual into its own file (Monte) - added register_resource() and unregister_resource() functions (Monte) - changed syntax of variables called within section loops, supplied fix_vars.php script to fix old syntax (Andrei) - - added $check_cached_insert_tags to speed up cached pages if + - added $insert_tag_check to speed up cached pages if {insert ...} is not used (Monte) - added $compiler_class variable to allow specifying a different compiler class. (Andrei) - changed Smarty to compile templates at runtime, allowing for arbitrary - template resources. Also added fix for LOCK_EX under windows and changed - a couple file permissions for security. (Monte) + template resources. (Monte) + - added fix for LOCK_EX under windows and changed a couple file + permissions for security. (Monte) - allow arbitrary date strings to date_format, html_select_date and html_select_time (Monte) diff --git a/README b/README index 99e54c83..65bf3463 100644 --- a/README +++ b/README @@ -52,11 +52,12 @@ DESCRIPTION: {}, {{}}, , or whatever you like. * template if/elseif/else/endif constructs are passed to the PHP parser, so the if syntax can be as simple or as complex as you like. - * unlimited nesting of sections,ifs, etc. allowed + * unlimited nesting of sections, ifs, etc. allowed * it is possible to imbed PHP code right in your template files, although not recommended and doubtfully needed since the engine is so customizable. * built in caching + * arbitrary template sources (flat files, databases, etc.) COPYRIGHT: Copyright (c) 2001 ispi of Lincoln, Inc. All rights reserved. diff --git a/RELEASE_NOTES b/RELEASE_NOTES index 9ee3f9fb..90ba9fd7 100644 --- a/RELEASE_NOTES +++ b/RELEASE_NOTES @@ -15,6 +15,10 @@ fix_vars.php *.tpl' Be sure you have proper write permission, and backup your scripts first to be safe! The examples in the 1.4.0 documentation have been updated to reflect the changes. +cd /path/to/templates +cp /path/to/fix_vars.php . +find . -name "*.tpl" -exec php -q ./fix_vars.php {} \; + NEW AND IMPROVED COMPILER Smarty 1.4.0 also has a new compilation process. Instead of compiling all the @@ -42,25 +46,25 @@ etc. Just give some formats a try and see what works. Smarty now has template filters, meaning that you can run your templates through custom functions before they are compiled. This is good for things like -removing unwanted comments, "beautifiying" your template code, keep an eye on +removing unwanted comments, "beautifying" your template code, keep an eye on words or functionality people are putting in templates, translating XML -> HTML, etc. See the register_filter documentation for more info. -The format of the files created in the $compile_dir are now a bit different. -Instead of reflecting the name and path of the source template, it uses an -encoded file name, all in the top level directory. This was done to make way -for arbitrary template resources. Each compiled template has a header that -states what template resource was used to create it. From a unix command -prompt, you can use "head -2 *" to see the first two lines of each file. - UPGRADE NOTES -When upgrading to 1.4.0, you will want to first clear out all your files in the -$compile_dir, since it now uses a new format. If you have $compile_check set to -false and the compiled template does not yet exist, it will compile it -regardless of this setting. This way you can clear out the $compile_dir and not -worry about setting $compile_check to true to get the inital compilation under -way. +The format of the files created in the $compile_dir are now a bit different. +The compiled template filename is the template resource name url-encoded. +Therefore, all compiled files are now in the top directory of $compile_dir. +This was done to make way for arbitrary template resources. Each compiled +template has a header that states what template resource was used to create it. +From a unix command prompt, you can use "head -2 *" to see the first two lines +of each file. + +When upgrading to 1.4.0, you will want to clear out all your old files in the +$compile_dir. If you have $compile_check set to false and the compiled template +does not yet exist, it will compile it regardless of this setting. This way you +can clear out the $compile_dir and not worry about setting $compile_check to +true to get the inital compilation under way. 1.3.2 ----- diff --git a/Smarty.class.php b/Smarty.class.php index c189f13e..2118123b 100644 --- a/Smarty.class.php +++ b/Smarty.class.php @@ -60,9 +60,13 @@ class Smarty var $compile_dir = './templates_c'; // name of directory for compiled templates var $config_dir = './configs'; // directory where config files are located - var $global_assign = array('SCRIPT_NAME'); // variables from the GLOBALS array - // that are implicitly assigned - // to all templates + var $global_assign = array( 'HTTP_SERVER_VARS' => + array( 'SCRIPT_NAME' ) + ); // variables from the GLOBALS array + // that are implicitly assigned + // to all templates + var $undefined = null; // undefined variables in $global_assign will be + // created with this value var $compile_check = true; // whether to check for compiling step or not: // This is generally set to false once the // application is entered into production and @@ -78,12 +82,12 @@ class Smarty var $cache_dir = './cache'; // name of directory for template cache var $cache_lifetime = 3600; // number of seconds cached content will persist. // 0 = never expires. default is one hour (3600) - var $check_cached_insert_tags = true; // if you have caching turned on and you - // don't use {insert} tags anywhere - // in your templates, set this to false. - // this will tell Smarty not to look for - // insert tags and speed up cached page - // fetches. + var $insert_tag_check = true; // if you have caching turned on and you + // don't use {insert} tags anywhere + // in your templates, set this to false. + // this will tell Smarty not to look for + // insert tags and speed up cached page + // fetches. var $tpl_file_ext = '.tpl'; // template file extention @@ -139,7 +143,7 @@ class Smarty // internal vars var $_error_msg = false; // error messages. true/false - var $_tpl_vars = array(); + var $_tpl_vars = array(); // where assigned template vars are kept var $_smarty_md5 = 'f8d698aea36fcbead2b9d5359ffca76f'; // md5 checksum of the string 'Smarty' /*======================================================================*\ @@ -148,9 +152,21 @@ class Smarty \*======================================================================*/ function Smarty() { - foreach ($this->global_assign as $var_name) { - if (isset($GLOBALS[$var_name])) { - $this->assign($var_name, $GLOBALS[$var_name]); + foreach ($this->global_assign as $key => $var_name) { + if (is_array($var_name)) { + foreach ($var_name as $var) { + if (isset($GLOBALS[$key][$var])) { + $this->assign($var, $GLOBALS[$key][$var]); + } else { + $this->assign($var, $this->undefined); + } + } + } else { + if (isset($GLOBALS[$var_name])) { + $this->assign($var_name, $GLOBALS[$var_name]); + } else { + $this->assign($var_name, $this->undefined); + } } } } @@ -410,7 +426,7 @@ class Smarty ($this->cache_lifetime == 0 || (time() - filemtime($cache_file) <= $this->cache_lifetime))) { $results = $this->_read_file($cache_file); - if($this->check_cached_insert_tags) { + if($this->insert_tag_check) { $results = $this->_process_cached_inserts($results); } if ($display) { @@ -500,8 +516,8 @@ class Smarty \*======================================================================*/ function _fetch_compile_path($tpl_file, &$compile_path) { - // for now, everything is in $compile_dir - $compile_path = $this->compile_dir.'/'.md5($tpl_file).'.php'; + // everything is in $compile_dir + $compile_path = $this->compile_dir.'/'.urlencode($tpl_file).'.php'; return true; } @@ -511,7 +527,7 @@ class Smarty \*======================================================================*/ function _compiled_template_exists($include_path) { - // for now, everything is in $compile_dir + // everything is in $compile_dir return file_exists($include_path); } @@ -521,7 +537,7 @@ class Smarty \*======================================================================*/ function _fetch_compiled_template_timestamp($include_path) { - // for now, everything is in $compile_dir + // everything is in $compile_dir return filemtime($include_path); } @@ -531,7 +547,7 @@ class Smarty \*======================================================================*/ function _write_compiled_template($compile_path, $template_compiled) { - // for now, we save everything into $compile_dir + // we save everything into $compile_dir $this->_write_file($compile_path, $template_compiled); return true; } @@ -542,6 +558,7 @@ class Smarty \*======================================================================*/ function _fetch_template_source($tpl_path, &$template_source, &$template_timestamp) { + // split tpl_path by the first colon $tpl_path_parts = explode(':', $tpl_path, 2); diff --git a/Smarty_Compiler.class.php b/Smarty_Compiler.class.php index 61092a88..a4436828 100644 --- a/Smarty_Compiler.class.php +++ b/Smarty_Compiler.class.php @@ -53,6 +53,7 @@ class Smarty_Compiler extends Smarty { \*======================================================================*/ function _compile_file($tpl_file, $template_source, &$template_compiled) { + // run template source through functions registered in filter_functions if(is_array($this->filter_functions) && count($this->filter_functions) > 0) { foreach($this->filter_functions as $curr_func) { @@ -64,7 +65,7 @@ class Smarty_Compiler extends Smarty { } } - $this->_current_file = $template_source; + $this->_current_file = $tpl_file; $this->_current_line_no = 1; $ldq = preg_quote($this->left_delimiter, '!'); $rdq = preg_quote($this->right_delimiter, '!'); diff --git a/docs.sgml b/docs.sgml index 7fdb41f1..9a32646e 100644 --- a/docs.sgml +++ b/docs.sgml @@ -85,7 +85,8 @@ syntax. So what if we combined the two strengths? Thus, Smarty was born... - What is Smarty? + + What is Smarty? Smarty is a template engine for PHP. One of the unique aspects about Smarty that sets it apart from other templating solutions is that it @@ -116,6 +117,7 @@ although this may not be needed (nor recommended) since the engine is so customizable. Built-in caching support (new in 1.3.0) + Arbitrary template sources (new in 1.4.0) @@ -359,6 +361,15 @@ chmod 700 cache added to Smarty 1.3.0. + + $insert_tag_check + + If you have $caching enabled and you do not use the insert tag anywhere in your + templates, set this to false. This saves the insert tag search, + speeding up cached page fetches. + + $tpl_file_ext @@ -461,7 +472,7 @@ chmod 700 cache These functions are used in the PHP portion of your application. - Smarty API Functions + API Functions assign @@ -575,39 +586,6 @@ $smarty->clear_assign(array("Name","Address","Zip")); // clear all assigned variables $smarty->clear_all_assign(); - - - - - register_function - - - void register_function - string funcname - string funcimpl - - - - Use this to dynamically register functions with Smarty. Pass in - the template function name, followed by the actual PHP function - name that it will map to. - - -register_function - - -$smarty->register_function("date_now","print_current_date"); - -function print_current_date ($params) { - extract($params); - if(empty($format)) - $format="%b %e, %Y"; - echo strftime($format,time()); -} - -// now you can use this in Smarty to print the current date: {date_now} -// or, {date_now format="%Y/%m/%d"} to format it. - @@ -661,6 +639,39 @@ $smarty->clear_cache("index.tpl","CACHEID"); $smarty->clear_all_cache(); + + + + + register_function + + + void register_function + string funcname + string funcimpl + + + + Use this to dynamically register functions with Smarty. Pass in + the template function name, followed by the actual PHP function + name that it will map to. + + +register_function + + +$smarty->register_function("date_now","print_current_date"); + +function print_current_date ($params) { + extract($params); + if(empty($format)) + $format="%b %e, %Y"; + echo strftime($format,time()); +} + +// now you can use this in Smarty to print the current date: {date_now} +// or, {date_now format="%Y/%m/%d"} to format it. + @@ -709,7 +720,7 @@ $smarty->unregister_modifier("fetch"); $smarty->register_modifier("sslash","stripslashes"); -// now you can use {$var} to strip slashes from variables +// now you can use {$var|sslash} to strip slashes from variables @@ -999,7 +1010,7 @@ echo $output; - Smarty Template Resources + Template Resources Your templates may come from a variety of sources. When you display or fetch a template, or when you include a template from within another @@ -1060,7 +1071,7 @@ $smarty->display("file:/path/to/my/templates/menu.tpl"); retrieving templates from a database. You do this by first registering your resource handler function, then creating your function to get the template. Smarty expects your function to be of - this form: funcname($tpl_name,&tpl_source, &$tpl_timestamp) + this form: funcname($tpl_name,&$tpl_source, &$tpl_timestamp) {} $tpl_name is passed into the function, and your function should populate $tpl_source and $tpl_timestamp with the template source and the last modified time of the template respectively. @@ -1083,6 +1094,7 @@ function get_db_template ($tpl_name, &$tpl_source, &$tpl_timestamp) { return true; } +// register the resource name "db" $smarty->register_resource("db" => "get_db_template"); // using resource from php script @@ -1096,7 +1108,7 @@ $smarty->display("db:index.tpl"); - Smarty Template Filters + Template Filters Template filters are PHP functions that your templates are ran through before they are compiled. This is good for preprocessing your templates @@ -3074,19 +3086,17 @@ OUTPUT: Variable Modifiers - Variable modifiers are a bit different than - custom functions. - Variable modifiers alter variable contents before they are displayed to - the template. All modifiers will get the value of the variable as the - first argument, and must return a single value. Modifier parameters are - separated by colons. Any additional parameters passed to a modifier are - passed as-is positionally, much like calling a PHP function. You can - also use native PHP functions as modifiers, but only if they expect the - correct arguments. If they do not, you can always write a wrapper - function in Smarty to get what you want (date_format is a wrapper - function to strftime() for example.) You can chain as many modifiers - together on a variable as you like, separating each with a vertical - pipe "|". + Variable modifiers are a bit different than custom functions. Variable modifiers + alter variable contents before they are displayed to the template. All + modifiers will get the value of the variable as the first argument, and + must return a single value. Modifier parameters are separated by + colons. Any additional parameters passed to a modifier are passed as-is + positionally, much like calling a PHP function. You can also use native + PHP functions as modifiers, but only if they expect the correct + arguments. If they do not, you can always write a wrapper function in + Smarty to get what you want. You can chain as many modifiers together + on a variable as you like, separating each with a vertical pipe "|". NOTE: if you apply a modifier to an array instead of a single value @@ -3105,7 +3115,7 @@ OUTPUT: {$articleTitle} -{$articleTitle} +{$articleTitle|capitalize} OUTPUT: @@ -3126,7 +3136,7 @@ Police Begin Campaign To Rundown Jaywalkers. {$articleTitle} -{$articleTitle} +{$articleTitle|count_characters} OUTPUT: @@ -3147,7 +3157,7 @@ Cold Wave Linked to Temperatures {$articleTitle} -{$articleTitle} +{$articleTitle|count_paragraphs} OUTPUT: @@ -3170,7 +3180,7 @@ Man is Fatally Slain. Death Causes Loneliness, Feeling of Isolation. {$articleTitle} -{$articleTitle} +{$articleTitle|count_sentences} OUTPUT: @@ -3191,7 +3201,7 @@ Two Soviet Ships Collide - One Dies. Enraged Cow Injures Farmer with Axe. {$articleTitle} -{$articleTitle} +{$articleTitle|count_words} OUTPUT: @@ -3242,8 +3252,8 @@ Dealers Will Hear Car Talk at Noon. {$currentDate} -{$currentDate} -{$currentDate} +{$currentDate|date_format:"%A, %B %e, %Y"} +{$currentDate|date_format:"%H:%M:%S"} OUTPUT: @@ -3382,7 +3392,7 @@ system's manpage for a full list of valid specifiers. {* this will display "no title" (without the quotes) if $articleTitle is empty *} -{$articleTitle} +{$articleTitle|default:"no title"} OUTPUT: @@ -3432,9 +3442,9 @@ no title {$articleTitle} -{$articleTitle} -{$articleTitle} -{$articleTitle} +{$articleTitle|escape} +{$articleTitle|escape:"html"} +{$articleTitle|escape:"url"} OUTPUT: @@ -3456,7 +3466,7 @@ Stiff+Opposition+Expected+to+Casketless+Funeral+Plan {$articleTitle} -{$articleTitle} +{$articleTitle|lower} OUTPUT: @@ -3510,8 +3520,8 @@ two convicts evade noose, jury hung. {$articleTitle} -{$articleTitle} -{$articleTitle} +{$articleTitle|replace:"Garden":"Vineyard"} +{$articleTitle|replace:" ":" "} OUTPUT: @@ -3561,8 +3571,8 @@ Child's Stool Great for Use in Garden. {$articleTitle} -{$articleTitle} -{$articleTitle} +{$articleTitle|spacify} +{$articleTitle|spacify:"^^"} OUTPUT: @@ -3611,8 +3621,8 @@ S^^o^^m^^e^^t^^h^^i^^n^^g^^ ^^W^^e^^n^^t^^ ^^W^^r^^o^^n^^g^^ ^^i^^n^^ ^^J^^e^^t^ {$number} -{$number} -{$number} +{$number|string_format:"%.2f"} +{$number|string_format:"%d"} OUTPUT: @@ -3633,7 +3643,7 @@ OUTPUT: {$articleTitle} -{$articleTitle} +{$articleTitle|strip_tags} OUTPUT: @@ -3700,10 +3710,38 @@ Blind Woman Gets New Kidney from Dad she Hasn't Seen in years. truncate +{$articleTitle} +{$articleTitle|truncate} +{$articleTitle|truncate:30} +{$articleTitle|truncate:30:""} +{$articleTitle|truncate:30:"---"} +{$articleTitle|truncate:30:"":true} +{$articleTitle|truncate:30:"...":true} + +OUTPUT: + +Two Sisters Reunite after Eighteen Years at Checkout Counter. +Two Sisters Reunite after Eighteen Years at Checkout Counter. +Two Sisters Reunite after... +Two Sisters Reunite after +Two Sisters Reunite after--- +Two Sisters Reunite after Eigh +Two Sisters Reunite after E... + + + + + + upper + + This is used to uppercase a variable. + + +upper + {$articleTitle} -{$articleTitle} -{$articleTitle} +{$articleTitle|upper} OUTPUT: @@ -3726,17 +3764,129 @@ IF STRIKE ISN'T SETTLED QUICKLY IT MAY LAST A WHILE. {$articleTitle} -{$articleTitle} -{$articleTitle} -{$articleTitle} -{$articleTitle} +{$articleTitle|upper|spacify} +{$articleTitle|lower|spacify|truncate} +{$articleTitle|lower|truncate:30|spacify} +{$articleTitle|lower|spacify|truncate:30:". . ."} + + +OUTPUT: + +Smokers are Productive, but Death Cuts Efficiency. +S M O K E R S A R E P R O D U C T I V E , B U T D E A T H C U T S E F F I C I E N C Y . +s m o k e r s a r e p r o d u c t i v e , b u t d e a t h c u t s... +s m o k e r s a r e p r o d u c t i v e , b u t . . . +s m o k e r s a r e p. . . + + + + + + Creating your own Variable Modifiers + + Creating your own modifiers is a fairly straight forward process. + The best way is to look at the ones that come with Smarty as + examples. The function names begin with smarty_mod_ and they are + located in the Smarty.addons.php file. + + + add your modifier to the Smarty.addons.php file. + It is recommended that you prepend your function name + with smarty_mod_ + map a template modifier name to your PHP function. + This is done at the top of the Smarty.class.php file + in the $custom_mods array. + Thats it! you can now use that modifier + from within Smarty. + + + You can also add modifiers programatically with the register_modifier API. + + + + + + Troubleshooting + + + Smarty/PHP errors + + Smarty can catch many errors such as missing tag attributes + or malformed variable names. If this happens, you will see an error + similar to the following: + + + +Smarty errors + + +Warning: Smarty: [in index.tpl line 4]: syntax error: unknown tag - '%blah' + in /path/to/smarty/Smarty.class.php on line 1041 + +Fatal error: Smarty: [in index.tpl line 28]: syntax error: missing section name + in /path/to/smarty/Smarty.class.php on line 1041 + + + + + + Smarty shows you the template name, the line number and the error. + After that, the error consists of the actual line number in the Smarty + class that the error occured. + + + + There are certain errors that Smarty cannot catch, such as missing + close tags. These types of errors usually end up in PHP compile-time + parsing errors. + + + +PHP parsing errors + + +Parse error: parse error in /path/to/smarty/templates_c/index.tpl.php on line 75 + + + + + + When you encounter a PHP parsing error, the error line number will + correspond to the compiled PHP script, not the template itself. Usually + you can look at the template and spot the syntax error. Here are some + common things to look for: missing close tags for {if}{/if} or + {section}{/section}, or syntax of logic within an {if} tag. If you + can't find the error, you might have to open the compiled PHP file and + go to the line number to figure out where the corresponding error is in + the template. + + + + + Tips & Tricks + + + + Dates + + As a rule of thumb, always pass dates to Smarty as timestamps. + This allows template designers to use date_format for full control over date + formatting, and also makes it easy to compare dates if necessary. + + +using date_format + + +{$startDate|date_format} OUTPUT: Jan 4, 2001 -{$startDate} +{$startDate|date_format:"%Y/%m/%d"} OUTPUT: @@ -3777,8 +3927,65 @@ function makeTimeStamp($year="",$month="",$day="") - + + WAP/WML + + WAP/WML templates require a php Content-Type header to be passed along + with the template. The easist way to do this would be to write a custom + function that prints the header. If you are using caching, that won't + work so we'll do it using the insert tag (remember insert tags are not + cached!) Be sure that there is nothing output to the browser before the + template, or else the header may fail. + + +using insert to write a WML Content-Type header + + +// be sure apache is configure for the .wml extensions! +// put this function somewhere in your application, or in Smarty.addons.php +function insert_header() { + // this function expects $content argument + extract(func_get_arg(0)); + if(empty($content)) + return; + header($content); + return; +} + +// your Smarty template _must_ begin with the insert tag example: + +{insert name=header content="Content-Type: text/vnd.wap.wml"} + +<?xml version="1.0"?> +<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml"> + +<!-- begin new wml deck --> +<wml> +<!-- begin first card --> +<card> +<do type="accept"> +<go href="#two"/> +</do> +<p> +Welcome to WAP with Smarty! +Press OK to continue... +</p> +</card> +<!-- begin second card --> +<card id="two"> +<p> +Pretty easy isn't it? +</p> +</card> +</wml> + + + + + + + Resources @@ -3805,35 +4012,4 @@ function makeTimeStamp($year="",$month="",$day="") /usr/local/lib/php. Windows users check C:/php/pear. - - CREDITS - - Monte Ohrt <monte@ispi.net>: Concepted compiling templates into PHP - scripts, wrote initial "proof of concept" implementation, and maintains - documentation. - - - Andrei Zmievski <andrei@ispi.net>: Rewrote parser from scratch and - added other features too numerous to mention. - - - Anne Holz <anne@ispi.net>: Provided creative input with respect - to web design. - - - Frank Kromann <fmk@php.net>: Idea of custom function ability. - - - A special thanks goes to the people that have contributed other templating - solutions to the PHP community which we learned a lot from. - - - A special thanks goes to the members of the php-template mailing list and - the smarty mailing list, whom sharing and bringing many ideas to the table. - - - Rasmus Lerdorf <rasmus@php.net>: For starting what eventually became - the coolest programming language ever. - - diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index c189f13e..2118123b 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -60,9 +60,13 @@ class Smarty var $compile_dir = './templates_c'; // name of directory for compiled templates var $config_dir = './configs'; // directory where config files are located - var $global_assign = array('SCRIPT_NAME'); // variables from the GLOBALS array - // that are implicitly assigned - // to all templates + var $global_assign = array( 'HTTP_SERVER_VARS' => + array( 'SCRIPT_NAME' ) + ); // variables from the GLOBALS array + // that are implicitly assigned + // to all templates + var $undefined = null; // undefined variables in $global_assign will be + // created with this value var $compile_check = true; // whether to check for compiling step or not: // This is generally set to false once the // application is entered into production and @@ -78,12 +82,12 @@ class Smarty var $cache_dir = './cache'; // name of directory for template cache var $cache_lifetime = 3600; // number of seconds cached content will persist. // 0 = never expires. default is one hour (3600) - var $check_cached_insert_tags = true; // if you have caching turned on and you - // don't use {insert} tags anywhere - // in your templates, set this to false. - // this will tell Smarty not to look for - // insert tags and speed up cached page - // fetches. + var $insert_tag_check = true; // if you have caching turned on and you + // don't use {insert} tags anywhere + // in your templates, set this to false. + // this will tell Smarty not to look for + // insert tags and speed up cached page + // fetches. var $tpl_file_ext = '.tpl'; // template file extention @@ -139,7 +143,7 @@ class Smarty // internal vars var $_error_msg = false; // error messages. true/false - var $_tpl_vars = array(); + var $_tpl_vars = array(); // where assigned template vars are kept var $_smarty_md5 = 'f8d698aea36fcbead2b9d5359ffca76f'; // md5 checksum of the string 'Smarty' /*======================================================================*\ @@ -148,9 +152,21 @@ class Smarty \*======================================================================*/ function Smarty() { - foreach ($this->global_assign as $var_name) { - if (isset($GLOBALS[$var_name])) { - $this->assign($var_name, $GLOBALS[$var_name]); + foreach ($this->global_assign as $key => $var_name) { + if (is_array($var_name)) { + foreach ($var_name as $var) { + if (isset($GLOBALS[$key][$var])) { + $this->assign($var, $GLOBALS[$key][$var]); + } else { + $this->assign($var, $this->undefined); + } + } + } else { + if (isset($GLOBALS[$var_name])) { + $this->assign($var_name, $GLOBALS[$var_name]); + } else { + $this->assign($var_name, $this->undefined); + } } } } @@ -410,7 +426,7 @@ class Smarty ($this->cache_lifetime == 0 || (time() - filemtime($cache_file) <= $this->cache_lifetime))) { $results = $this->_read_file($cache_file); - if($this->check_cached_insert_tags) { + if($this->insert_tag_check) { $results = $this->_process_cached_inserts($results); } if ($display) { @@ -500,8 +516,8 @@ class Smarty \*======================================================================*/ function _fetch_compile_path($tpl_file, &$compile_path) { - // for now, everything is in $compile_dir - $compile_path = $this->compile_dir.'/'.md5($tpl_file).'.php'; + // everything is in $compile_dir + $compile_path = $this->compile_dir.'/'.urlencode($tpl_file).'.php'; return true; } @@ -511,7 +527,7 @@ class Smarty \*======================================================================*/ function _compiled_template_exists($include_path) { - // for now, everything is in $compile_dir + // everything is in $compile_dir return file_exists($include_path); } @@ -521,7 +537,7 @@ class Smarty \*======================================================================*/ function _fetch_compiled_template_timestamp($include_path) { - // for now, everything is in $compile_dir + // everything is in $compile_dir return filemtime($include_path); } @@ -531,7 +547,7 @@ class Smarty \*======================================================================*/ function _write_compiled_template($compile_path, $template_compiled) { - // for now, we save everything into $compile_dir + // we save everything into $compile_dir $this->_write_file($compile_path, $template_compiled); return true; } @@ -542,6 +558,7 @@ class Smarty \*======================================================================*/ function _fetch_template_source($tpl_path, &$template_source, &$template_timestamp) { + // split tpl_path by the first colon $tpl_path_parts = explode(':', $tpl_path, 2); diff --git a/libs/Smarty_Compiler.class.php b/libs/Smarty_Compiler.class.php index 61092a88..a4436828 100644 --- a/libs/Smarty_Compiler.class.php +++ b/libs/Smarty_Compiler.class.php @@ -53,6 +53,7 @@ class Smarty_Compiler extends Smarty { \*======================================================================*/ function _compile_file($tpl_file, $template_source, &$template_compiled) { + // run template source through functions registered in filter_functions if(is_array($this->filter_functions) && count($this->filter_functions) > 0) { foreach($this->filter_functions as $curr_func) { @@ -64,7 +65,7 @@ class Smarty_Compiler extends Smarty { } } - $this->_current_file = $template_source; + $this->_current_file = $tpl_file; $this->_current_line_no = 1; $ldq = preg_quote($this->left_delimiter, '!'); $rdq = preg_quote($this->right_delimiter, '!');