diff --git a/NEWS b/NEWS index 91d7e9cc..64ee18d0 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,9 @@ +Version 1.4.0 +------------- + - added RELEASE_NOTES file to distribution (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 {insert ...} is not used (Monte) - added $compiler_class variable to allow specifying a different compiler diff --git a/README b/README index aad3ffe1..99e54c83 100644 --- a/README +++ b/README @@ -2,7 +2,7 @@ NAME: Smarty - the PHP compiling template engine -VERSION: 1.3.2 +VERSION: 1.4.0 AUTHORS: @@ -14,7 +14,8 @@ MAILING LIST: We have a mailing list for you to share your ideas or ask questions. send a blank e-mail message to subscribe-smarty@lists.ispi.net - + You can also browse the mailing list archives at + http://marc.theaimsgroup.com/?l=smarty&r=1&w=2 SYNOPSIS: diff --git a/RELEASE_NOTES b/RELEASE_NOTES index 3f799207..8c69e71f 100644 --- a/RELEASE_NOTES +++ b/RELEASE_NOTES @@ -3,15 +3,15 @@ IMPORTANT NOTICE: Smarty now has a new syntax for accessing elements within section loops. The new syntax is easier to use for complex data structures. -Consequently, this breaks the old syntax. To fix your old templates, we have -provided a script that will fix the syntax for you. Located in +Consequently, this breaks the old syntax. To fix your current templates, we +have provided a script that will adjust the syntax for you. Located in misc/fix_vars.php, run this script from the the command line, giving each template as an argument. Be sure to use absolute pathnames, or pathnames relative to the executing script. Probably the easiest way to do this is to copy the fix_vars.php script into your template directory and run 'php -q -fix_vars.php *.tpl' For each template, this will create a temporary file with -the fixes, then move the temp file over top the original. Backup your scripts -first to be safe! +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. Smarty 1.4.0 also has a new compilation process. Instead of compiling all the templates up front, it now compiles them at runtime. This has several @@ -21,13 +21,11 @@ directories or even database calls. This also speeds the performance of Smarty when $compile_check is enabled, since it is only checking the template that is being executed instead of everything found in the template directory. The $tpl_file_ext is no longer needed, but kept for backward compatability. -Templates can now be named anything you like with any extension. Smarty makes -use of the PEAR database abstraction class for obtaining templates from -databases. +Templates can now be named anything you like with any extension. -We also added a workaround for LOCK_EX on Windows systems, and changed a couple -of file permissions for better security on public servers. Thanks goes to -Fernando Nunes for his code contributions. +A workaround for LOCK_EX on Windows systems was added, and changed a couple of +file permissions for better security on public servers. Thanks goes to Fernando +Nunes for his code contributions. date_format, html_select_date and html_select_time used to require a unix timestamp as the format of the date passed into the template. Smarty is now a @@ -35,14 +33,19 @@ bit smarter at this. It will take a unix timestamp, a mysql timestamp, or any date string that is parsable by strtotime, such as 10/01/2001 or 2001-10-01, etc. Just give some formats a try and see what works. +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. + 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. Since templates can come from any -source, the compiled versions have encoded names. Each file has a comment at -the top that states which resource was used as the source file. In unix, "head --2 *" shows the first two lines of each file. If you have $compile_check set to +$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. Once it is compiled though, it will not check to see -if recompilation is necessary. +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.addons.php b/Smarty.addons.php index cb82c87f..1e70c8a6 100644 --- a/Smarty.addons.php +++ b/Smarty.addons.php @@ -4,7 +4,7 @@ * File: Smarty.addons.php * Author: Monte Ohrt * Andrei Zmievski - * Version: 1.3.2 + * Version: 1.4.0 * Copyright: 2001 ispi of Lincoln, Inc. * * This library is free software; you can redistribute it and/or diff --git a/Smarty.class.php b/Smarty.class.php index a0e59934..1f7de4db 100644 --- a/Smarty.class.php +++ b/Smarty.class.php @@ -5,7 +5,7 @@ * Author: Monte Ohrt * Andrei Zmievski * - * Version: 1.3.2 + * Version: 1.4.0 * Copyright: 2001 ispi of Lincoln, Inc. * * This library is free software; you can redistribute it and/or @@ -123,9 +123,7 @@ class Smarty 'count_paragraphs' => 'smarty_mod_count_paragraphs' ); - var $template_resource_handlers = array(); // where resource handlers are mapped - - var $version = '1.3.2'; // Smarty version number + var $version = '1.4.0'; // Smarty version number var $show_info_header = true; // display info header at top of page output var $compiler_class = 'Smarty_Compiler'; // the compiler class used by @@ -140,6 +138,7 @@ class Smarty var $_error_msg = false; // error messages. true/false var $_tpl_vars = array(); var $_smarty_md5 = 'f8d698aea36fcbead2b9d5359ffca76f'; // md5 checksum of the string 'Smarty' + var $_resource_handlers = array(); // what functions resource handlers are mapped to /*======================================================================*\ Function: Smarty @@ -248,6 +247,24 @@ class Smarty unset($this->custom_mods[$modifier]); } +/*======================================================================*\ + Function: register_resource + Purpose: Registers a resource to fetch a template +\*======================================================================*/ + function register_resource($name, $function_name) + { + $this->_resource_handlers[$name] = $function_name; + } + +/*======================================================================*\ + Function: unregister_resource + Purpose: Unregisters a resource +\*======================================================================*/ + function unregister_resource($name) + { + unset($this->_resource_handlers[$name]); + } + /*======================================================================*\ Function: clear_cache() @@ -525,10 +542,21 @@ class Smarty return false; } break; - default: - $this->_trigger_error_msg("unknown resource type: \"$resource_type.\""); - return false; + if(isset($this->_resource_handlers[$resource_type])) { + $funcname = $this->_resource_handlers[$resource_type]; + if(function_exists($funcname)) { + // call the function to fetch the template + $funcname($resource_name,$template_source,$template_timestamp); + return true; + } else { + $this->_trigger_error_msg("function: \"$funcname\" does not exist for resource type: \"$resource_type\"."); + return false; + } + } else { + $this->_trigger_error_msg("unknown resource type: \"$resource_type\". Register this resource first."); + return false; + } break; } diff --git a/Smarty_Compiler.class.php b/Smarty_Compiler.class.php index cec7502d..8fad285f 100644 --- a/Smarty_Compiler.class.php +++ b/Smarty_Compiler.class.php @@ -6,7 +6,7 @@ * Author: Monte Ohrt * Andrei Zmievski * - * Version: 1.3.2 + * Version: 1.4.0 * Copyright: 2001 ispi of Lincoln, Inc. * * This library is free software; you can redistribute it and/or diff --git a/docs.sgml b/docs.sgml index 1eca4d8c..d3808b51 100644 --- a/docs.sgml +++ b/docs.sgml @@ -734,6 +734,51 @@ $smarty->register_modifier("sslash","stripslashes"); $smarty->unregister_modifier("strip_tags"); + + + + + register_resource + + + void register_resource + string resource_name + string function_name + + + + 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 + + +$smarty->register_resource("db","get_db_template"); + + + + + + unregister_resource + + + void unregister_resource + string resource_name + + + + Use this to dynamically unregister a resource. + + +unregister_resource + + +$smarty->unregister_resource("db"); + @@ -921,9 +966,10 @@ echo $output; Templates from $template_dir - Templates from the $template_dir do not require a template resource. - Just supply the path to the template you want to use relative to the - $template_dir root directory. + Templates from the $template_dir do not require a template + resource, although you can use the file: resource for consistancy. + Just supply the path to the template you want to use relative to + the $template_dir root directory. @@ -959,6 +1005,47 @@ $smarty->display("file:/path/to/my/templates/menu.tpl"); {* from within Smarty template *} {include file="file:/usr/local/share/templates/navigation.tpl"} + + + + + Templates from a function call + + You can get templates from a custom function call, such as + 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) {} $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. + + + +using templates from a function call + + +// from PHP script + +// put this function somewhere in your application +function get_db_template ($tpl_name, &$tpl_source, &$tpl_timestamp) { + // do database calls (or whatever) here to fetch your template, populating + // $tpl_source and $tpl_timestamp. + + $tpl_source = "This is a simulation of a template fetched from a db."; + $tpl_timestamp = mktime(); + + return true; +} + +$smarty->register_resource("db" => "get_db_template"); + +// using resource from php script +$smarty->display("db:index.tpl"); + +{* using resource from within Smarty template *} +{include file="db:/extras/navigation.tpl"} + diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index a0e59934..1f7de4db 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -5,7 +5,7 @@ * Author: Monte Ohrt * Andrei Zmievski * - * Version: 1.3.2 + * Version: 1.4.0 * Copyright: 2001 ispi of Lincoln, Inc. * * This library is free software; you can redistribute it and/or @@ -123,9 +123,7 @@ class Smarty 'count_paragraphs' => 'smarty_mod_count_paragraphs' ); - var $template_resource_handlers = array(); // where resource handlers are mapped - - var $version = '1.3.2'; // Smarty version number + var $version = '1.4.0'; // Smarty version number var $show_info_header = true; // display info header at top of page output var $compiler_class = 'Smarty_Compiler'; // the compiler class used by @@ -140,6 +138,7 @@ class Smarty var $_error_msg = false; // error messages. true/false var $_tpl_vars = array(); var $_smarty_md5 = 'f8d698aea36fcbead2b9d5359ffca76f'; // md5 checksum of the string 'Smarty' + var $_resource_handlers = array(); // what functions resource handlers are mapped to /*======================================================================*\ Function: Smarty @@ -248,6 +247,24 @@ class Smarty unset($this->custom_mods[$modifier]); } +/*======================================================================*\ + Function: register_resource + Purpose: Registers a resource to fetch a template +\*======================================================================*/ + function register_resource($name, $function_name) + { + $this->_resource_handlers[$name] = $function_name; + } + +/*======================================================================*\ + Function: unregister_resource + Purpose: Unregisters a resource +\*======================================================================*/ + function unregister_resource($name) + { + unset($this->_resource_handlers[$name]); + } + /*======================================================================*\ Function: clear_cache() @@ -525,10 +542,21 @@ class Smarty return false; } break; - default: - $this->_trigger_error_msg("unknown resource type: \"$resource_type.\""); - return false; + if(isset($this->_resource_handlers[$resource_type])) { + $funcname = $this->_resource_handlers[$resource_type]; + if(function_exists($funcname)) { + // call the function to fetch the template + $funcname($resource_name,$template_source,$template_timestamp); + return true; + } else { + $this->_trigger_error_msg("function: \"$funcname\" does not exist for resource type: \"$resource_type\"."); + return false; + } + } else { + $this->_trigger_error_msg("unknown resource type: \"$resource_type\". Register this resource first."); + return false; + } break; } diff --git a/libs/Smarty_Compiler.class.php b/libs/Smarty_Compiler.class.php index cec7502d..8fad285f 100644 --- a/libs/Smarty_Compiler.class.php +++ b/libs/Smarty_Compiler.class.php @@ -6,7 +6,7 @@ * Author: Monte Ohrt * Andrei Zmievski * - * Version: 1.3.2 + * Version: 1.4.0 * Copyright: 2001 ispi of Lincoln, Inc. * * This library is free software; you can redistribute it and/or