From 27ba49c57338c9d3297cdcfcac4e420f60c55cef Mon Sep 17 00:00:00 2001 From: andrey Date: Fri, 22 Feb 2002 15:12:00 +0000 Subject: [PATCH] *** empty log message *** --- AUTHORS | 15 +- NEWS | 4 +- docs.sgml | 1512 +++++++++++++++++++++++------------------------------ 3 files changed, 652 insertions(+), 879 deletions(-) diff --git a/AUTHORS b/AUTHORS index fc69abe2..07bb322a 100644 --- a/AUTHORS +++ b/AUTHORS @@ -1,10 +1,11 @@ Monte Ohrt - - idea of compiling into PHP scripts - - initial implementation - - documentation + - idea of compiling into PHP scripts + - initial implementation + - documentation Andrei Zmievski - - rewrote parser from scratch - - maintains code base - - plugin architecture - - wrote Config_File class + - rewrote parser from scratch + - maintains code base + - plugin architecture + - wrote Config_File class + - documentation diff --git a/NEWS b/NEWS index f35ddc25..c1cbfa51 100644 --- a/NEWS +++ b/NEWS @@ -1,7 +1,7 @@ Version 2.0 ----------- - - removed $tpl_file_ext variable, no longer used (Monte) - - added "hex" and "hexentity" attributes to escape modifier (Monte) + - removed $tpl_file_ext class variable, no longer used. (Monte) + - added "hex" and "hexentity" escape types to escape modifier. (Monte) - removed dependency on PEAR. (Andrei) - update popup_init to accept src attribute. (Monte, Duncan Forrest) - implemented several optimizations, speeding up Smarty significantly in diff --git a/docs.sgml b/docs.sgml index e44016f2..7805bd57 100644 --- a/docs.sgml +++ b/docs.sgml @@ -1,5 +1,5 @@ - + Smarty - the compiling PHP template engine @@ -14,7 +14,7 @@
andrei@php.net
- Version 2.0 + Version 1.5.2 2001ispi of Lincoln, Inc.
@@ -233,7 +233,6 @@ Example of installing Smarty - # be sure you are in the web server document tree # this assumes your web server runs as user "nobody" # and you are in a un*x environment @@ -244,8 +243,7 @@ chmod 700 templates_c # if you are using caching, do the following mkdir cache chown nobody:nobody cache -chmod 700 cache - +chmod 700 cache Next, try running the index.php script from your web browser. @@ -272,13 +270,10 @@ chmod 700 cache SMARTY_DIR - // set path to Smarty directory define("SMARTY_DIR","/usr/local/lib/php/Smarty/"); -require_once(SMARTY_DIR."Smarty.class.php"); - - +require_once(SMARTY_DIR."Smarty.class.php"); @@ -731,15 +726,12 @@ require_once(SMARTY_DIR."Smarty.class.php"); assign - // passing name/value pairs $smarty->assign("Name","Fred"); $smarty->assign("Address",$address); // passing an associative array -$smarty->assign(array("city" => "Lincoln","state" => "Nebraska")); - - +$smarty->assign(array("city" => "Lincoln","state" => "Nebraska")); @@ -773,15 +765,12 @@ $smarty->assign(array("city" => "Lincoln","state" => "Nebraska")); append - // passing name/value pairs $smarty->append("Name","Fred"); $smarty->append("Address",$address); // passing an associative array -$smarty->append(array("city" => "Lincoln","state" => "Nebraska")); - - +$smarty->append(array("city" => "Lincoln","state" => "Nebraska")); @@ -800,15 +789,11 @@ $smarty->append(array("city" => "Lincoln","state" => "Nebraska")); clear_assign - // clear a single variable $smarty->clear_assign("Name"); // clear multiple variables -$smarty->clear_assign(array("Name","Address","Zip")); - - - +$smarty->clear_assign(array("Name","Address","Zip")); @@ -825,11 +810,8 @@ $smarty->clear_assign(array("Name","Address","Zip")); clear_all_assign - // clear all assigned variables -$smarty->clear_all_assign(); - - +$smarty->clear_all_assign(); @@ -851,15 +833,11 @@ $smarty->clear_all_assign(); clear_cache - // clear the cache for a template $smarty->clear_cache("index.tpl"); // clear the cache for a particular cache id in an multiple-cache template -$smarty->clear_cache("index.tpl","CACHEID"); - - - +$smarty->clear_cache("index.tpl","CACHEID"); @@ -877,12 +855,8 @@ $smarty->clear_cache("index.tpl","CACHEID"); clear_all_cache - // clear the entire cache -$smarty->clear_all_cache(); - - - +$smarty->clear_all_cache(); @@ -901,15 +875,11 @@ $smarty->clear_all_cache(); clear_compiled_tpl - // clear a specific template resource $smarty->clear_compiled_tpl("index.tpl"); // clear entire compile directory -$smarty->clear_compiled_tpl(); - - - +$smarty->clear_compiled_tpl(); @@ -917,20 +887,19 @@ $smarty->clear_compiled_tpl(); void register_function - string funcname - string funcimpl + string name + string impl - 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. + Use this to dynamically register template function plugins. + Pass in the template function name, followed by the PHP + function name that implements it. register_function - -$smarty->register_function("date_now","print_current_date"); +$smarty->register_function("date_now", "print_current_date"); function print_current_date ($params) { extract($params); @@ -940,130 +909,115 @@ function print_current_date ($params) { } // now you can use this in Smarty to print the current date: {date_now} -// or, {date_now format="%Y/%m/%d"} to format it. - - +// or, {date_now format="%Y/%m/%d"} to format it. - unregister_function - - - void unregister_function - string funcname - - - - Use this to dynamically unregister functions with Smarty. Pass in - the Smarty function name. - + unregister_function + + + void unregister_function + string name + + + + Use this to dynamically unregister template function plugin. + Pass in the template function name. + unregister_function - // we don't want template designers to have access to system files -$smarty->unregister_function("fetch"); - - +$smarty->unregister_function("fetch"); - register_modifier - - - void register_modifier - string modname - string funcimpl - - - - Use this to dynamically register modifiers with Smarty. Pass in - the template modifier name, followed by the actual PHP function name - that it will map to. - + register_modifier + + + void register_modifier + string name + string impl + + + + Use this to dynamically register modifier plugin. Pass in the + template modifier name, followed by the PHP function that it + implements it. + register_modifier - // let's map PHP's stripslashes function to a Smarty modifier. $smarty->register_modifier("sslash","stripslashes"); -// now you can use {$var|sslash} to strip slashes from variables - - +// now you can use {$var|sslash} to strip slashes from variables - unregister_modifier - - - void unregister_modifier - string modname - - - - Use this to dynamically unregister modifiers with Smarty. Pass in - the template modifier name. - + unregister_modifier + + + void unregister_modifier + string name + + + + Use this to dynamically unregister modifier plugin. Pass in the + template modifier name. + unregister_modifier - // we don't want template designers to strip tags from elements -$smarty->unregister_modifier("strip_tags"); - - +$smarty->unregister_modifier("strip_tags"); - - register_resource - - - void register_resource - string resource_name - array resource_funcs - - - - Use this to dynamically register a resource to fetch templates - with Smarty. See template resources - for more information on how to setup a function for fetching - templates. - + + register_resource + + + void register_resource + string name + array resource_funcs + + + + Use this to dynamically register a resource plugin with Smarty. + Pass in the name of the resource and the array of PHP functions + implementing it. See + template resources + for more information on how to setup a function for fetching + templates. + register_resource - $smarty->register_resource("db", array("db_get_template", "db_get_timestamp", - "db_get_secure", - "db_get_trusted")); - - + "db_get_secure", + "db_get_trusted")); - - unregister_resource - - - void unregister_resource - string resource_name - - - - Use this to dynamically unregister a resource. - + + unregister_resource + + + void unregister_resource + string name + + + + Use this to dynamically unregister a resource plugin. Pass in the + name of the resource. + unregister_resource - -$smarty->unregister_resource("db"); - - +$smarty->unregister_resource("db"); @@ -1080,14 +1034,6 @@ $smarty->unregister_resource("db"); linkend="section.template.prefilters">template prefilters for more information on how to setup a prefiltering function. - -register_prefilter - - -$smarty->register_prefilter("remove_dw_comments"); - - - unregister_prefilter @@ -1100,14 +1046,6 @@ $smarty->register_prefilter("remove_dw_comments"); Use this to dynamically unregister a prefilter. - -unregister_prefilter - - -$smarty->unregister_prefilter("embed_asp_jokes"); - - - register_postfilter @@ -1123,14 +1061,6 @@ $smarty->unregister_prefilter("embed_asp_jokes"); linkend="section.template.postfilters">template postfilters for more information on how to setup a postfiltering function. - -register_postfilter - - -$smarty->register_postfilter("remove_dw_comments"); - - - unregister_postfilter @@ -1143,80 +1073,35 @@ $smarty->register_postfilter("remove_dw_comments"); Use this to dynamically unregister a postfilter. - -unregister_postfilter - - -$smarty->unregister_postfilter("embed_asp_jokes"); - - - register_compiler_function void register_compiler_function - string funcname - string funcimpl + string name + string impl - Use this to register a template compiler function. These - functions are called only during compilation of the template. - They are useful for injecting PHP code or time-sensitive static - content into the template. The first parameter specifies the - template function name, and the second one the PHP function - implementing it. If there is both a compiler function and a - custom function registered under the same name, the compiler - function has precedence. For advanced users only. + Use this to dynamically register a compiler function plugin. + Pass in the compiler function name, followed by the PHP + function that implements it. - - The compiler function is passed two parameters: the tag - argument string - basically, everything from the function - name until the ending delimiter, and the smarty object. It's - supposed to return the code to be injected into the compiled - template. - - -register_compiler_function - - -// PHP code - -$smarty->register_compiler_function("tplheader", "smarty_template_header"); - -function smarty_template_header($tag_arg_string, $smarty) -{ - return "\necho '" . $smarty->_current_file . "' compiled at " . date('Y-m-d H:M'). ";"; -} - -{* this function gets executed at compile time only *} -{tplheader } - - - - - - unregister_compiler_function - - - void unregister_compiler_function - string funcname - - - - Use this to unregister a compiler function. - - -unregister_compiler_function - - -$smarty->unregister_compiler_function("tplheader"); - - - + + unregister_compiler_function + + + void unregister_compiler_function + string name + + + + Use this to dynamically unregister a compiler function. Pass in + the name of the compiler function. + + trigger_error @@ -1253,16 +1138,13 @@ $smarty->unregister_compiler_function("tplheader"); is_cached - $smarty->caching = true; if(!$smarty->is_cached("index.tpl")) { // do database calls, assign vars here } -$smarty->display("index.tpl"); - - +$smarty->display("index.tpl"); You can also pass a cache id as an an optional second parameter @@ -1271,16 +1153,13 @@ $smarty->display("index.tpl"); is_cached with multiple-cache template - $smarty->caching = true; if(!$smarty->is_cached("index.tpl","FrontPage")) { // do database calls, assign vars here } -$smarty->display("index.tpl","FrontPage"); - - +$smarty->display("index.tpl","FrontPage"); @@ -1297,14 +1176,11 @@ $smarty->display("index.tpl","FrontPage"); get_template_vars - // get all assigned template vars $tpl_vars = $smarty->get_template_vars(); // take a look at them -var_dump($tpl_vars); - - +var_dump($tpl_vars); @@ -1319,7 +1195,7 @@ var_dump($tpl_vars); This displays the template. Supply a valid template resource + linkend="template.resources">template resource type and path. As an optional second parameter, you can pass a cache id. See the caching section for more information. @@ -1333,7 +1209,6 @@ var_dump($tpl_vars); display - include("Smarty.class.php"); $smarty = new Smarty; $smarty->caching = true; @@ -1357,18 +1232,16 @@ if(!$smarty->is_cached("index.tpl")) } // display the output -$smarty->display("index.tpl"); - +$smarty->display("index.tpl"); Use the syntax for template resources to + linkend="template.resources">template resources to display files outside of the $template_dir directory. function display template resource examples - // absolute filepath $smarty->display("/usr/local/include/templates/header.tpl"); @@ -1379,9 +1252,7 @@ $smarty->display("file:/usr/local/include/templates/header.tpl"); $smarty->display("file:C:/www/pub/templates/header.tpl"); // include from template resource named "db" -$smarty->display("db:header.tpl"); - - +$smarty->display("db:header.tpl"); @@ -1398,7 +1269,7 @@ $smarty->display("db:header.tpl"); This returns the template output instead of displaying it. Supply a valid template resource + linkend="template.resources">template resource type and path. As an optional second parameter, you can pass a cache id. See the caching section for more information. @@ -1412,7 +1283,6 @@ $smarty->display("db:header.tpl"); fetch - include("Smarty.class.php"); $smarty = new Smarty; @@ -1441,19 +1311,17 @@ $output = $smarty->fetch("index.tpl"); // do something with $output here -echo $output; - - +echo $output; - + 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 template, you supply a resource type, followed by the appropriate path - and template name. Template resources were added to Smarty 1.4.0. + and template name. Templates from $template_dir @@ -1467,7 +1335,6 @@ echo $output; using templates from $template_dir - // from PHP script $smarty->display("index.tpl"); $smarty->display("admin/menu.tpl"); @@ -1475,108 +1342,74 @@ $smarty->display("file:admin/menu.tpl"); // same as one above {* from within Smarty template *} {include file="index.tpl"} -{include file="file:index.tpl"} {* same as one above *} - - +{include file="file:index.tpl"} {* same as one above *} - - Templates from any directory - - Templates outside of the $template_dir require the file: template - resource type, followed by the absolute path and name of the - template. - - - -using templates from any directory - + + Templates from any directory + + Templates outside of the $template_dir require the file: template + resource type, followed by the absolute path and name of the + template. + + + using templates from any directory + // from PHP script $smarty->display("file:/export/templates/index.tpl"); $smarty->display("file:/path/to/my/templates/menu.tpl"); {* from within Smarty template *} -{include file="file:/usr/local/share/templates/navigation.tpl"} - - - - - - Windows Filepaths - - If you are using a Windows machine, filepaths usually include a - drive letter (C:) at the beginning of the pathname. Be sure to use - "file:" in the path to avoid namespace conflicts and get the - desired results. - - - -using templates from windows file paths - - +{include file="file:/usr/local/share/templates/navigation.tpl"} + + + Windows Filepaths + + If you are using a Windows machine, filepaths usually include a + drive letter (C:) at the beginning of the pathname. Be sure to use + "file:" in the path to avoid namespace conflicts and get the + desired results. + + + using templates from windows file paths + // from PHP script $smarty->display("file:C:/export/templates/index.tpl"); $smarty->display("file:F:/path/to/my/templates/menu.tpl"); {* from within Smarty template *} -{include file="file:D:/usr/local/share/templates/navigation.tpl"} - - - +{include file="file:D:/usr/local/share/templates/navigation.tpl"} + + - - Templates from callbacks - - You can retrieve templates using custom callbacks. Fetching - templates from a database is one of the examples. You do this by - creating the resource handler callbacks and then registering - them with Smarty. - - - There are a total of 4 functions that need to be registered for - each type of resource. Every function will receive the resource - name as the first parameter and the Smarty object as the last - parameter. The rest of parameters depend on the function. - + + Templates from other sources + + You can retrieve templates using whatever possible source you can + access with PHP: databases, sockets, LDAP, and so on. You do this + by writing resource plugin functions and registering them with + Smarty. + - - The first function is supposed to retrieve the resource. Its - second parameter is a variable passed by reference where the - source should be stored. The function is supposed to return - true if it was able to successfully retrieve - the source and false otherwise. - - - - The second function is supposed to retrieve the last - modification time of the requested resource (as a UNIX - timestamp). The second parameter is a by reference variable - where the timestamp should be stored. The function is supposed - to return true if the timestamp could be - succesfully determined, and false otherwise. - - - - The third function is supposed to return true - or false, depending on whether the requested - resource is secure or not. This function is not used for - non-template resources but should still be defined. - - - - The fourth function is supposed to return - true or false, depending - on whether the requested resource is trusted or not. This - function is not used for template resources but should still be - defined. - - - -Using template resource callbacks - + + See resource plugins + section for more information on the functions you are supposed + to provide. + + + + Note that you cannot override the built-in + file resource, but you can provide a resource + that fetches templates from the file system in some other way by + registering under another resource name. + + + + using custom resources + // from PHP script // put these function somewhere in your application @@ -1619,7 +1452,7 @@ function db_get_secure($tpl_name, &$smarty_obj) function db_get_trusted($tpl_name, &$smarty_obj) { - // not used + // not used for templates } // register the resource name "db" @@ -1632,24 +1465,21 @@ $smarty->register_resource("db", array("db_get_template", $smarty->display("db:index.tpl"); {* using resource from within Smarty template *} -{include file="db:/extras/navigation.tpl"} - - - +{include file="db:/extras/navigation.tpl"} + - - Default Template Handler Function - - You can specify a function that is used to retrieve template - contents in the event the template cannot be retrieved from its - resource. One use of this is to create templates that do not exist - on-the-fly. This was added to Smarty 1.5.2. - - - -using the default template handler function call - + + Default Template Handler Function + + You can specify a function that is used to retrieve template + contents in the event the template cannot be retrieved from its + resource. One use of this is to create templates that do not exist + on-the-fly. + + + using the default template handler function + // from PHP script // put this function somewhere in your application @@ -1672,13 +1502,11 @@ function make_template ($resource_type, $resource_name, &$template_source, } // set the default handler -$smarty->default_template_handler_func = 'make_template'; - - - - +$smarty->default_template_handler_func = 'make_template'; + - + + Template Prefilters @@ -1705,7 +1533,6 @@ $smarty->default_template_handler_func = 'make_template'; using a template prefilter - // put this in your application, or in Smarty.addons.php function remove_dw_comments($tpl_source) { return preg_replace("/<!--#.*-->/U","",$tpl_source); @@ -1716,9 +1543,7 @@ $smarty->register_prefilter("remove_dw_comments"); $smarty->display("index.tpl"); {* from within Smarty template *} -<!--# this line will get removed by the prefilter --> - - +<!--# this line will get removed by the prefilter --> @@ -1746,7 +1571,6 @@ $smarty->display("index.tpl"); using a template postfilter - // this program puts a // put this in your application, or in Smarty.addons.php function add_header_comment($tpl_source) { @@ -1759,9 +1583,7 @@ $smarty->display("index.tpl"); {* from within Smarty template *} -{* rest of template content... *} - - +{* rest of template content... *} @@ -1792,7 +1614,6 @@ $smarty->display("index.tpl"); example using MySQL as a cache source - <?php /* @@ -1895,9 +1716,6 @@ function mysql_cache_handler($action, &$smarty_obj, &$cache_content, $tpl_file=n } ?> - - - @@ -1930,7 +1748,6 @@ function mysql_cache_handler($action, &$smarty_obj, &$cache_content, $tpl_file=n displaying assigned variables - Hello {$firstname}, glad to see you could make it. <p> Your last login was on {$lastLoginDate}. @@ -1939,8 +1756,7 @@ OUTPUT: Hello Doug, glad to see you could make it. <p> -Your last login was on January 11th, 2001. - +Your last login was on January 11th, 2001. @@ -1953,7 +1769,6 @@ Your last login was on January 11th, 2001. displaying assigned associative array variables - {$Contacts.fax}<br> {$Contacts.email}<br> {* you can print arrays of arrays as well *} @@ -1966,9 +1781,6 @@ OUTPUT: zaphod@slartibartfast.com<br> 555-444-3333<br> 555-111-1234<br> - - - @@ -1981,14 +1793,11 @@ zaphod@slartibartfast.com<br> displaying arrays by index - {$Contacts[0]}<br> {$Contacts[1]}<br> {* you can print arrays of arrays as well *} {$Contacts[0][0]}<br> -{$Contacts[0][1]}<br> - - +{$Contacts[0][1]}<br> @@ -2000,16 +1809,13 @@ zaphod@slartibartfast.com<br> displaying object properties - name: {$person->name}<br> email: {$person->email}<br> OUTPUT: name: Zaphod Beeblebrox<br> -email: zaphod@slartibartfast.com<br> - - +email: zaphod@slartibartfast.com<br> @@ -2025,7 +1831,6 @@ email: zaphod@slartibartfast.com<br> displaying config variables - <html> <title>{#pageTitle#}</title> <body bgcolor="{#bodyBgColor#}"> @@ -2037,8 +1842,7 @@ email: zaphod@slartibartfast.com<br> </tr> </table> </body> -</html> - +</html> Config file variables cannot be displayed until @@ -2078,7 +1882,6 @@ email: zaphod@slartibartfast.com<br> displaying request variables - {* display the variable "page" given in the URL, or from a form using the GET method *} {$smarty.get.page} @@ -2098,9 +1901,7 @@ email: zaphod@slartibartfast.com<br> {$smarty.session.id} {* display the variable "username" from merged get/post/cookies/server/env *} -{$smarty.request.username} - - +{$smarty.request.username} @@ -2116,11 +1917,8 @@ email: zaphod@slartibartfast.com<br> using {$smarty.now} - {* use the date_format modifier to show current date and time *} -{$smarty.now|date_format:"%Y-%m-%d %H:%M:%S"} - - +{$smarty.now|date_format:"%Y-%m-%d %H:%M:%S"} @@ -2154,7 +1952,6 @@ email: zaphod@slartibartfast.com<br> function syntax - {config_load file="colors.conf"} {include file="header.tpl"} @@ -2165,8 +1962,7 @@ email: zaphod@slartibartfast.com<br> Welcome, <font color="{#fontColor#}">{$name}!</font> {/if} -{include file="footer.tpl"} - +{include file="footer.tpl"} Both built-in functions and custom functions have the same syntax @@ -2190,7 +1986,6 @@ email: zaphod@slartibartfast.com<br> function attribute syntax - {include file="header.tpl"} {include file=$includeFile} @@ -2199,8 +1994,7 @@ email: zaphod@slartibartfast.com<br> <SELECT name=company> {html_options values=$vals selected=$selected output=$output} -</SELECT> - +</SELECT> @@ -2214,7 +2008,6 @@ email: zaphod@slartibartfast.com<br> Comments - {* Smarty *} {* include the header file here *} @@ -2227,8 +2020,7 @@ email: zaphod@slartibartfast.com<br> {* display dropdown lists *} <SELECT name=company> {html_options values=$vals selected=$selected output=$output} -</SELECT> - +</SELECT> @@ -2248,7 +2040,6 @@ email: zaphod@slartibartfast.com<br> Example of config file syntax - # global variables pageTitle = "Main Menu" bodyBgColor = #000000 @@ -2271,8 +2062,6 @@ host=my.domain.com db=ADDRESSBOOK user=php-user pass=foobar - - @@ -2341,7 +2130,6 @@ pass=foobar capturing template content - {* we don't want to print a table row unless content is displayed *} {capture name=banner} {include file="get_banner.tpl"} @@ -2352,9 +2140,7 @@ pass=foobar {$return} </td> </tr> -{/if} - - +{/if} @@ -2430,7 +2216,6 @@ pass=foobar function config_load - {config_load file="colors.conf"} <html> @@ -2444,8 +2229,7 @@ pass=foobar </tr> </table> </body> -</html> - +</html> Config files may also contain sections. You can load variables from @@ -2461,7 +2245,6 @@ pass=foobar function config_load with section - {config_load file="colors.conf" section="Customer"} <html> @@ -2475,8 +2258,7 @@ pass=foobar </tr> </table> </body> -</html> - +</html> @@ -2538,13 +2320,11 @@ pass=foobar function include - {include file="header.tpl"} {* body of template goes here *} -{include file="footer.tpl"} - +{include file="footer.tpl"} You can also pass variables to included templates as attributes. @@ -2556,24 +2336,20 @@ pass=foobar function include passing variables - {include file="header.tpl" title="Main Menu" table_bgcolor="#c0c0c0"} {* body of template goes here *} -{include file="footer.tpl" logo="http://my.domain.com/logo.gif"} - - +{include file="footer.tpl" logo="http://my.domain.com/logo.gif"} Use the syntax for template resources to + linkend="template.resources">template resources to include files outside of the $template_dir directory. function include template resource examples - {* absolute filepath *} {include file="/usr/local/include/templates/header.tpl"} @@ -2584,9 +2360,7 @@ pass=foobar {include file="file:C:/www/pub/templates/header.tpl"} {* include from template resource named "db" *} -{include file="db:header.tpl"} - - +{include file="db:header.tpl"} @@ -2655,7 +2429,6 @@ pass=foobar function include_php - load_nav.php ------------- @@ -2678,9 +2451,7 @@ index.tpl {foreach item=$curr_section from=$sections} <a href="{$curr_section.url}">{$curr_section.name}</a><br> -{/foreach} - - +{/foreach} @@ -2753,11 +2524,8 @@ index.tpl function insert - {* example of fetching a banner *} -{insert name="getBanner" lid=#banner_location_id# sid=#site_id#} - - +{insert name="getBanner" lid=#banner_location_id# sid=#site_id#} In this example, we are using the name "getBanner" and passing the @@ -2819,7 +2587,6 @@ index.tpl if statements - {if $name eq "Fred"} Welcome Sir. {elseif $name eq "Wilma"} @@ -2880,9 +2647,7 @@ index.tpl {* 0=even, 1=even, 2=even, 3=odd, 4=odd, 5=odd, etc. *} {if $var is even by 3} ... -{/if} - - +{/if} @@ -2895,7 +2660,6 @@ index.tpl ldelim, rdelim - {* this will print literal delimiters out of the template *} {ldelim}funcname{rdelim} is how functions look in Smarty! @@ -2903,8 +2667,7 @@ index.tpl OUTPUT: -{funcname} is how functions look in Smarty! - +{funcname} is how functions look in Smarty! @@ -2920,7 +2683,6 @@ OUTPUT: literal tags - {literal} <script language=javascript> @@ -2937,8 +2699,7 @@ OUTPUT: // --> </script> -{/literal} - +{/literal} @@ -2953,14 +2714,11 @@ OUTPUT: php tags - {php} // including a php script directly // from the template. include("/path/to/display_weather.php"); -{/php} - - +{/php} @@ -3056,7 +2814,6 @@ OUTPUT: section - {* this example will print out all the values of the $custid array *} {section name=customer loop=$custid} id: {$custid[customer]}<br> @@ -3066,15 +2823,12 @@ OUTPUT: id: 1000<br> id: 1001<br> -id: 1002<br> - - +id: 1002<br> section loop variable - {* the loop variable only determines the number of times to loop. you can access any variable from the template within the section. This example assumes that $custid, $name and $address are all @@ -3100,15 +2854,12 @@ address: 417 Mulberry ln<br> id: 1002<br> name: Jane Munson<br> address: 5605 apple st<br> -<p> - - +<p> section names - {* the name of the section can be anything you like, and it is used to reference the data within the section *} {section name=mydata loop=$custid} @@ -3116,15 +2867,12 @@ address: 5605 apple st<br> name: {$name[mydata]}<br> address: {$address[mydata]}<br> <p> -{/section} - - +{/section} nested sections - {* sections can be nested as deep as you like. With nested sections, you can access complex data structures, such as multi-dimensional arrays. In this example, $contact_type[customer] is an array of @@ -3162,16 +2910,12 @@ address: 5605 apple st<br> home phone: 555-555-5555<br> cell phone: 555-555-5555<br> e-mail: jane@mydomain.com<br> -<p> - - - +<p> sections and associative arrays - {* This is an example of printing an associative array of data within a section *} {section name=customer loop=$contacts} @@ -3195,10 +2939,7 @@ e-mail: jack@mydomain.com<p> name: Jane Munson<br> home phone: 555-555-5555<br> cell phone: 555-555-5555<br> -e-mail: jane@mydomain.com<p> - - - +e-mail: jane@mydomain.com<p> @@ -3206,15 +2947,12 @@ e-mail: jane@mydomain.com<p> sectionelse - {* sectionelse will execute if there are no $custid values *} {section name=customer loop=$custid} id: {$custid[customer]}<br> {sectionelse} there are no values in $custid. -{/section} - - +{/section} Sections also have their own variables that handle section properties. @@ -3252,8 +2990,7 @@ e-mail: jane@mydomain.com<p> 0 id: 1000<br> 1 id: 1001<br> 2 id: 1002<br> - - + @@ -3282,8 +3019,7 @@ e-mail: jane@mydomain.com<p> The customer id changed<br> 2 id: 1002<br> The customer id changed<br> - - + @@ -3313,8 +3049,7 @@ e-mail: jane@mydomain.com<p> The customer id will change<br> 2 id: 1002<br> The customer id will change<br> - - + @@ -3353,8 +3088,7 @@ e-mail: jane@mydomain.com<p> current loop iteration: 3 9 id: 1002<br> The customer id will change<br> - - + @@ -3387,8 +3121,7 @@ e-mail: jane@mydomain.com<p> <tr><td>1 id: 1001</td></tr> <tr><td>2 id: 1002</td></tr> </table> - - + @@ -3421,8 +3154,7 @@ e-mail: jane@mydomain.com<p> <tr><td>1 id: 1001</td></tr> <tr><td>2 id: 1002</td></tr> </table> - - + @@ -3444,8 +3176,7 @@ e-mail: jane@mydomain.com<p> 1 id: 1000<br> 2 id: 1001<br> 3 id: 1002<br> - - + @@ -3470,8 +3201,7 @@ e-mail: jane@mydomain.com<p> 2 id: 1002<br> There were 3 customers shown above. - - + @@ -3505,8 +3235,7 @@ e-mail: jane@mydomain.com<p> 3 id: 1002<br> the section was shown. - - + @@ -3534,8 +3263,7 @@ e-mail: jane@mydomain.com<p> 4 id: 1002<br> There were 3 customers shown above. - - + @@ -3613,7 +3341,6 @@ e-mail: jane@mydomain.com<p> foreach - {* this example will print out all the values of the $custid array *} {foreach from=$custid item=curr_id} id: {$curr_id}<br> @@ -3623,15 +3350,12 @@ OUTPUT: id: 1000<br> id: 1001<br> -id: 1002<br> - - +id: 1002<br> foreach key - {* The key contains the key for each looped value assignment looks like this: @@ -3654,9 +3378,7 @@ fax: 2<br> cell: 3<br> phone: 555-4444<br> fax: 555-3333<br> -cell: 760-1234<br> - - +cell: 760-1234<br> @@ -3679,7 +3401,6 @@ cell: 760-1234<br> strip tags - {* the following will be all run into one line upon output *} {strip} <table border=0> @@ -3696,9 +3417,7 @@ cell: 760-1234<br> OUTPUT: -<table border=0><tr><td><A HREF="http://my.domain.com"><font color="red">This is a test</font></A></td></tr></table> - - +<table border=0><tr><td><A HREF="http://my.domain.com"><font color="red">This is a test</font></A></td></tr></table> Notice that in the above example, all the lines begin and end @@ -3761,16 +3480,13 @@ OUTPUT: assign - {assign var="name" value="Bob"} The value of $name is {$name}. OUTPUT: -The value of $name is Bob. - - +The value of $name is Bob. @@ -3854,7 +3570,6 @@ The value of $name is Bob. counter - {* initialize the count *} {counter start=0 skip=2 print=false} @@ -3868,9 +3583,7 @@ OUTPUT: 2<br> 4<br> 6<br> -8<br> - - +8<br> @@ -3936,7 +3649,6 @@ OUTPUT: fetch - {* include some javascript in your template *} {fetch file="/export/httpd/www.domain.com/docs/navbar.js"} @@ -3950,10 +3662,7 @@ OUTPUT: {fetch file="http://www.myweather.com/68502/" assign="weather"} {if $weather ne ""} <b>{$weather}</b> -{/if} - - - +{/if} @@ -4019,7 +3728,6 @@ OUTPUT: html_options - {* assume that $cust_ids, and $cust_names are arrays of values, and $customer_id may or may not be set to a value *} @@ -4044,9 +3752,7 @@ OUTPUT: <option value="1001" selected>Jack Smith<option> <option value="1002">Jane Johnson<option> <option value="1003">Charlie Brown<option> -</select> - - +</select> @@ -4238,7 +3944,6 @@ OUTPUT: html_select_date - {html_select_date} @@ -4293,9 +3998,7 @@ OUTPUT: </select> <select name="Date_Year"> <option value="2001" selected>2001</option> -</select> - - +</select> @@ -4303,7 +4006,6 @@ OUTPUT: html_select_date - {html_select_date prefix="StartDate" time=$time start_year=1995 end_year=2001 display_days=false} OUTPUT: @@ -4330,9 +4032,7 @@ OUTPUT: <option value="1999">1999</option> <option value="2000" selected>2000</option> <option value="2001">2001</option> -</select> - - +</select> @@ -4435,7 +4135,6 @@ OUTPUT: html_select_time - {html_select_time use_24_hours=true} @@ -4594,9 +4293,7 @@ OUTPUT: <select name="Time_Meridian"> <option value="am" selected>AM</option> <option value="pm">PM</option> -</select> - - +</select> @@ -4674,7 +4371,6 @@ OUTPUT: math - {* $height=4, $width=5 *} {math equation="x + y" x=$height y=$width} @@ -4711,9 +4407,7 @@ OUTPUT: OUTPUT: -9.44 - - +9.44 @@ -5127,7 +4821,6 @@ OUTPUT: popup - {* popup_init must be called once at the top of the page *} {popup_init} @@ -5140,9 +4833,7 @@ text="<UL><LI>links<LI>pages<LI>images</UL>" snapx OUTPUT: -(See the Smarty official web site for working examples.) - - +(See the Smarty official web site for working examples.) @@ -5176,16 +4867,13 @@ OUTPUT: capitalize - {$articleTitle} {$articleTitle|capitalize} OUTPUT: Police begin campaign to rundown jaywalkers. -Police Begin Campaign To Rundown Jaywalkers. - - +Police Begin Campaign To Rundown Jaywalkers. @@ -5197,16 +4885,13 @@ Police Begin Campaign To Rundown Jaywalkers. count_characters - {$articleTitle} {$articleTitle|count_characters} OUTPUT: Cold Wave Linked to Temperatures -32 - - +32 @@ -5218,7 +4903,6 @@ Cold Wave Linked to Temperatures count_paragraphs - {$articleTitle} {$articleTitle|count_paragraphs} @@ -5227,9 +4911,7 @@ OUTPUT: War Dims Hope for Peace. Child's Death Ruins Couple's Holiday. Man is Fatally Slain. Death Causes Loneliness, Feeling of Isolation. -2 - - +2 @@ -5241,16 +4923,13 @@ Man is Fatally Slain. Death Causes Loneliness, Feeling of Isolation. count_sentences - {$articleTitle} {$articleTitle|count_sentences} OUTPUT: Two Soviet Ships Collide - One Dies. Enraged Cow Injures Farmer with Axe. -2 - - +2 @@ -5262,16 +4941,13 @@ Two Soviet Ships Collide - One Dies. Enraged Cow Injures Farmer with Axe. count_words - {$articleTitle} {$articleTitle|count_words} OUTPUT: Dealers Will Hear Car Talk at Noon. -7 - - +7 @@ -5313,7 +4989,6 @@ Dealers Will Hear Car Talk at Noon. date_format - {$currentDate} {$currentDate|date_format:"%A, %B %e, %Y"} {$currentDate|date_format:"%H:%M:%S"} @@ -5322,9 +4997,7 @@ OUTPUT: Feb 6, 2001 Tuesday, February 6, 2001 -14:33:00 - - +14:33:00 date_format conversion specifiers @@ -5410,8 +5083,7 @@ is the first week that has at least 4 days in the current year, and with Monday PROGRAMMERS NOTE: date_format is essentially a wrapper to PHP's strftime() function. You may have more or less conversion specifiers available depending on your system's strftime() function where PHP was compiled. Check your -system's manpage for a full list of valid specifiers. - +system's manpage for a full list of valid specifiers. @@ -5453,15 +5125,12 @@ system's manpage for a full list of valid specifiers. default - {* this will display "no title" (without the quotes) if $articleTitle is empty *} {$articleTitle|default:"no title"} OUTPUT: -no title - - +no title @@ -5507,7 +5176,6 @@ no title escape - {$articleTitle} {$articleTitle|escape} {$articleTitle|escape:"html"} @@ -5524,9 +5192,7 @@ OUTPUT: 'Stiff+Opposition+Expected+to+Casketless+Funeral+Plan' \'Stiff Opposition Expected to Casketless Funeral Plan\' <a -href="mailto:%62%6f%62%40%6d%65%2e%6e%65%74">&#x62;&#x6f;&#x62;&#x40;&#x6d;&#x65;&#x2e;&#x6e;&#x65;&#x74;</a> - - +href="mailto:%62%6f%62%40%6d%65%2e%6e%65%74">&#x62;&#x6f;&#x62;&#x40;&#x6d;&#x65;&#x2e;&#x6e;&#x65;&#x74;</a> @@ -5599,9 +5265,7 @@ Statistics show that teen pregnancy drops off significantly after 25. NJ judge to rule on nude beach. Sun or rain expected today, dark tonight. - Statistics show that teen pregnancy drops off significantly after 25. - - + Statistics show that teen pregnancy drops off significantly after 25. @@ -5612,16 +5276,13 @@ Statistics show that teen pregnancy drops off significantly after 25. lower - {$articleTitle} {$articleTitle|lower} OUTPUT: Two Convicts Evade Noose, Jury Hung. -two convicts evade noose, jury hung. - - +two convicts evade noose, jury hung. @@ -5670,7 +5331,6 @@ two convicts evade noose, jury hung. regex_replace - {* replace each carriage return, tab & new line with a space *} {$articleTitle} @@ -5680,10 +5340,7 @@ OUTPUT: Infertility unlikely to be passed on, experts say -Infertility unlikely to be passed on, experts say - - - +Infertility unlikely to be passed on, experts say @@ -5728,7 +5385,6 @@ Infertility unlikely to be passed on, experts say replace - {$articleTitle} {$articleTitle|replace:"Garden":"Vineyard"} {$articleTitle|replace:" ":" "} @@ -5737,9 +5393,7 @@ OUTPUT: Child's Stool Great for Use in Garden. Child's Stool Great for Use in Vineyard. -Child's Stool Great for Use in Garden. - - +Child's Stool Great for Use in Garden. @@ -5779,7 +5433,6 @@ Child's Stool Great for Use in Garden. spacify - {$articleTitle} {$articleTitle|spacify} {$articleTitle|spacify:"^^"} @@ -5788,9 +5441,7 @@ OUTPUT: Something Went Wrong in Jet Crash, Experts Say. S o m e t h i n g W e n t W r o n g i n J e t C r a s h , E x p e r t s S a y . -S^^o^^m^^e^^t^^h^^i^^n^^g^^ ^^W^^e^^n^^t^^ ^^W^^r^^o^^n^^g^^ ^^i^^n^^ ^^J^^e^^t^^ ^^C^^r^^a^^s^^h^^,^^ ^^E^^x^^p^^e^^r^^t^^s^^ ^^S^^a^^y^^. - - +S^^o^^m^^e^^t^^h^^i^^n^^g^^ ^^W^^e^^n^^t^^ ^^W^^r^^o^^n^^g^^ ^^i^^n^^ ^^J^^e^^t^^ ^^C^^r^^a^^s^^h^^,^^ ^^E^^x^^p^^e^^r^^t^^s^^ ^^S^^a^^y^^. @@ -5829,7 +5480,6 @@ S^^o^^m^^e^^t^^h^^i^^n^^g^^ ^^W^^e^^n^^t^^ ^^W^^r^^o^^n^^g^^ ^^i^^n^^ ^^J^^e^^t^ string_format - {$number} {$number|string_format:"%.2f"} {$number|string_format:"%d"} @@ -5838,9 +5488,7 @@ OUTPUT: 23.5787446 23.58 -24 - - +24 @@ -5851,16 +5499,13 @@ OUTPUT: strip_tags - {$articleTitle} {$articleTitle|strip_tags} OUTPUT: Blind Woman Gets <font face="helvetica">New Kidney</font> from Dad she Hasn't Seen in <b>years</b>. -Blind Woman Gets New Kidney from Dad she Hasn't Seen in years. - - +Blind Woman Gets New Kidney from Dad she Hasn't Seen in years. @@ -5936,9 +5581,7 @@ Two Sisters Reunite after... Two Sisters Reunite after Two Sisters Reunite after--- Two Sisters Reunite after Eigh -Two Sisters Reunite after E... - - +Two Sisters Reunite after E... @@ -5949,16 +5592,13 @@ Two Sisters Reunite after E... upper - {$articleTitle} {$articleTitle|upper} OUTPUT: If Strike isn't Settled Quickly it may Last a While. -IF STRIKE ISN'T SETTLED QUICKLY IT MAY LAST A WHILE. - - +IF STRIKE ISN'T SETTLED QUICKLY IT MAY LAST A WHILE. @@ -6041,9 +5681,7 @@ Blind woman gets new kidney<br> from dad she hasn't seen in years. Blind woman gets new kidney fr -om dad she hasn't seen in years. - - +om dad she hasn't seen in years. @@ -6057,7 +5695,6 @@ om dad she hasn't seen in years. combining modifiers - {$articleTitle} {$articleTitle|upper|spacify} {$articleTitle|lower|spacify|truncate} @@ -6071,27 +5708,25 @@ 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. . . - - +s m o k e r s a r e p. . . - + Plugins Version 2.0 introduced the plugin architecture that is used for almost all the customizable functionality of Smarty. This includes: - - functions - modifiers - compiler functions - prefilters - postfilters - resources - inserts - + + functions + modifiers + compiler functions + prefilters + postfilters + resources + inserts + With the exception of resources, backwards compatibility with the old way of registering handler functions via register_* API is preserved. If you did not use the API but instead modified the class variables @@ -6112,75 +5747,78 @@ s m o k e r s a r e p. . . Pre/post-filters are a bit of a special case. If a pre/post-filter plugin is found in the plugins directory, it will always be loaded because these filters are never explicitly mentioned in the template - language. + language. This also means that the order in which multiple prefilters + or postfilters is executed depends on the way they are named + (basically, alphabetical order). There is only one plugins directory (for performance reasons). To install a plugin, simply place it in the directory and Smarty will use it automatically. - - Naming Conventions - - Plugin files and functions must follow a very specific naming - convention in order to be located by Smarty. - - - The plugin files must be named as follows: -
- - type.name.php - -
-
- - type is one of these plugin types: - - function - modifier - compiler - prefilter - postfilter - resource - insert - - - - name should be a valid identifier (letters, - numbers, and underscores only). - - - Some examples: function.html_select_date.php, - resource.db.php, - modifier.spacify.php. - - - The plugin functions inside the plugin files must be named as follows: -
- - smarty_type_name() - -
-
- - The meanings of type and name are - the same as before. - - - Smarty will output appropriate error messages if the plugin file it - needs is not found, or if the file or the plugin function are named - improperly. - - - - Note that pre/postfilters still have to be correctly - named even though the names are not really used for anything. - - -
- + + Naming Conventions + + Plugin files and functions must follow a very specific naming + convention in order to be located by Smarty. + + + The plugin files must be named as follows: +
+ + type.name.php + +
+
+ + type is one of these plugin types: + + function + modifier + compiler + prefilter + postfilter + resource + insert + + + + name should be a valid identifier (letters, + numbers, and underscores only). + + + Some examples: function.html_select_date.php, + resource.db.php, + modifier.spacify.php. + + + The plugin functions inside the plugin files must be named as follows: +
+ + smarty_type_name() + +
+
+ + The meanings of type and name are + the same as before. + + + Smarty will output appropriate error messages if the plugin file it + needs is not found, or if the file or the plugin function are named + improperly. + + + + Note that pre/postfilters still have to be correctly + named even though the names are not really used for anything. + + +
+ + Writing Plugins Plugins can be either loaded by Smarty automatically from the @@ -6197,46 +5835,46 @@ s m o k e r s a r e p. . . (as is the case with some plugins bundled with Smarty), then the proper way to load the needed plugin is this: -require_once SMARTY_DIR . 'plugins/function.html_options.php'; - +require_once SMARTY_DIR . 'plugins/function.html_options.php'; As a general rule, Smarty object is always passed to the plugins as the last parameter (except for modifiers). + - Functions - - - void plugin_name - array $params - object &$smarty - - - - All attributes passed to template functions from the template are - contained in the $params as an associative - array. Either access those values directly, e.g. $params['start'] or - use extract($params) to import them into the symbol table. - - - The output of the function will be substituted in place of the - function tag in the template (fetch function, for - example). Alternatively, the function can simply perform some other - task without any output (assign function). - - - If the function needs to assign some variables to the template or use - some other Smarty-provided functionality, it can use the supplied - $smarty object to do so. - - - See also: - register_function(), - unregister_function(). - - - - function plugin with output - + Template Functions + + + void func_name + array $params + object &$smarty + + + + All attributes passed to template functions from the template are + contained in the $params as an associative + array. Either access those values directly, e.g. $params['start'] or + use extract($params) to import them into the symbol table. + + + The output of the function will be substituted in place of the + function tag in the template (fetch function, for + example). Alternatively, the function can simply perform some other + task without any output (assign function). + + + If the function needs to assign some variables to the template or use + some other Smarty-provided functionality, it can use the supplied + $smarty object to do so. + + + See also: + register_function(), + unregister_function(). + + + + function plugin with output + <?php /* * Smarty plugin @@ -6259,15 +5897,15 @@ function smarty_function_eightball($params, &$smarty) $result = array_rand($answers); echo $answers[$result]; } -?> - +?> + + which can be used in the template as: - Question: Will we ever have time travel? - Answer: {eightball}. - +Question: Will we ever have time travel? +Answer: {eightball}. @@ -6301,41 +5939,40 @@ function smarty_function_assign($params, &$smarty) $smarty->assign($var, $value); } -?> - +?> - +
- Modifiers - - - mixed plugin_name - mixed $value - [mixed $param1, ...] - - + Modifiers + + + mixed modifier_name + mixed $value + [mixed $param1, ...] + + + + The first parameter to the modifier plugin is the value on which + the modifier is supposed to operate. The rest of the parameters can be + optional, depending on what kind of operation is supposed to be + performed. + + + The modifier has to return the result of its processing. + + + See also + register_modifier(), + unregister_modifier(). + + + simple modifier plugin - The first parameter to the modifier plugin is the value on which - the modifier is supposed to operate. The rest of the parameters can be - optional, depending on what kind of operation is supposed to be - performed. + This plugin basically aliases one of the built-in PHP functions. It + does not have any additional parameters. - - The modifier has to return the result of its processing. - - - See also - register_modifier(), - unregister_modifier(). - - - simple modifier plugin - - This plugin basically aliases one of the built-in PHP functions. It - does not have any additional parameters. - - + <?php /* * Smarty plugin @@ -6350,12 +5987,11 @@ function smarty_modifier_capitalize($string) { return ucwords($string); } -?> - - - - more complex modifier plugin - +?> + + + more complex modifier plugin + <?php /* * Smarty plugin @@ -6385,61 +6021,229 @@ function smarty_modifier_truncate($string, $length = 80, $etc = '...', } else return $string; } -?> - - - +?> + + - compiler functions + Compiler Functions + + Compiler functions are called only during compilation of the template. + They are useful for injecting PHP code or time-sensitive static + content into the template. If there is both a compiler function and a + custom function registered under the same name, the compiler function + has precedence. + + + + mixed compiler_name + string $tag_arg + object &$smarty + + + + The compiler function is passed two parameters: the tag argument + string - basically, everything from the function name until the ending + delimiter, and the Smarty object. It's supposed to return the PHP code + to be injected into the compiled template. + + + See also + register_compiler_function(), + unregister_compiler_function(). + + + simple compiler function + +<?php +/* + * Smarty plugin + * ------------------------------------------------------------- + * File: compiler.tplheader.php + * Type: compiler + * Name: tplheader + * Purpose: Output header containing the source file name and + * the time it was compiled. + * ------------------------------------------------------------- + */ +function smarty_compiler_tplheader($tag_arg, &$smarty) +{ + return "\necho '" . $smarty->_current_file . " compiled at " . date('Y-m-d H:M'). "';"; +} +?> - - - compiler function plugin + This function can be called from the template as: - - - - - prefilters - - - - prefilter plugin + {* this function gets executed at compile time only *} + {tplheader} + The resulting PHP code in the compiled template would be something like this: - - - - - postfilters - +<php +echo 'index.tpl compiled at 2002-02-20 20:02'; +?> - - postfilter plugin - + + - - - - template resources - - - - resource plugin - + Prefilters/Postfilters + + Prefilter and postfilter plugins are very similar in concept; where + they differ is in the execution -- more precisely the time of their + execution. + + + + string prefilter_name + string $source + object &$smarty + + + + Prefilters are used to process the source of the template immediately + before compilation. The first parameter to the prefilter function is + the template source, possibly modified by some other prefilters. The + plugin is supposed to return the modified source. Note that this + source is not saved anywhere, it is only used for compilation. + + + + string postfilter_name + string $compiled_source + object &$smarty + + + + Postfilters are used to process the compiled output of the template + (the PHP code) immediately after the compilation is done but before the + compiled template is saved to the filesystem. The first parameter to + the postfilter function is the compiled template code, possibly + modified by other postfilters. The plugin is supposed to return the + modified version of this code. + + + prefilter plugin + +<?php +/* + * Smarty plugin + * ------------------------------------------------------------- + * File: prefilter.pre01.php + * Type: prefilter + * Name: pre01 + * Purpose: Convert html tags to be lowercase. + * ------------------------------------------------------------- + */ + function smarty_prefilter_pre01($source, &$smarty) + { + return preg_replace('!<(\w+)[^>]+>!e', 'strtolower("$1")', $source); + } +?> + + + + postfilter plugin + +<?php +/* + * Smarty plugin + * ------------------------------------------------------------- + * File: postfilter.post01.php + * Type: postfilter + * Name: post01 + * Purpose: Output code that lists all current template vars. + * ------------------------------------------------------------- + */ + function smarty_postfilter_post01($compiled, &$smarty) + { + $compiled = "<pre>\n<?php print_r(\$this->get_template_vars()); ?>\n</pre>" . $compiled; + return $compiled; + } +?> + + - - - - inserts - - - - insert plugin - + Resources + + Resource plugins are meant as a generic way of providing template + sources or PHP script components to Smarty. Some examples of resources: + databases, LDAP, shared memory, sockets, and so on. + - - - + + There are a total of 4 functions that need to be registered for each + type of resource. Every function will receive the requested resource as + the first parameter and the Smarty object as the last parameter. The + rest of parameters depend on the function. + + + + + bool rsrc_source + string $name + string &$source + object &$smarty + + + bool rsrc_timestamp + string $name + int &$timestamp + object &$smarty + + + bool rsrc_is_secure + string $name + object &$smarty + + + bool rsrc_is_trusted + string $name + object &$smarty + + + + + The first function is supposed to retrieve the resource. Its + second parameter is a variable passed by reference where the + result should be stored. The function is supposed to return + true if it was able to successfully retrieve + the resource and false otherwise. + + + + The second function is supposed to retrieve the last + modification time of the requested resource (as a UNIX + timestamp). The second parameter is a by reference variable + where the timestamp should be stored. The function is supposed + to return true if the timestamp could be + succesfully determined, and false otherwise. + + + + The third function is supposed to return true + or false, depending on whether the requested + resource is secure or not. This function is not used for + non-template resources but should still be defined. + + + + The fourth function is supposed to return + true or false, depending + on whether the requested resource is trusted or not. This + function is not used for template resources but should still be + defined. + + + resource plugin + + + + + inserts + + + + insert plugin + +
@@ -6466,14 +6270,12 @@ function smarty_modifier_truncate($string, $length = 80, $etc = '...', Debug data is not cached and debug.tpl info is not included in the output of the debug console. + - NOTE: This feature was added to Smarty 1.4.3. - - - NOTE: The load times of each template file and config file have been added to - the debug output in Smarty 1.4.6. This output is in seconds, or fractions - thereof. + The load times of each template and config file are in seconds, or + fractions thereof. + Troubleshooting @@ -6489,14 +6291,11 @@ function smarty_modifier_truncate($string, $length = 80, $etc = '...', 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 - - + in /path/to/smarty/Smarty.class.php on line 1041 @@ -6514,10 +6313,7 @@ Fatal error: Smarty: [in index.tpl line 28]: syntax error: missing section name PHP parsing errors - -Parse error: parse error in /path/to/smarty/templates_c/index.tpl.php on line 75 - - +Parse error: parse error in /path/to/smarty/templates_c/index.tpl.php on line 75 @@ -6549,7 +6345,6 @@ Parse error: parse error in /path/to/smarty/templates_c/index.tpl.php on line 75 Printing &nbsp; when a variable is empty - {* the long way *} {if $title eq ""} @@ -6561,9 +6356,7 @@ Parse error: parse error in /path/to/smarty/templates_c/index.tpl.php on line 75 {* the short way *} -{$title|default:"&nbsp;"} - - +{$title|default:"&nbsp;"} @@ -6577,14 +6370,11 @@ Parse error: parse error in /path/to/smarty/templates_c/index.tpl.php on line 75 Assigning a template variable its default value - {* do this somewhere at the top of your template *} {assign var="title" value=$title|default:"no title"} {* if $title was empty, it now contains the value "no title" when you print it *} -{$title} - - +{$title} @@ -6600,7 +6390,6 @@ Parse error: parse error in /path/to/smarty/templates_c/index.tpl.php on line 75 Passing the title variable to the header template - mainpage.tpl ------------ @@ -6630,9 +6419,7 @@ header.tpl footer.tpl ---------- </BODY> -</HTML> - - +</HTML> When the main page is drawn, the title of "Main Page" is passed to the @@ -6659,7 +6446,6 @@ footer.tpl using date_format - {$startDate|date_format} OUTPUT: @@ -6676,9 +6462,7 @@ OUTPUT: {if $date1 < $date2} ... -{/if} - - +{/if} When using {html_select_date} in a template, The programmer will most @@ -6688,7 +6472,6 @@ OUTPUT: converting form date elements back to a timestamp - // this assumes your form elements are named // startDate_Day, startDate_Month, startDate_Year @@ -6704,9 +6487,7 @@ function makeTimeStamp($year="",$month="",$day="") $day = strftime("%d"); return mktime(0,0,0,$month,$day,$year); -} - - +} @@ -6722,7 +6503,6 @@ function makeTimeStamp($year="",$month="",$day="") 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() { @@ -6759,9 +6539,7 @@ Press OK to continue... Pretty easy isn't it? </p> </card> -</wml> - - +</wml> @@ -6782,17 +6560,16 @@ Pretty easy isn't it? template, and not worry about fetching the data up front? - As of Smarty 1.4.0, you can embed php into your templates with the - {php}{/php} tags. With this, you can setup self contained templates - with their own data structures for assigning their own variables. With - the logic embedded like this, you can keep the template & logic - together. This way no matter where the template source is coming from, - it is always together as one component. + You can embed PHP into your templates with the {php}{/php} tags. + With this, you can setup self contained templates with their own + data structures for assigning their own variables. With the logic + embedded like this, you can keep the template & logic together. This + way no matter where the template source is coming from, it is always + together as one component. componentized template - {* Smarty *} {php} @@ -6812,9 +6589,7 @@ Pretty easy isn't it? {/php} -Stock Name: {$ticker_name} Stock Price: {$ticker_price} - - +Stock Name: {$ticker_name} Stock Price: {$ticker_price} As of Smarty 1.5.0, there is even a cleaner way. You can include php in @@ -6826,7 +6601,6 @@ Stock Name: {$ticker_name} Stock Price: {$ticker_price} componentized template with include_php - load_ticker.php --------------- @@ -6853,9 +6627,7 @@ index.tpl {include_php file="load_ticker.php"} -Stock Name: {$ticker_name} Stock Price: {$ticker_price} - - +Stock Name: {$ticker_name} Stock Price: {$ticker_price}