diff --git a/docs/en/programmers.xml b/docs/en/programmers.xml index edb91e17..672d23f8 100644 --- a/docs/en/programmers.xml +++ b/docs/en/programmers.xml @@ -152,9 +152,13 @@ require_once(SMARTY_DIR."Smarty.class.php"); where keys are filter types and values are arrays of the filter names. For example: - -$smarty->autoload_filters = array('pre' => array('trim', 'stamp'), - 'output' => array('convert')); + +autoload_filters = array('pre' => array('trim', 'stamp'), + 'output' => array('convert')); +?> +]]> @@ -544,13 +548,18 @@ $smarty->autoload_filters = array('pre' => array('trim', 'stamp'), append - + +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")); +?> +]]> + @@ -588,10 +597,15 @@ $smarty->append(array("city" => "Lincoln","state" => "Nebraska")); append_by_ref - + +append_by_ref("Name",$myname); -$smarty->append_by_ref("Address",$address); +$smarty->append_by_ref("Name", $myname); +$smarty->append_by_ref("Address", $address); +?> +]]> + @@ -614,13 +628,18 @@ $smarty->append_by_ref("Address",$address); assign - + +assign("Name","Fred"); -$smarty->assign("Address",$address); +$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")); +?> +]]> + @@ -649,10 +668,15 @@ $smarty->assign(array("city" => "Lincoln","state" => "Nebraska")); assign_by_ref - + +assign_by_ref("Name",$myname); -$smarty->assign_by_ref("Address",$address); +$smarty->assign_by_ref('Name', $myname); +$smarty->assign_by_ref('Address', $address); +?> +]]> + @@ -668,9 +692,14 @@ $smarty->assign_by_ref("Address",$address); clear_all_assign - + +clear_all_assign(); +$smarty->clear_all_assign(); +?> +]]> + @@ -688,9 +717,14 @@ $smarty->clear_all_assign(); clear_all_cache - + +clear_all_cache(); +$smarty->clear_all_cache(); +?> +]]> + @@ -707,12 +741,16 @@ $smarty->clear_all_cache(); clear_assign - + +clear_assign("Name"); // clear multiple variables $smarty->clear_assign(array("Name","Address","Zip")); +?> +]]> @@ -738,12 +776,17 @@ $smarty->clear_assign(array("Name","Address","Zip")); clear_cache - + +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"); +?> +]]> + @@ -765,12 +808,17 @@ $smarty->clear_cache("index.tpl","CACHEID"); clear_compiled_tpl - + +clear_compiled_tpl("index.tpl"); // clear entire compile directory -$smarty->clear_compiled_tpl(); +$smarty->clear_compiled_tpl(); +?> +]]> + @@ -1004,14 +1052,19 @@ print_r($config_vars); get_registered_object - -function smarty_block_foo($params, &$smarty) { + +&get_registered_object($params['object']); + $obj_ref = &$smarty->&get_registered_object($params['object']); // use $obj_ref is now a reference to the object } -} +} +?> +]]> + @@ -1026,7 +1079,9 @@ function smarty_block_foo($params, &$smarty) { get_template_vars - + +get_template_vars('foo'); @@ -1034,7 +1089,10 @@ $foo = $smarty->get_template_vars('foo'); $tpl_vars = $smarty->get_template_vars(); // take a look at them -print_r($tpl_vars); +print_r($tpl_vars); +?> +]]> + @@ -1053,14 +1111,19 @@ print_r($tpl_vars); is_cached - + +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 @@ -1068,16 +1131,21 @@ $smarty->display("index.tpl"); is_cached with multiple-cache template - + +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"); +?> +]]> + - + load_filter @@ -1095,10 +1163,15 @@ $smarty->display("index.tpl","FrontPage"); loading filter plugins - + +load_filter('pre', 'trim'); // load prefilter named 'trim' $smarty->load_filter('pre', 'datefooter'); // load another prefilter named 'datefooter' -$smarty->load_filter('output', 'compress'); // load output filter named 'compress' +$smarty->load_filter('output', 'compress'); // load output filter named 'compress' +?> +]]> + @@ -1121,11 +1194,11 @@ $smarty->load_filter('output', 'compress'); // load output filter named 'compres The php-function callback impl can be either (a) a string containing the function name or (b) an array of the form - array(&$object, $method) with - &$object being a reference to an + array(&$object, $method) with + &$object being a reference to an object and $method being a string containing the mehod-name or (c) an array of the form - array(&$class, $method) with + array(&$class, $method) with $class being a classname and $method being a class method of that class. @@ -1135,22 +1208,27 @@ $smarty->load_filter('output', 'compress'); // load output filter named 'compres register_block - + +register_block("translate", "do_translation"); -function do_translation ($params, $content, &$smarty, &$repeat) { +function do_translation ($params, $content, &$smarty, &$repeat) { if (isset($content)) { $lang = $params['lang']; // do some translation with $content return $translation; } } +?> {* template *} {translate lang="br"} Hello, world! -{/translate} +{/translate} +]]> + @@ -1171,11 +1249,11 @@ function do_translation ($params, $content, &$smarty, &$repeat) { The php-function callback impl can be either (a) a string containing the function name or (b) an array of the form - array(&$object, $method) with - &$object being a reference to an + array(&$object, $method) with + &$object being a reference to an object and $method being a string containing the mehod-name or (c) an array of the form - array(&$class, $method) with + array(&$class, $method) with $class being a classname and $method being a class method of that class. @@ -1205,11 +1283,11 @@ function do_translation ($params, $content, &$smarty, &$repeat) { The php-function callback impl can be either (a) a string containing the function name or (b) an array of the form - array(&$object, $method) with - &$object being a reference to an + array(&$object, $method) with + &$object being a reference to an object and $method being a string containing the mehod-name or (c) an array of the form - array(&$class, $method) with + array(&$class, $method) with $class being a classname and $method being a class method of that class. @@ -1219,7 +1297,9 @@ function do_translation ($params, $content, &$smarty, &$repeat) { register_function - + +register_function("date_now", "print_current_date"); function print_current_date ($params) { @@ -1231,7 +1311,10 @@ 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. +?> +]]> + @@ -1251,11 +1334,11 @@ function print_current_date ($params) { The php-function callback impl can be either (a) a string containing the function name or (b) an array of the form - array(&$object, $method) with - &$object being a reference to an + array(&$object, $method) with + &$object being a reference to an object and $method being a string containing the mehod-name or (c) an array of the form - array(&$class, $method) with + array(&$class, $method) with $class being a classname and $method being a class method of that class. @@ -1263,12 +1346,17 @@ function print_current_date ($params) { register_modifier - + +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 +?> +]]> + @@ -1307,11 +1395,11 @@ $smarty->register_modifier("sslash","stripslashes"); The php-function callback function can be either (a) a string containing the function name or (b) an array of the form - array(&$object, $method) with - &$object being a reference to an + array(&$object, $method) with + &$object being a reference to an object and $method being a string containing the mehod-name or (c) an array of the form - array(&$class, $method) with + array(&$class, $method) with $class being a classname and $method being a class method of that class. @@ -1334,11 +1422,11 @@ $smarty->register_modifier("sslash","stripslashes"); The php-function callback function can be either (a) a string containing the function name or (b) an array of the form - array(&$object, $method) with - &$object being a reference to an + array(&$object, $method) with + &$object being a reference to an object and $method being a string containing the mehod-name or (c) an array of the form - array(&$class, $method) with + array(&$class, $method) with $class being a classname and $method being a class method of that class. @@ -1361,11 +1449,11 @@ $smarty->register_modifier("sslash","stripslashes"); The php-function callback function can be either (a) a string containing the function name or (b) an array of the form - array(&$object, $method) with - &$object being a reference to an + array(&$object, $method) with + &$object being a reference to an object and $method being a string containing the mehod-name or (c) an array of the form - array(&$class, $method) with + array(&$class, $method) with $class being a classname and $method being a class method of that class. @@ -1893,7 +1981,7 @@ require('Smarty.class.php'); $smarty = new Smarty; $smarty->caching = true; -function remaining_seconds($params, &$smarty) { +function remaining_seconds($params, &$smarty) { $remain = $params['endtime'] - time(); if ($remain >=0) return $remain . " second(s)"; @@ -1929,7 +2017,7 @@ require('Smarty.class.php'); $smarty = new Smarty; $smarty->caching = true; -function smarty_block_dynamic($param, $content, &$smarty) { +function smarty_block_dynamic($param, $content, &$smarty) { return $content; } $smarty->register_block('dynamic', 'smarty_block_dynamic', false); @@ -1999,8 +2087,8 @@ When reloading the page you will notice that both dates differ. One is "dynamic" parameters for block-function-plugins: They get 4 parameters $params, $content, - &$smarty and - &$repeat and they also behave like + &$smarty and + &$repeat and they also behave like block-function-plugins. @@ -2010,7 +2098,7 @@ When reloading the page you will notice that both dates differ. One is "dynamic" // the object class My_Object { - function meth1($params, &$smarty_obj) { + function meth1($params, &$smarty_obj) { return "this is my meth1"; } } @@ -2062,7 +2150,7 @@ the output was {$output} <?php // put this in your application -function remove_dw_comments($tpl_source, &$smarty) +function remove_dw_comments($tpl_source, &$smarty) { return preg_replace("/<!--#.*-->/U","",$tpl_source); } @@ -2096,7 +2184,7 @@ $smarty->display("index.tpl"); <?php // put this in your application -function add_header_comment($tpl_source, &$smarty) +function add_header_comment($tpl_source, &$smarty) { return "<?php echo \"<!-- Created by Smarty! -->\n\" ?>\n".$tpl_source; } @@ -2137,7 +2225,7 @@ $smarty->display("index.tpl"); <?php // put this in your application -function protect_email($tpl_output, &$smarty) +function protect_email($tpl_output, &$smarty) { $tpl_output = preg_replace('!(\S+)@([a-zA-Z0-9\.\-]+\.([a-zA-Z]{2,3}|[0-9]{1,3}))!', @@ -2208,7 +2296,7 @@ CacheContents MEDIUMTEXT NOT NULL */ -function mysql_cache_handler($action, &$smarty_obj, &$cache_content, $tpl_file=null, $cache_id=null, $compile_id=null, $exp_time=null) +function mysql_cache_handler($action, &$smarty_obj, &$cache_content, $tpl_file=null, $cache_id=null, $compile_id=null, $exp_time=null) { // set db host, user and pass here $db_host = 'localhost'; @@ -2235,7 +2323,7 @@ function mysql_cache_handler($action, &$smarty_obj, &$cache_content, $tp } $row = mysql_fetch_array($results,MYSQL_ASSOC); - if($use_gzip && function_exists("gzuncompress")) { + if($use_gzip && function_exists("gzuncompress")) { $cache_contents = gzuncompress($row["CacheContents"]); } else { $cache_contents = $row["CacheContents"]; @@ -2245,7 +2333,7 @@ function mysql_cache_handler($action, &$smarty_obj, &$cache_content, $tp case 'write': // save cache to database - if($use_gzip && function_exists("gzcompress")) { + if($use_gzip && function_exists("gzcompress")) { // compress the contents for storage efficiency $contents = gzcompress($cache_content); } else { @@ -2262,7 +2350,7 @@ function mysql_cache_handler($action, &$smarty_obj, &$cache_content, $tp break; case 'clear': // clear cache info - if(empty($cache_id) && empty($compile_id) && empty($tpl_file)) { + if(empty($cache_id) && empty($compile_id) && empty($tpl_file)) { // clear them all $results = mysql_query("delete from CACHE_PAGES"); } else { @@ -2387,7 +2475,7 @@ $smarty->display("file:F:/path/to/my/templates/menu.tpl"); // from PHP script // put these function somewhere in your application -function db_get_template ($tpl_name, &$tpl_source, &$smarty_obj) +function db_get_template ($tpl_name, &$tpl_source, &$smarty_obj) { // do database call here to fetch your template, // populating $tpl_source @@ -2403,7 +2491,7 @@ function db_get_template ($tpl_name, &$tpl_source, &$smarty_obj) } } -function db_get_timestamp($tpl_name, &$tpl_timestamp, &$smarty_obj) +function db_get_timestamp($tpl_name, &$tpl_timestamp, &$smarty_obj) { // do database call here to populate $tpl_timestamp. $sql = new SQL; @@ -2418,13 +2506,13 @@ function db_get_timestamp($tpl_name, &$tpl_timestamp, &$smarty_obj) } } -function db_get_secure($tpl_name, &$smarty_obj) +function db_get_secure($tpl_name, &$smarty_obj) { // assume all templates are secure return true; } -function db_get_trusted($tpl_name, &$smarty_obj) +function db_get_trusted($tpl_name, &$smarty_obj) { // not used for templates } @@ -2457,7 +2545,7 @@ $smarty->display("db:index.tpl"); <?php // put this function somewhere in your application -function make_template ($resource_type, $resource_name, &$template_source, &$template_timestamp, &$smarty_obj) +function make_template ($resource_type, $resource_name, &$template_source, &$template_timestamp, &$smarty_obj) { if( $resource_type == 'file' ) { if ( ! is_readable ( $resource_name )) { @@ -2609,7 +2697,7 @@ require_once $smarty->_get_plugin_filepath('function', 'html_options');&$repeat after the Smarty object to keep + &$repeat after the Smarty object to keep backwards compatibility to older versions of Smarty). @@ -2619,7 +2707,7 @@ require_once $smarty->_get_plugin_filepath('function', 'html_options'); void smarty_function_name array $params - object &$smarty + object &$smarty @@ -2657,7 +2745,7 @@ require_once $smarty->_get_plugin_filepath('function', 'html_options'); * Purpose: assign a value to a template variable * ------------------------------------------------------------- */ -function smarty_function_assign($params, &$smarty) +function smarty_function_assign($params, &$smarty) { if (empty($params['var'])) { $smarty->trigger_error("assign: missing 'var' parameter"); @@ -2804,7 +2892,7 @@ function smarty_modifier_truncate($string, $length = 80, $etc = '...', void smarty_block_name array $params mixed $content - object &$smarty + object &$smarty @@ -2817,7 +2905,7 @@ function smarty_modifier_truncate($string, $length = 80, $etc = '...', By default your function implementation is called twice by Smarty: once for the opening tag, and once for the closing tag - (see &$repeat below how to change this). + (see &$repeat below how to change this). Only the opening tag of the block function may have attributes. All @@ -2838,7 +2926,7 @@ function smarty_modifier_truncate($string, $length = 80, $etc = '...', - The parameter &$repeat is passed by + The parameter &$repeat is passed by reference to the function implementation and provides a possibility for it to control how many times the block is displayed. By default $repeat is @@ -2846,7 +2934,7 @@ function smarty_modifier_truncate($string, $length = 80, $etc = '...', (the block opening tag) and false on all subsequent calls to the block function (the block's closing tag). Each time the function implementation returns with - &$repeat being true, the contents between + &$repeat being true, the contents between {func} .. {/func} are evaluated and the function implementation is called again with the new block contents in the parameter $content. @@ -2877,7 +2965,7 @@ function smarty_modifier_truncate($string, $length = 80, $etc = '...', * Purpose: translate a block of text * ------------------------------------------------------------- */ -function smarty_block_translate($params, $content, &$smarty) +function smarty_block_translate($params, $content, &$smarty) { if (isset($content)) { $lang = $params['lang']; @@ -2900,7 +2988,7 @@ function smarty_block_translate($params, $content, &$smarty) mixed smarty_compiler_name string $tag_arg - object &$smarty + object &$smarty @@ -2928,7 +3016,7 @@ function smarty_block_translate($params, $content, &$smarty) * the time it was compiled. * ------------------------------------------------------------- */ -function smarty_compiler_tplheader($tag_arg, &$smarty) +function smarty_compiler_tplheader($tag_arg, &$smarty) { return "\necho '" . $smarty->_current_file . " compiled at " . date('Y-m-d H:M'). "';"; } @@ -2960,7 +3048,7 @@ echo 'index.tpl compiled at 2002-02-20 20:02'; string smarty_prefilter_name string $source - object &$smarty + object &$smarty @@ -2974,7 +3062,7 @@ echo 'index.tpl compiled at 2002-02-20 20:02'; string smarty_postfilter_name string $compiled - object &$smarty + object &$smarty @@ -2998,7 +3086,7 @@ echo 'index.tpl compiled at 2002-02-20 20:02'; * Purpose: Convert html tags to be lowercase. * ------------------------------------------------------------- */ - function smarty_prefilter_pre01($source, &$smarty) + function smarty_prefilter_pre01($source, &$smarty) { return preg_replace('!<(\w+)[^>]+>!e', 'strtolower("$1")', $source); } @@ -3018,7 +3106,7 @@ echo 'index.tpl compiled at 2002-02-20 20:02'; * Purpose: Output code that lists all current template vars. * ------------------------------------------------------------- */ - function smarty_postfilter_post01($compiled, &$smarty) + function smarty_postfilter_post01($compiled, &$smarty) { $compiled = "<pre>\n<?php print_r(\$this->get_template_vars()); ?>\n</pre>" . $compiled; return $compiled; @@ -3036,7 +3124,7 @@ echo 'index.tpl compiled at 2002-02-20 20:02'; string smarty_outputfilter_name string $template_output - object &$smarty + object &$smarty @@ -3058,7 +3146,7 @@ echo 'index.tpl compiled at 2002-02-20 20:02'; * a simple protection against spambots * ------------------------------------------------------------- */ - function smarty_outputfilter_protect_email($output, &$smarty) + function smarty_outputfilter_protect_email($output, &$smarty) { return preg_replace('!(\S+)@([a-zA-Z0-9\.\-]+\.([a-zA-Z]{2,3}|[0-9]{1,3}))!', '$1%40$2', $output); @@ -3085,24 +3173,24 @@ echo 'index.tpl compiled at 2002-02-20 20:02'; bool smarty_resource_name_source string $rsrc_name - string &$source - object &$smarty + string &$source + object &$smarty bool smarty_resource_name_timestamp string $rsrc_name - int &$timestamp - object &$smarty + int &$timestamp + object &$smarty bool smarty_resource_name_secure string $rsrc_name - object &$smarty + object &$smarty bool smarty_resource_name_trusted string $rsrc_name - object &$smarty + object &$smarty @@ -3157,7 +3245,7 @@ echo 'index.tpl compiled at 2002-02-20 20:02'; * Purpose: Fetches templates from a database * ------------------------------------------------------------- */ -function smarty_resource_db_source($tpl_name, &$tpl_source, &$smarty) +function smarty_resource_db_source($tpl_name, &$tpl_source, &$smarty) { // do database call here to fetch your template, // populating $tpl_source @@ -3173,7 +3261,7 @@ function smarty_resource_db_source($tpl_name, &$tpl_source, &$smarty) } } -function smarty_resource_db_timestamp($tpl_name, &$tpl_timestamp, &$smarty) +function smarty_resource_db_timestamp($tpl_name, &$tpl_timestamp, &$smarty) { // do database call here to populate $tpl_timestamp. $sql = new SQL; @@ -3188,13 +3276,13 @@ function smarty_resource_db_timestamp($tpl_name, &$tpl_timestamp, &$smar } } -function smarty_resource_db_secure($tpl_name, &$smarty) +function smarty_resource_db_secure($tpl_name, &$smarty) { // assume all templates are secure return true; } -function smarty_resource_db_trusted($tpl_name, &$smarty) +function smarty_resource_db_trusted($tpl_name, &$smarty) { // not used for templates } @@ -3212,7 +3300,7 @@ function smarty_resource_db_trusted($tpl_name, &$smarty) string smarty_insert_name array $params - object &$smarty + object &$smarty @@ -3237,7 +3325,7 @@ function smarty_resource_db_trusted($tpl_name, &$smarty) * Purpose: Inserts current date/time according to format * ------------------------------------------------------------- */ -function smarty_insert_time($params, &$smarty) +function smarty_insert_time($params, &$smarty) { if (empty($params['format'])) { $smarty->trigger_error("insert time: missing 'format' parameter");