Re: [SMARTY-GENERAL] smarty templates caching.

From: Date: Fri, 05 Apr 2002 08:52:56 +0000
Subject: Re: [SMARTY-GENERAL] smarty templates caching.
References: 1  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-3504@lists.php.net to get a copy of this message
At 18:30 04/04/02 -0500, Elena Fraboschi wrote:
P.S. I would appreciate seeing same practical samples of the use of fetch. The docs give an example that is a bit too little. Or who knows, I may try to explain later what I have in mind, to see whether fetch might be the route to take for that.
Elena, What follow is not an official explanation, not even a good technical one, its just the way I understand and use it…. When you include a template, you do exactly what you can do with PHP include statement, one file is copied and pasted into the other. That means that if there is a variable or a calculation in the included file, no processing is done, its simply a copy and paste. Fetch is rather different, its stimulates a call of the file by the web browser, so when you fetch a template file that contains a variable this variable is evaluated and what is output is the result of the variable replacement (or the result of the calculation if any). Fetch is (among other uses) used for caching. When you want to cache something you don't want to cache a file that contains variables to be replaced by real value, but you want to cache a file that contains the real value. So you use Fetch to call the template, this template is then parsed, variable replacement is done and the generated output is cached. A simple example, il you write in a template : {$Smarty.now|date_format:"%d/%m/%Y"} When you do an include the resulting file (the result that combines the two templates) will contain : {$Smarty.now|date_format:"%d/%m/%Y"} If you do a Fetch, the sentence "{$Smarty.now|date_format:"%d/%m/%Y"}" will be evaluated (exactly as if the template had been called by the browser and parsed by the PHP parser) and replaced by the actual date. If you want to place a template file called "mytemplate.tpl" inside another template you just need to write in the main template {include file="file:/home/foo/www/templates/mytemplate.tpl"} or {include file="mytemplate.tpl"} This is different if you want to cache the result, because the content of mytemplate.tpl must be evaluated before been cached (what is cached is the resulting HTML content) So you will do : $smarty->caching = true;
        if(!$smarty->is_cached('file:/home/foo/www/templates/mytemplate.tpl', $REQUEST_URI)) {
        $smarty->assign('center',$smarty->fetch('file:/home/foo/www/templates/mytemplate.tpl', $REQUEST_URI));
$smarty->caching = false; and write in the main template {$center} Hope this helps Luc

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