How to only cache part of a page using Smarty Template (PHP)?

How do I cache everything on a Smarty template page except for a small portion of the content (which is really dynamic)?

+2


source to share


4 answers


Building view on Zed's answer - if your "dynamic" content has a finite number of permutations, build separate templates for those permutations and fetch()

them based on some variable. Something like:

<html>
<body>
<p>Common content would go here with other Smarty {$variables}.</p>

<p>You could then fetch other content using
{if $var1 eq 'foo'}
    {fetch file='/path/to/foo.tpl'}
{elseif $var1 eq 'bar'}
    {fetch file='/path/to/bar.tpl'}
{/if}
</body>
</html>

      



Unless you really want the included files to be cached, you will need to create some kind of exclusion logic to ensure that these subpatterns are not cached.

However, if you have an undefined number of potential dynamic parameters, you may need to simply create HTML in your PHP code and pass it to Smarty as a view variable.

+1


source


{nocache} is used to disable caching of a template section. Each {nocache} must be paired with the corresponding {/ nocache}.

Example:



<html>    
<body>

<p>Some code, it will be cached</p>
{nocache}
<p>It won't be cached</p>
{/nocache}

</body>
</html>

      

+1


source


You can put the caching side in a separate template and fetch its content with fetch () from another template.

0


source


You can use Smarty insert function.

http://www.smarty.net/manual/en/language.function.insert.php

You create a function that gets called and inserts dynamic content into the page. Useful for shopping carts, for example.

0


source







All Articles