From 0c781dd0bb44b7f30429628403d59888f8fe10e5 Mon Sep 17 00:00:00 2001 From: nlopess Date: Thu, 25 Mar 2004 16:39:16 +0000 Subject: [PATCH] fixing examples --- docs/en/programmers.xml | 429 ++++++++++++++++++++++++++++------------ 1 file changed, 303 insertions(+), 126 deletions(-) diff --git a/docs/en/programmers.xml b/docs/en/programmers.xml index 672d23f8..cd3c90b5 100644 --- a/docs/en/programmers.xml +++ b/docs/en/programmers.xml @@ -833,12 +833,17 @@ $smarty->clear_compiled_tpl(); clear_config - + +clear_config(); // clear one variable -$smarty->clear_config('foobar'); +$smarty->clear_config('foobar'); +?> +]]> + @@ -867,12 +872,17 @@ $smarty->clear_config('foobar'); config_load - + +config_load('my.conf'); // load a section -$smarty->config_load('my.conf','foobar'); +$smarty->config_load('my.conf','foobar'); +?> +]]> + @@ -904,7 +914,9 @@ $smarty->config_load('my.conf','foobar'); display - + +caching = true; @@ -928,7 +940,10 @@ if(!$smarty->is_cached("index.tpl")) } // display the output -$smarty->display("index.tpl"); +$smarty->display("index.tpl"); +?> +]]> + Use the syntax for display("index.tpl"); function display template resource examples - + +display("/usr/local/include/templates/header.tpl"); @@ -948,7 +965,10 @@ $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"); +?> +]]> + @@ -982,7 +1002,9 @@ $smarty->display("db:header.tpl"); fetch - + +fetch("index.tpl"); // do something with $output here -echo $output; +echo $output; +?> +]]> + @@ -1026,7 +1051,9 @@ echo $output; get_config_vars - + +get_config_vars('foo'); @@ -1034,7 +1061,10 @@ $foo = $smarty->get_config_vars('foo'); $config_vars = $smarty->get_config_vars(); // take a look at them -print_r($config_vars); +print_r($config_vars); +?> +]]> + @@ -1211,7 +1241,6 @@ $smarty->load_filter('output', 'compress'); // load output filter named 'compres register_block("translate", "do_translation"); function do_translation ($params, $content, &$smarty, &$repeat) { @@ -1497,11 +1526,16 @@ $smarty->register_modifier("sslash","stripslashes"); register_resource - + +register_resource("db", array("db_get_template", "db_get_timestamp", "db_get_secure", - "db_get_trusted")); + "db_get_trusted")); +?> +]]> + @@ -1575,10 +1609,15 @@ $smarty->register_resource("db", array("db_get_template", unregister_function - + +unregister_function("fetch"); +$smarty->unregister_function("fetch"); +?> +]]> + @@ -1595,10 +1634,15 @@ $smarty->unregister_function("fetch"); unregister_modifier - + +unregister_modifier("strip_tags"); +$smarty->unregister_modifier("strip_tags"); +?> +]]> + @@ -1663,8 +1707,13 @@ $smarty->unregister_modifier("strip_tags"); unregister_resource - -$smarty->unregister_resource("db"); + +unregister_resource("db"); +?> +]]> + @@ -1697,13 +1746,18 @@ $smarty->unregister_resource("db"); enabling caching - + +caching = true; -$smarty->display('index.tpl'); +$smarty->display('index.tpl'); +?> +]]> + With caching enabled, the function call to display('index.tpl') will render @@ -1730,7 +1784,9 @@ $smarty->display('index.tpl'); setting cache_lifetime per cache - + +display('home.tpl'); // to 1 hour, and will no longer respect the value of $cache_lifetime. // The home.tpl cache will still expire after 1 hour. $smarty->cache_lifetime = 30; // 30 seconds -$smarty->display('home.tpl'); +$smarty->display('home.tpl'); +?> +]]> + If $compile_check is enabled, @@ -1760,14 +1819,19 @@ $smarty->display('home.tpl'); enabling $compile_check - + +caching = true; $smarty->compile_check = true; -$smarty->display('index.tpl'); +$smarty->display('index.tpl'); +?> +]]> + If $force_compile is enabled, @@ -1784,7 +1848,9 @@ $smarty->display('index.tpl'); using is_cached() - + +is_cached('index.tpl')) { $smarty->assign($contents); } -$smarty->display('index.tpl'); +$smarty->display('index.tpl'); +?> +]]> + You can keep parts of a page dynamic with the display('index.tpl'); clearing the cache - + +clear_all_cache(); // clear only cache for index.tpl $smarty->clear_cache('index.tpl'); -$smarty->display('index.tpl'); +$smarty->display('index.tpl'); +?> +]]> + @@ -1841,7 +1915,9 @@ $smarty->display('index.tpl'); passing a cache_id to display() - + +caching = true; $my_cache_id = $_GET['article_id']; -$smarty->display('index.tpl',$my_cache_id); +$smarty->display('index.tpl',$my_cache_id); +?> +]]> + Above, we are passing the variable $my_cache_id to display() as the @@ -1879,7 +1958,9 @@ $smarty->display('index.tpl',$my_cache_id); passing a cache_id to is_cached() - + +is_cached('index.tpl',$my_cache_id)) { $smarty->assign($contents); } -$smarty->display('index.tpl',$my_cache_id); +$smarty->display('index.tpl',$my_cache_id); +?> +]]> + You can clear all caches for a particular cache_id by passing null as the @@ -1901,7 +1985,9 @@ $smarty->display('index.tpl',$my_cache_id); clearing all caches for a particular cache_id - + +caching = true; // clear all caches with "sports" as the cache_id $smarty->clear_cache(null,"sports"); -$smarty->display('index.tpl',"sports"); +$smarty->display('index.tpl',"sports"); +?> +]]> + In this manner, you can "group" your caches together by giving them the @@ -1926,7 +2015,9 @@ $smarty->display('index.tpl',"sports"); cache_id groups - + +clear_cache(null,"sports|basketball"); // include "sports|basketball", or "sports|(anything)|(anything)|(anything)|..." $smarty->clear_cache(null,"sports"); -$smarty->display('index.tpl',"sports|basketball"); +$smarty->display('index.tpl',"sports|basketball"); +?> +]]> + Technical Note @@ -1974,9 +2068,11 @@ In contrast to {insert} the attr Preventing a plugin's output from being cached - + +caching = true; @@ -1997,11 +2093,14 @@ if (!$smarty->is_cached('index.tpl')) { } $smarty->display('index.tpl'); +?> index.tpl: -Time Remaining: {remain endtime=$obj->endtime} +Time Remaining: {remain endtime=$obj->endtime} +]]> + The number of seconds till the endtime of $obj is reached changes on each display of the page, even if the page is cached. Since the endtime attribute is cached the object only has to be pulled from the database when page is written to the cache but not on subsequent requests of the page. @@ -2010,9 +2109,11 @@ The number of seconds till the endtime of $obj is reached changes on each displa Preventing a whole passage of a template from being cached - + +caching = true; @@ -2023,6 +2124,7 @@ function smarty_block_dynamic($param, $content, &$smarty) { $smarty->register_block('dynamic', 'smarty_block_dynamic', false); $smarty->display('index.tpl'); +?> index.tpl: @@ -2035,7 +2137,9 @@ Now is: {"0"|date_format:"%D %H:%M:%S"} ... do other stuff ... -{/dynamic} +{/dynamic} +]]> + @@ -2093,8 +2197,9 @@ When reloading the page you will notice that both dates differ. One is "dynamic" using a registered or assigned object - -<?php + +register_object("foobar",$myobj,null,false); $smarty->assign_by_ref("myobj", $myobj); $smarty->display("index.tpl"); -?> +?> TEMPLATE: @@ -2127,7 +2232,9 @@ TEMPLATE: the output was {$output} {* access our assigned object *} -{$myobj->meth1("foo",$bar)} +{$myobj->meth1("foo",$bar)} +]]> + @@ -2147,21 +2254,24 @@ the output was {$output} using a template prefilter - -<?php + +/U","",$tpl_source); } // register the prefilter $smarty->register_prefilter("remove_dw_comments"); $smarty->display("index.tpl"); -?> +?> {* Smarty template index.tpl *} -<!--# this line will get removed by the prefilter --> + +]]> + @@ -2181,22 +2291,25 @@ $smarty->display("index.tpl"); using a template postfilter - -<?php + +;\n\" ?>;\n".$tpl_source; } // register the postfilter $smarty->register_postfilter("add_header_comment"); $smarty->display("index.tpl"); -?> +?> {* compiled Smarty template index.tpl *} -<!-- Created by Smarty! --> -{* rest of template content... *} + +{* rest of template content... *} +]]> + @@ -2222,8 +2335,9 @@ $smarty->display("index.tpl"); using a template outputfilter - -<?php + +display("index.tpl"); // now any occurrence of an email address in the template output will have // a simple protection against spambots -?> +?> +]]> + @@ -2270,8 +2386,9 @@ $smarty->display("index.tpl"); example using MySQL as a cache source - -<?php + +cache_handler_func = 'mysql_cache_handler'; -$smarty->display('index.tpl'); +$smarty->display('index.tpl'); mysql database is expected in this format: @@ -2309,7 +2426,7 @@ function mysql_cache_handler($action, &$smarty_obj, &$cache_content, $tpl_file=n $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"); + $smarty_obj->_trigger_error_msg("cache_handler: could not connect to database"); return false; } mysql_select_db($db_name); @@ -2319,7 +2436,7 @@ function mysql_cache_handler($action, &$smarty_obj, &$cache_content, $tpl_file=n // 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."); + $smarty_obj->_trigger_error_msg("cache_handler: query failed."); } $row = mysql_fetch_array($results,MYSQL_ASSOC); @@ -2344,7 +2461,7 @@ function mysql_cache_handler($action, &$smarty_obj, &$cache_content, $tpl_file=n '".addslashes($contents)."') "); if(!$results) { - $smarty_obj->_trigger_error_msg("cache_handler: query failed."); + $smarty_obj->_trigger_error_msg("cache_handler: query failed."); } $return = $results; break; @@ -2357,13 +2474,13 @@ function mysql_cache_handler($action, &$smarty_obj, &$cache_content, $tpl_file=n $results = mysql_query("delete from CACHE_PAGES where CacheID='$CacheID'"); } if(!$results) { - $smarty_obj->_trigger_error_msg("cache_handler: query failed."); + $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\""); + $smarty_obj->_trigger_error_msg("cache_handler: unknown action \"$action\""); $return = false; break; } @@ -2372,7 +2489,9 @@ function mysql_cache_handler($action, &$smarty_obj, &$cache_content, $tpl_file=n } -?> +?> +]]> + @@ -2396,15 +2515,19 @@ function mysql_cache_handler($action, &$smarty_obj, &$cache_content, $tpl_file=n using templates from $template_dir - -// from PHP script + +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 *} +{include file="file:index.tpl"} {* same as one above *} +]]> + @@ -2416,13 +2539,17 @@ $smarty->display("file:admin/menu.tpl"); // same as one above using templates from any directory - -// from PHP script + +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"} +{include file="file:/usr/local/share/templates/navigation.tpl"} +]]> + @@ -2435,13 +2562,17 @@ $smarty->display("file:/path/to/my/templates/menu.tpl"); using templates from windows file paths - -// from PHP script + +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"} +]]> + @@ -2471,9 +2602,9 @@ $smarty->display("file:F:/path/to/my/templates/menu.tpl"); using custom resources - -// from PHP script - + +register_resource("db", array("db_get_template", // using resource from php script $smarty->display("db:index.tpl"); +?> {* using resource from within Smarty template *} -{include file="db:/extras/navigation.tpl"} +{include file="db:/extras/navigation.tpl"} +]]> + @@ -2541,8 +2675,9 @@ $smarty->display("db:index.tpl"); using the default template handler function - -<?php + +default_template_handler_func = 'make_template'; -?> +?> +]]> + @@ -2734,8 +2871,9 @@ require_once $smarty->_get_plugin_filepath('function', 'html_options'); function plugin with output - -<?php + + +?> +]]> + @@ -2765,12 +2905,14 @@ function smarty_function_eightball($params, &$smarty) Question: Will we ever have time travel? -Answer: {eightball}. +Answer: {eightball}. + function plugin without output - -<?php + +assign($params['var'], $params['value']); } -?> +?> +]]> + @@ -2832,8 +2976,9 @@ function smarty_function_assign($params, &$smarty) This plugin basically aliases one of the built-in PHP functions. It does not have any additional parameters. - -<?php + + +?> +]]> + more complex modifier plugin - -<?php + + +?> +]]> + @@ -2954,8 +3104,9 @@ function smarty_modifier_truncate($string, $length = 80, $etc = '...', block function - -<?php + + +} +?> +]]> + @@ -3004,8 +3158,9 @@ function smarty_block_translate($params, $content, &$smarty) simple compiler function - -<?php + +_current_file . " compiled at " . date('Y-m-d H:M'). "';"; } -?> +?> +]]> + This function can be called from the template as: {* this function gets executed at compile time only *} -{tplheader} +{tplheader} + The resulting PHP code in the compiled template would be something like this: - -<php + + +?> +]]> + @@ -3075,8 +3236,9 @@ echo 'index.tpl compiled at 2002-02-20 20:02'; prefilter plugin - -<?php + +]+>!e', 'strtolower("$1")', $source); } -?> +?> +]]> + postfilter plugin - -<?php + +\nget_template_vars()); ?>\n" . $compiled; return $compiled; } -?> +?> +]]> + @@ -3135,7 +3302,9 @@ echo 'index.tpl compiled at 2002-02-20 20:02'; output filter plugin - + + +]]> @@ -3234,8 +3405,9 @@ echo 'index.tpl compiled at 2002-02-20 20:02'; resource plugin - -<?php + + +?> +]]> + @@ -3314,8 +3488,9 @@ function smarty_resource_db_trusted($tpl_name, &$smarty) insert plugin - -<?php + + +?> +]]> +