<HTML ><HEAD ><TITLE >Multiple Caches Per Page</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="Smarty Manual" HREF="index.html"><LINK REL="UP" TITLE="Caching" HREF="caching.html"><LINK REL="PREVIOUS" TITLE="Caching" HREF="caching.html"><LINK REL="NEXT" TITLE="Cache Groups" HREF="caching.groups.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="sect1" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >Smarty Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="caching.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Chapter 14. Caching</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="caching.groups.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="caching.multiple.caches" ></A >Multiple Caches Per Page</H1 ><P > You can have multiple cache files for a single call to <A HREF="api.display.html" ><TT CLASS="varname" >display()</TT ></A > or <A HREF="api.fetch.html" ><TT CLASS="varname" >fetch()</TT ></A >. Let's say that a call to <TT CLASS="literal" >display('index.tpl')</TT > may have several different output contents depending on some condition, and you want separate caches for each one. You can do this by passing a <TT CLASS="parameter" ><I >$cache_id</I ></TT > as the second parameter to the function call. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN6758" ></A ><P ><B >Example 14-6. Passing a $cache_id to display()</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">require(</span><span style="color: #DD0000">'Smarty.class.php'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$smarty </span><span style="color: #007700">= new </span><span style="color: #0000BB">Smarty</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">caching </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$my_cache_id </span><span style="color: #007700">= </span><span style="color: #0000BB">$_GET</span><span style="color: #007700">[</span><span style="color: #DD0000">'article_id'</span><span style="color: #007700">];<br /><br /></span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">display</span><span style="color: #007700">(</span><span style="color: #DD0000">'index.tpl'</span><span style="color: #007700">, </span><span style="color: #0000BB">$my_cache_id</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P > Above, we are passing the variable <TT CLASS="literal" >$my_cache_id</TT > to <A HREF="api.display.html" ><TT CLASS="varname" >display()</TT ></A > as the <TT CLASS="parameter" ><I >$cache_id</I ></TT >. For each unique value of <TT CLASS="literal" >$my_cache_id</TT >, a separate cache will be generated for <TT CLASS="filename" >index.tpl</TT >. In this example, <TT CLASS="literal" >article_id</TT > was passed in the URL and is used as the <TT CLASS="parameter" ><I >$cache_id</I ></TT >. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Technical Note: </B > Be very cautious when passing values from a client (web browser) into Smarty or any PHP application. Although the above example of using the article_id from the URL looks handy, it could have bad consequences. The <TT CLASS="parameter" ><I >$cache_id</I ></TT > is used to create a directory on the file system, so if the user decided to pass an extremely large value for article_id, or write a script that sends random article_id's at a rapid pace, this could possibly cause problems at the server level. Be sure to sanitize any data passed in before using it. In this instance, maybe you know the article_id has a length of ten characters and is made up of alpha-numerics only, and must be a valid article_id in the database. Check for this! </P ></BLOCKQUOTE ></DIV ><P > Be sure to pass the same <TT CLASS="parameter" ><I >$cache_id</I ></TT > as the second parameter to <A HREF="api.is.cached.html" ><TT CLASS="varname" >is_cached()</TT ></A > and <A HREF="api.clear.cache.html" ><TT CLASS="varname" >clear_cache()</TT ></A >. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN6780" ></A ><P ><B >Example 14-7. Passing a cache_id to is_cached()</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">require(</span><span style="color: #DD0000">'Smarty.class.php'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$smarty </span><span style="color: #007700">= new </span><span style="color: #0000BB">Smarty</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">caching </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$my_cache_id </span><span style="color: #007700">= </span><span style="color: #0000BB">$_GET</span><span style="color: #007700">[</span><span style="color: #DD0000">'article_id'</span><span style="color: #007700">];<br /><br />if(!</span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">is_cached</span><span style="color: #007700">(</span><span style="color: #DD0000">'index.tpl'</span><span style="color: #007700">,</span><span style="color: #0000BB">$my_cache_id</span><span style="color: #007700">)) {<br /> </span><span style="color: #FF8000">// No cache available, do variable assignments here.<br /> </span><span style="color: #0000BB">$contents </span><span style="color: #007700">= </span><span style="color: #0000BB">get_database_contents</span><span style="color: #007700">();<br /> </span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">assign</span><span style="color: #007700">(</span><span style="color: #0000BB">$contents</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">display</span><span style="color: #007700">(</span><span style="color: #DD0000">'index.tpl'</span><span style="color: #007700">,</span><span style="color: #0000BB">$my_cache_id</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P > You can clear all caches for a particular <TT CLASS="parameter" ><I >$cache_id</I ></TT > by passing <TT CLASS="constant" >NULL</TT > as the first parameter to <A HREF="api.clear.cache.html" ><TT CLASS="varname" >clear_cache()</TT ></A >. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN6788" ></A ><P ><B >Example 14-8. Clearing all caches for a particular $cache_id</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">require(</span><span style="color: #DD0000">'Smarty.class.php'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$smarty </span><span style="color: #007700">= new </span><span style="color: #0000BB">Smarty</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">caching </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// clear all caches with "sports" as the $cache_id<br /></span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">clear_cache</span><span style="color: #007700">(</span><span style="color: #0000BB">null</span><span style="color: #007700">,</span><span style="color: #DD0000">'sports'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">display</span><span style="color: #007700">(</span><span style="color: #DD0000">'index.tpl'</span><span style="color: #007700">,</span><span style="color: #DD0000">'sports'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P > In this manner, you can <SPAN CLASS="QUOTE" >"group"</SPAN > your caches together by giving them the same <TT CLASS="parameter" ><I >$cache_id</I ></TT >. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="caching.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="caching.groups.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Caching</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="caching.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Cache Groups</TD ></TR ></TABLE ></DIV ></BODY ></HTML >