Re: [SMARTY] Cache and insert
| From: | Ferdinand Beyer | Date: | Mon, 27 Jan 2003 18:08:32 +0000 |
| Subject: | Re: [SMARTY] Cache and insert | ||
| References: | 1 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-7346@lists.php.net to get a copy of this message | ||
On 26 Jan 2003 at 22:03, Nino Katic wrote:
> I'm not sure if I have quite understood insert tag, but this is what I have:
>
> In categories.tpl I have something like this:
>
> {if $show == true}
> {foreach from=$al_name item=al_item}
> <p>
> {$al_item} - {cycle name=autor_name values=$user} {cycle
> name=autor_pname values=$user2} Some number: {cycle name=pic values=$nmr}
> </p>
> {/foreach}
> {/if}
>
> In my index.php I'm calling a function prepareData($id). That function looks
> like this in some sort of pseudocode:
>
> function prepareData($id)
> {
> $query = "select .... ";
> // get data from DB and asign it to arrays. assign arrays to smarty.
>
> if $number_rows == 0 then $show becomes false
> else
> {
> assign data to arrays
> assign arrays to smarty
> $show = true
> }
> }
>
> Now, I want to use cache, so I suppose I should put my function in plugin
> class, name it like insert_prepareDate and call it from categories.tpl. The
> problem is that now I have to make my output coded in a function, or I have
> understood it wrong? If this is rigth then all talk about layout and code
> separation loose it sense?
>
> Nino
>
>
>
> --
> Smarty General Mailing List (http://smarty.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
Hi Nino,
I'm not sure if I understood you right. If you just want to use caching,
you do not have to change anything at all. Simply set $smarty-
>caching = true and your templates will be cached.
Caching means that the output of your template _after_
prepareData() has run and _after_ the arrays are layouted nicely by
your template is saved and loaded next time instead of running the
template again.
So if the template is cached, there is no need to call prepareData()
since the data will not be processed. That's why you should test this
before calling prepareData():
if (!$smarty->is_cached('your_template.tpl'`)) {
prepareData();
}
$smarty->display('your_template.tpl');
I guess you want to use the same template for different data
specified by the $id in prepareData($id), don't you? If yes, I
recommend you to use cache ids and to include $id in the cache id
to make sure that different ids will be cached separately.
Inserts are smarty plugin functions that are _never_ cached. So you
only need them in special situations, eg. if you want to add the
current time on your page. In your situation, you don't need an insert
tag since you want to cache the result of prepareData(). If not,
please don't enable caching for this template!
You were talking about plugin classes - Smarty doesn't use classes
but functions for plugins. Maybe you would like to read the
documentation pages at http://smarty.php.net for plugins,
caching
(including cache ids) and insert tags.
Hope that helps!
--
Ferdinand Beyer
<fbeyer@clickhand.de>