RE: [SMARTY] (Fwd) Re: [SMARTY] timer on script
| From: | Internet Racer | Date: | Sun, 07 Jul 2002 11:33:39 +0000 |
| Subject: | RE: [SMARTY] (Fwd) Re: [SMARTY] timer on script | ||
| References: | 1 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-5214@lists.php.net to get a copy of this message | ||
Okay sorry I wasn't thinking. I was trying to time my whole script but what
I ended up with was just timing the template. I scrapped it and did this
instead.
index.php
======================================================
/* start time of script */
$timer_start = explode(' ', microtime());
$timer_start = $timer_start[1] + $timer_start[0];
$smarty = new Smarty;
$smarty->assign("timer_start", $timer_start);
$smarty->display('index.tpl');
======================================================
footer.tpl
======================================================
{php}
/* end time of script */
$timer_end = explode(' ', microtime());
$timer_end = $timer_end[1] + $timer_end[0];
printf ("%.4f sec", $timer_end - $timer_start);
{/php}
======================================================
-----Original Message-----
From: Ferdinand Beyer [mailto:fbeyer@clickhand.de]
Sent: July 7, 2002 5:56 AM
To: smarty-general@lists.php.net
Subject: [SMARTY] (Fwd) Re: [SMARTY] timer on script
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>
--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php