diff --git a/docs.sgml b/docs.sgml
index 5dc95e1d..aa1b8884 100644
--- a/docs.sgml
+++ b/docs.sgml
@@ -257,14 +257,6 @@ chmod 700 cache
Next, try running the index.php script from your web browser.
-
-
-
- Setting up Smarty
-
- There are several variables that are at the top of the Smarty.class.php
- file. The default settings work for all of the examples and tutorials.
- Constants
@@ -287,12 +279,13 @@ require_once(SMARTY_DIR."Smarty.class.php");
+
-
- Variables
+
+ Class Variables
-
+ $template_dir
This is the name of the default template directory. If you do
@@ -305,8 +298,8 @@ require_once(SMARTY_DIR."Smarty.class.php");
TECHNICAL NOTE: It is not mandatory to put this directory under
the web server document root.
-
-
+
+ $compile_dir
This is the name of the directory where compiled templates are
@@ -323,8 +316,8 @@ require_once(SMARTY_DIR."Smarty.class.php");
TECHNICAL NOTE: It is not mandatory to put this directory under
the web server document root.
-
-
+
+ $config_dir
This is the directory used to store config files used in the
@@ -336,8 +329,8 @@ require_once(SMARTY_DIR."Smarty.class.php");
TECHNICAL NOTE: It is not mandatory to put this directory under
the web server document root.
-
-
+
+ $plugins_dir
This is the directory where Smarty will look for the plugins that
@@ -345,8 +338,8 @@ require_once(SMARTY_DIR."Smarty.class.php");
Smarty itself is installed. Default is "plugins". There can be
only one plugins directory.
-
-
+
+ $debugging
This enables the
NOTE: This was added to Smarty 1.4.3.
-
-
+
+ $debug_tpl
This is the name of the template file used for the debugging
@@ -368,8 +361,8 @@ require_once(SMARTY_DIR."Smarty.class.php");
NOTE: This was added to Smarty 1.4.3.
-
-
+
+ $debugging_ctrl
This allows alternate ways to enable debugging. NONE means no
@@ -381,8 +374,8 @@ require_once(SMARTY_DIR."Smarty.class.php");
NOTE: This was added to Smarty 1.4.4.
-
-
+
+ $global_assign
This is a list of variables that are always implicitly assigned
@@ -401,16 +394,16 @@ require_once(SMARTY_DIR."Smarty.class.php");
section on the
$smarty variable.
-
-
+
+ $undefined
This sets the value of $undefined for Smarty, default is null.
Currently this is only used to set undefined variables in
$global_assign to a default value.
-
-
+
+ $compile_check
Upon each invocation of the PHP application, Smarty tests to
@@ -430,8 +423,8 @@ require_once(SMARTY_DIR."Smarty.class.php");
linkend="variable.force.compile">$force_compile or clear_compiled_tpl.
-
-
+
+ $force_compile
This forces Smarty to (re)compile templates on every
@@ -440,8 +433,8 @@ require_once(SMARTY_DIR."Smarty.class.php");
It should never be used in a production environment. If caching
is enabled, the cache file(s) will be regenerated every time.
-
-
+
+ $caching
This tells Smarty whether or not to cache the output of the
@@ -459,8 +452,8 @@ require_once(SMARTY_DIR."Smarty.class.php");
to 1.4.6). If $force_compile is enabled, the cached content
will always be regenerated. (added to Smarty 1.4.7)
-
-
+
+ $cache_dir
This is the name of the directory where template caches are
@@ -478,8 +471,8 @@ require_once(SMARTY_DIR."Smarty.class.php");
TECHNICAL NOTE: It is not mandatory to put this directory under
the web server document root.
-
-
+
+ $cache_lifetime
This is the length of time in seconds that a template cache is
@@ -489,16 +482,16 @@ require_once(SMARTY_DIR."Smarty.class.php");
linkend="api.clear.all.cache">clear_all_cache. (added to
Smarty 1.3.0)
-
-
+
+ $cache_handler_func
You can supply a custom function to handle cache files instead
of using the built-in method using the $cache_dir. See the
custom cache handler function section for details.
-
-
+
+ $cache_modified_check
If set to true, Smarty will respect the If-Modified-Since
@@ -507,15 +500,15 @@ require_once(SMARTY_DIR."Smarty.class.php");
header will be sent instead of the content. This works only on
cached content without {insert} tags.
-
-
+
+ $default_template_handler_func
This function is called when a template cannot be obtained from
its resource. (added to Smarty 1.5.2)
-
-
+
+ $php_handling
This tells Smarty how to handle PHP code embedded in the
@@ -538,8 +531,8 @@ require_once(SMARTY_DIR."Smarty.class.php");
Use custom functions or
modifiers instead.
-
-
+
+ $security
$security true/false, default is false. Security is good for
@@ -565,15 +558,15 @@ require_once(SMARTY_DIR."Smarty.class.php");
NOTE: Security features were added to Smarty 1.4.3.
-
-
+
+ $secure_dir
This is an array of all local directories that are considered
secure. {include} and {fetch} use this when security is enabled.
-
-
+
+ $security_settings
These are used to override or specify the security settings when
@@ -592,79 +585,72 @@ require_once(SMARTY_DIR."Smarty.class.php");
MODIFIER_FUNCS - This is an array of the names of permitted
PHP functions used as variable modifiers.
-
-
+
+ $trusted_dir
- $trusted_dir is only for use when $security is enabled.
- This is an array of all directories that are considered
- trusted. When a directory is considered trusted, $security is
- temporarily disabled while any templates from here are
- included. New in Smarty 1.5.0.
+ $trusted_dir is only for use when $security is enabled. This is an array
+ of all directories that are considered trusted. Trusted directories are
+ where you keep php scripts that are executed directly from the templates
+ with {include_php}.
-
-
+
+ $left_delimiter
This is the left delimiter used by the template language.
Default is "{".
-
-
+
+ $right_delimiter
This is the right delimiter used by the template language.
Default is "}".
-
-
+
+ $show_info_header
Shows an HTML comment at the beginning of the templates output,
displaying smarty version and date generated. Default is false.
-
-
+
+ $show_info_include
Shows an HTML comment before and after each included template.
Default is false.
-
-
+
+ $compiler_class
Specifies the name of the compiler class that Smarty will use
to compile the templates. The default is 'Smarty_Compiler'. For
advanced users only.
-
-
+
+ $request_vars_order
The order in which request variables are registered, similar to
variables_order in php.ini
-
-
+
+ $compile_id
Persistant compile identifier. As an alternative to passing the
same compile_id to each and every function call, you can set this
compile_id and it will be used implicitly thereafter.
-
-
+
- Smarty API
-
- These functions are used in the PHP portion of your application.
-
- API Functions
-
+ assign
@@ -692,16 +678,8 @@ $smarty->assign("Address",$address);
// passing an associative array
$smarty->assign(array("city" => "Lincoln","state" => "Nebraska"));
-
-
- assign_debug_info
-
- This is used to assign debugging data to the template. This is
- used by the debugging
- console, and should probably never be used directly.
-
-
-
+
+ append
@@ -729,8 +707,8 @@ $smarty->append("Address",$address);
// passing an associative array
$smarty->append(array("city" => "Lincoln","state" => "Nebraska"));
-
-
+
+ clear_assign
@@ -752,8 +730,8 @@ $smarty->clear_assign("Name");
// clear multiple variables
$smarty->clear_assign(array("Name","Address","Zip"));
-
-
+
+ clear_all_assign
@@ -770,8 +748,8 @@ $smarty->clear_assign(array("Name","Address","Zip"));
// clear all assigned variables
$smarty->clear_all_assign();
-
-
+
+ clear_cache
@@ -796,8 +774,8 @@ $smarty->clear_cache("index.tpl");
// clear the cache for a particular cache id in an multiple-cache template
$smarty->clear_cache("index.tpl","CACHEID");
-
-
+
+ clear_all_cache
@@ -815,8 +793,8 @@ $smarty->clear_cache("index.tpl","CACHEID");
// clear the entire cache
$smarty->clear_all_cache();
-
-
+
+ clear_compiled_tpl
@@ -838,8 +816,8 @@ $smarty->clear_compiled_tpl("index.tpl");
// clear entire compile directory
$smarty->clear_compiled_tpl();
-
-
+
+ register_function
@@ -868,8 +846,8 @@ 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.
-
-
+
+ unregister_function
@@ -888,8 +866,8 @@ function print_current_date ($params) {
$smarty->unregister_function("fetch");
-
-
+
+ register_modifier
@@ -912,8 +890,8 @@ $smarty->register_modifier("sslash","stripslashes");
// now you can use {$var|sslash} to strip slashes from variables
-
-
+
+ unregister_modifier
@@ -932,8 +910,8 @@ $smarty->register_modifier("sslash","stripslashes");
$smarty->unregister_modifier("strip_tags");
-
-
+
+ register_resource
@@ -958,8 +936,8 @@ $smarty->register_resource("db", array("db_get_template",
"db_get_secure",
"db_get_trusted"));
-
-
+
+ unregister_resource
@@ -976,8 +954,8 @@ $smarty->register_resource("db", array("db_get_template",
$smarty->unregister_resource("db");
-
-
+
+ register_prefilter
@@ -991,8 +969,8 @@ $smarty->unregister_resource("db");
linkend="section.template.prefilters">template prefilters for
more information on how to setup a prefiltering function.
-
-
+
+ unregister_prefilter
@@ -1003,8 +981,8 @@ $smarty->unregister_resource("db");
Use this to dynamically unregister a prefilter.
-
-
+
+ register_postfilter
@@ -1018,8 +996,8 @@ $smarty->unregister_resource("db");
linkend="section.template.postfilters">template postfilters for
more information on how to setup a postfiltering function.
-
-
+
+ unregister_postfilter
@@ -1030,8 +1008,8 @@ $smarty->unregister_resource("db");
Use this to dynamically unregister a postfilter.
-
-
+
+ register_compiler_function
@@ -1045,8 +1023,8 @@ $smarty->unregister_resource("db");
Pass in the compiler function name, followed by the PHP
function that implements it.
-
-
+
+ unregister_compiler_function
@@ -1058,8 +1036,8 @@ $smarty->unregister_resource("db");
Use this to dynamically unregister a compiler function. Pass in
the name of the compiler function.
-
-
+
+ trigger_error
@@ -1075,9 +1053,9 @@ $smarty->unregister_resource("db");
E_USER_WARNING, etc. By default it's E_USER_WARNING. This function
was added to Smarty 2.0.
-
+
-
+ is_cached
@@ -1118,8 +1096,8 @@ if(!$smarty->is_cached("index.tpl","FrontPage")) {
$smarty->display("index.tpl","FrontPage");
-
-
+
+ get_template_vars
@@ -1139,8 +1117,8 @@ $tpl_vars = $smarty->get_template_vars();
// take a look at them
var_dump($tpl_vars);
-
-
+
+ display
@@ -1212,8 +1190,8 @@ $smarty->display("file:C:/www/pub/templates/header.tpl");
$smarty->display("db:header.tpl");
-
-
+
+ fetch
@@ -1270,413 +1248,7 @@ $output = $smarty->fetch("index.tpl");
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.
-
-
- Templates from $template_dir
-
- 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.
-
-
-
-using templates from $template_dir
-
-// from PHP script
-$smarty->display("index.tpl");
-$smarty->display("admin/menu.tpl");
-$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 *}
-
-
-
- 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
-
-// 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"}
-
-
-
-
-
- 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.
-
-
-
- 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
-function db_get_template ($tpl_name, &tpl_source, &$smarty_obj)
-{
- // do database call here to fetch your template,
- // populating $tpl_source
- $sql = new SQL;
- $sql->query("select tpl_source
- from my_table
- where tpl_name='$tpl_name'");
- if ($sql->num_rows) {
- $tpl_source = $sql->record['tpl_source'];
- return true;
- } else {
- return false;
- }
-}
-
-function db_get_timestamp($tpl_name, &$tpl_timestamp, &$smarty_obj)
-{
- // do database call here to populate $tpl_timestamp.
- $sql = new SQL;
- $sql->query("select tpl_timestamp
- from my_table
- where tpl_name='$tpl_name'");
- if ($sql->num_rows) {
- $tpl_timestamp = $sql->record['tpl_timestamp'];
- return true;
- } else {
- return false;
- }
-}
-
-function db_get_secure($tpl_name, &$smarty_obj)
-{
- // assume all templates are secure
- return true;
-}
-
-function db_get_trusted($tpl_name, &$smarty_obj)
-{
- // not used for templates
-}
-
-// register the resource name "db"
-$smarty->register_resource("db", array("db_get_template",
- "db_get_timestamp",
- "db_get_secure",
- "db_get_trusted"));
-
-// using resource from php script
-$smarty->display("db:index.tpl");
-
-{* using resource from within Smarty template *}
-{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.
-
-
- using the default template handler function
-
-// from PHP script
-
-// put this function somewhere in your application
-
-function make_template ($resource_type, $resource_name, &$template_source,
- &$template_timestamp, &$smarty_obj) {
-
- if( $resource_type == 'file' ) {
- if ( ! is_readable ( $resource_name )) {
- // create the template file, return contents.
- $template_source = "This is a new template.";
- $template_timestamp = time();
- $smarty_obj->_write_file($resource_name,$template_source);
- return true;
- }
- else {
- // not a file
- return false;
- }
-}
-
-// set the default handler
-$smarty->default_template_handler_func = 'make_template';
-
-
-
-
-
- Template Prefilters
-
- Template prefilters are PHP functions that your templates are ran through
- before they are compiled. This is good for preprocessing your templates
- to remove unwanted comments, keeping an eye on what people are putting
- in their templates, etc. Prefilters are processed in the order they are
- registered.
-
-
- Example of a Template Prefilter
-
- Create a function in your application that Smarty will use as a
- template prefilter. Smarty will pass the template source code as the
- first argument, an expect the function to return the resulting
- template source code.
-
-
- NOTE: As of Smarty 1.4.5, the Smarty object is passed as the second
- argument. This way you can reference and modify information in the
- Smarty object from within the prefilter function.
-
-
-
-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);
-}
-
-// register the prefilter
-$smarty->register_prefilter("remove_dw_comments");
-$smarty->display("index.tpl");
-
-{* from within Smarty template *}
-<!--# this line will get removed by the prefilter -->
-
-
-
-
- Template Postfilters
-
- Template postfilters are PHP functions that your templates are ran through
- after they are compiled. Postfilters are processed in the order they are
- registered.
-
-
- Example of a Template Postfilter
-
- Create a function in your application that Smarty will use as a
- template postfilter. Smarty will pass the template source code as the
- first argument, an expect the function to return the resulting
- template source code.
-
-
- The Smarty object is passed as the second argument. This way you
- can reference and modify information in the Smarty object from
- within the prefilter function.
-
-
-
-using a template postfilter
-
-// this program puts a
-// put this in your application, or in Smarty.addons.php
-function add_header_comment($tpl_source) {
- return "\n\" ?>\n".$tpl_source;
-}
-
-// register the postfilter
-$smarty->register_postfilter("add_header_comment");
-$smarty->display("index.tpl");
-
-{* from within Smarty template *}
-
-{* rest of template content... *}
-
-
-
-
- Template Cache Handling Function
-
- As an alternative to using the default file-based caching mechanism, you
- can specify a custom cache handling function that will be used to read,
- write and clear cached files.
-
-
- Example of a Template Cache Handling Function
-
- Create a function in your application that Smarty will use as a
- cache handler. Set the name of it in the $cache_handler_func class
- variable. Smarty will now use this to handle cached data. The first
- argument is the action, which will be one of 'read', 'write' and
- 'clear'. The second parameter is the smarty object. The third
- parameter is the cached content. Upon a write, Smarty passes the
- cached content in these parameters. Upon a 'read', Smarty expects
- your function to pass this by reference and populate it with the
- cached data. Upon a 'clear', pass a dummy variable here since it is
- not used. The fourth parameter is the name of the template file
- (needed for read/write), the fifth parameter is the cache_id
- (optional), and the sixth is the compile_id (optional)
-
-
-
-example using MySQL as a cache source
-
-<?php
-
-/*
-
-example usage:
-
-include('Smarty.class.php');
-include('mysql_cache_handler.php');
-
-$smarty = new Smarty;
-$smarty->cache_handler_func='mysql_cache_handler';
-
-$smarty->display('index.tpl');
-
-
-mysql database is expected in this format:
-
-create database SMARTY_CACHE;
-
-create table CACHE_PAGES(
-CacheID char(32) PRIMARY KEY,
-CacheContents MEDIUMTEXT NOT NULL
-);
-
-*/
-
-function mysql_cache_handler($action, &$smarty_obj, &$cache_content, $tpl_file=null, $cache_id=null, $compile_id=null) {
- // set db host, user and pass here
- $db_host = 'localhost';
- $db_user = 'myuser';
- $db_pass = 'mypass';
- $db_name = 'SMARTY_CACHE';
- $use_gzip = false;
-
- // create unique cache id
- $CacheID = md5($tpl_file.$cache_id.$compile_id);
-
- if(! $link = mysql_pconnect($db_host, $db_user, $db_pass)) {
- $smarty_obj->_trigger_error_msg("cache_handler: could not connect to database");
- return false;
- }
- mysql_select_db($db_name);
-
- switch ($action) {
- case 'read':
- // save cache to database
- $results = mysql_query("select CacheContents from CACHE_PAGES where CacheID='$CacheID'");
- if(!$results) {
- $smarty_obj->_trigger_error_msg("cache_handler: query failed.");
- }
- $row = mysql_fetch_array($results,MYSQL_ASSOC);
-
- if($use_gzip && function_exists("gzuncompress")) {
- $cache_contents = gzuncompress($row["CacheContents"]);
- } else {
- $cache_contents = $row["CacheContents"];
- }
- $return = $results;
- break;
- case 'write':
- // save cache to database
-
- if($use_gzip && function_exists("gzcompress")) {
- // compress the contents for storage efficiency
- $contents = gzcompress($cache_content);
- } else {
- $contents = $cache_content;
- }
- $results = mysql_query("replace into CACHE_PAGES values(
- '$CacheID',
- '".addslashes($contents)."')
- ");
- if(!$results) {
- $smarty_obj->_trigger_error_msg("cache_handler: query failed.");
- }
- $return = $results;
- break;
- case 'clear':
- // clear cache info
- if(empty($cache_id) && empty($compile_id) && empty($tpl_file)) {
- // clear them all
- $results = mysql_query("delete from CACHE_PAGES");
- } else {
- $results = mysql_query("delete from CACHE_PAGES where CacheID='$CacheID'");
- }
- if(!$results) {
- $smarty_obj->_trigger_error_msg("cache_handler: query failed.");
- }
- $return = $results;
- break;
- default:
- // error, unknown action
- $smarty_obj->_trigger_error_msg("cache_handler: unknown action \"$action\"");
- $return = false;
- break;
- }
- mysql_close($link);
- return $return;
-
-}
-
-?>
-
-
-
-
+
Smarty Templates
@@ -6322,6 +5894,415 @@ function smarty_insert_time($params, &$smarty)
+
+ Advanced Features
+
+ Template Prefilters
+
+ Template prefilters are PHP functions that your templates are ran through
+ before they are compiled. This is good for preprocessing your templates
+ to remove unwanted comments, keeping an eye on what people are putting
+ in their templates, etc. Prefilters are processed in the order they are
+ registered.
+
+
+ Example of a Template Prefilter
+
+ Create a function in your application that Smarty will use as a
+ template prefilter. Smarty will pass the template source code as the
+ first argument, an expect the function to return the resulting
+ template source code.
+
+
+ NOTE: As of Smarty 1.4.5, the Smarty object is passed as the second
+ argument. This way you can reference and modify information in the
+ Smarty object from within the prefilter function.
+
+
+
+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);
+}
+
+// register the prefilter
+$smarty->register_prefilter("remove_dw_comments");
+$smarty->display("index.tpl");
+
+{* from within Smarty template *}
+<!--# this line will get removed by the prefilter -->
+
+
+
+
+ Template Postfilters
+
+ Template postfilters are PHP functions that your templates are ran through
+ after they are compiled. Postfilters are processed in the order they are
+ registered.
+
+
+ Example of a Template Postfilter
+
+ Create a function in your application that Smarty will use as a
+ template postfilter. Smarty will pass the template source code as the
+ first argument, an expect the function to return the resulting
+ template source code.
+
+
+ The Smarty object is passed as the second argument. This way you
+ can reference and modify information in the Smarty object from
+ within the prefilter function.
+
+
+
+using a template postfilter
+
+// this program puts a
+// put this in your application, or in Smarty.addons.php
+function add_header_comment($tpl_source) {
+ return "\n\" ?>\n".$tpl_source;
+}
+
+// register the postfilter
+$smarty->register_postfilter("add_header_comment");
+$smarty->display("index.tpl");
+
+{* from within Smarty template *}
+
+{* rest of template content... *}
+
+
+
+
+ Template Cache Handling Function
+
+ As an alternative to using the default file-based caching mechanism, you
+ can specify a custom cache handling function that will be used to read,
+ write and clear cached files.
+
+
+ Example of a Template Cache Handling Function
+
+ Create a function in your application that Smarty will use as a
+ cache handler. Set the name of it in the $cache_handler_func class
+ variable. Smarty will now use this to handle cached data. The first
+ argument is the action, which will be one of 'read', 'write' and
+ 'clear'. The second parameter is the smarty object. The third
+ parameter is the cached content. Upon a write, Smarty passes the
+ cached content in these parameters. Upon a 'read', Smarty expects
+ your function to pass this by reference and populate it with the
+ cached data. Upon a 'clear', pass a dummy variable here since it is
+ not used. The fourth parameter is the name of the template file
+ (needed for read/write), the fifth parameter is the cache_id
+ (optional), and the sixth is the compile_id (optional)
+
+
+
+example using MySQL as a cache source
+
+<?php
+
+/*
+
+example usage:
+
+include('Smarty.class.php');
+include('mysql_cache_handler.php');
+
+$smarty = new Smarty;
+$smarty->cache_handler_func='mysql_cache_handler';
+
+$smarty->display('index.tpl');
+
+
+mysql database is expected in this format:
+
+create database SMARTY_CACHE;
+
+create table CACHE_PAGES(
+CacheID char(32) PRIMARY KEY,
+CacheContents MEDIUMTEXT NOT NULL
+);
+
+*/
+
+function mysql_cache_handler($action, &$smarty_obj, &$cache_content, $tpl_file=null, $cache_id=null, $compile_id=null) {
+ // set db host, user and pass here
+ $db_host = 'localhost';
+ $db_user = 'myuser';
+ $db_pass = 'mypass';
+ $db_name = 'SMARTY_CACHE';
+ $use_gzip = false;
+
+ // create unique cache id
+ $CacheID = md5($tpl_file.$cache_id.$compile_id);
+
+ if(! $link = mysql_pconnect($db_host, $db_user, $db_pass)) {
+ $smarty_obj->_trigger_error_msg("cache_handler: could not connect to database");
+ return false;
+ }
+ mysql_select_db($db_name);
+
+ switch ($action) {
+ case 'read':
+ // save cache to database
+ $results = mysql_query("select CacheContents from CACHE_PAGES where CacheID='$CacheID'");
+ if(!$results) {
+ $smarty_obj->_trigger_error_msg("cache_handler: query failed.");
+ }
+ $row = mysql_fetch_array($results,MYSQL_ASSOC);
+
+ if($use_gzip && function_exists("gzuncompress")) {
+ $cache_contents = gzuncompress($row["CacheContents"]);
+ } else {
+ $cache_contents = $row["CacheContents"];
+ }
+ $return = $results;
+ break;
+ case 'write':
+ // save cache to database
+
+ if($use_gzip && function_exists("gzcompress")) {
+ // compress the contents for storage efficiency
+ $contents = gzcompress($cache_content);
+ } else {
+ $contents = $cache_content;
+ }
+ $results = mysql_query("replace into CACHE_PAGES values(
+ '$CacheID',
+ '".addslashes($contents)."')
+ ");
+ if(!$results) {
+ $smarty_obj->_trigger_error_msg("cache_handler: query failed.");
+ }
+ $return = $results;
+ break;
+ case 'clear':
+ // clear cache info
+ if(empty($cache_id) && empty($compile_id) && empty($tpl_file)) {
+ // clear them all
+ $results = mysql_query("delete from CACHE_PAGES");
+ } else {
+ $results = mysql_query("delete from CACHE_PAGES where CacheID='$CacheID'");
+ }
+ if(!$results) {
+ $smarty_obj->_trigger_error_msg("cache_handler: query failed.");
+ }
+ $return = $results;
+ break;
+ default:
+ // error, unknown action
+ $smarty_obj->_trigger_error_msg("cache_handler: unknown action \"$action\"");
+ $return = false;
+ break;
+ }
+ mysql_close($link);
+ return $return;
+
+}
+
+?>
+
+
+
+
+
+ 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.
+
+
+ Templates from $template_dir
+
+ 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.
+
+
+
+using templates from $template_dir
+
+// from PHP script
+$smarty->display("index.tpl");
+$smarty->display("admin/menu.tpl");
+$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 *}
+
+
+
+ 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
+
+// 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"}
+
+
+
+
+
+ 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.
+
+
+
+ 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
+function db_get_template ($tpl_name, &tpl_source, &$smarty_obj)
+{
+ // do database call here to fetch your template,
+ // populating $tpl_source
+ $sql = new SQL;
+ $sql->query("select tpl_source
+ from my_table
+ where tpl_name='$tpl_name'");
+ if ($sql->num_rows) {
+ $tpl_source = $sql->record['tpl_source'];
+ return true;
+ } else {
+ return false;
+ }
+}
+
+function db_get_timestamp($tpl_name, &$tpl_timestamp, &$smarty_obj)
+{
+ // do database call here to populate $tpl_timestamp.
+ $sql = new SQL;
+ $sql->query("select tpl_timestamp
+ from my_table
+ where tpl_name='$tpl_name'");
+ if ($sql->num_rows) {
+ $tpl_timestamp = $sql->record['tpl_timestamp'];
+ return true;
+ } else {
+ return false;
+ }
+}
+
+function db_get_secure($tpl_name, &$smarty_obj)
+{
+ // assume all templates are secure
+ return true;
+}
+
+function db_get_trusted($tpl_name, &$smarty_obj)
+{
+ // not used for templates
+}
+
+// register the resource name "db"
+$smarty->register_resource("db", array("db_get_template",
+ "db_get_timestamp",
+ "db_get_secure",
+ "db_get_trusted"));
+
+// using resource from php script
+$smarty->display("db:index.tpl");
+
+{* using resource from within Smarty template *}
+{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.
+
+
+ using the default template handler function
+
+// from PHP script
+
+// put this function somewhere in your application
+
+function make_template ($resource_type, $resource_name, &$template_source,
+ &$template_timestamp, &$smarty_obj) {
+
+ if( $resource_type == 'file' ) {
+ if ( ! is_readable ( $resource_name )) {
+ // create the template file, return contents.
+ $template_source = "This is a new template.";
+ $template_timestamp = time();
+ $smarty_obj->_write_file($resource_name,$template_source);
+ return true;
+ }
+ else {
+ // not a file
+ return false;
+ }
+}
+
+// set the default handler
+$smarty->default_template_handler_func = 'make_template';
+
+
+
+
+
+
Debugging Console