(Fwd) Re: [SMARTY] timer on script

From: Date: Sun, 07 Jul 2002 09:55:57 +0000
Subject: (Fwd) Re: [SMARTY] timer on script
Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-5212@lists.php.net to get a copy of this message
On 6 Jul 2002 at 22:09, Internet Racer wrote: > I have the following template file and want to pass the $timer variable > through to footer.tpl so I can use it. How can I do this? Yes I know in > debug mode I get the execution time but I want to just show the time at the > bottom of the page. Any suggestions would be great. Simply use custom a smarty function for this. Example: Smarty code: ======================================= {timer action="start"} ... Smarty code here .... {timer action="stop" assign="time"} {include file="footer.tpl" time=$time} ======================================= Save this code into a file called 'function.timer.php' in your plugin directory: ======================================= function smarty_function_timer($params, &$smarty) { static $start = -1; extract($params); if (!isset($action) || (($action != 'start) && ($action != 'stop'))) { $smarty->trigger_error("timer: missing attribute 'action'."); return false; } $mtime = explode(' ', microtime()); $time = $mtime[1] + $mtime[0]; if ($action == 'start') { $start = $time; return true; } if ($start < 0) { $smarty->trigger_error("timer: 'stop' without 'start'"); return false; } $time = $start - $time; $start = -1; if (!empty($assign)) { $smarty->assign($assign, $time); } else { echo $time; } return true; } ======================================= Now you can start the timer with {timer action="start"} and stop it with {timer action="stop"}. If you pass the 'assign' parameter to {timer action="stop"}, the time will be saved to a smarty variable; otherwise {timer action="stop"} directly outputs the time. Note: I have not tested the code above -- feel free to modify it! -- Ferdinand Beyer <fbeyer@clickhand.de>-- Ferdinand Beyer <fbeyer@clickhand.de>

« previous php.smarty.general (#5212) next »