| 
									
										
										
										
											2004-04-13 11:47:32 +00:00
										 |  |  | <?xml version="1.0" encoding="iso-8859-1"?> | 
					
						
							|  |  |  | <!-- $Revision$ --> | 
					
						
							|  |  |  |    <sect1 id="caching.multiple.caches"> | 
					
						
							|  |  |  |     <title>Multiple Caches Per Page</title> | 
					
						
							| 
									
										
										
										
											2004-04-13 12:14:17 +00:00
										 |  |  |     <para> | 
					
						
							| 
									
										
										
										
											2005-05-24 14:01:49 +00:00
										 |  |  |      You can have multiple cache files for a single call to | 
					
						
							|  |  |  |      <link linkend="api.display">display()</link> | 
					
						
							|  |  |  |      or <link linkend="api.fetch">fetch()</link>. | 
					
						
							|  |  |  |      Let's say that a call to display('index.tpl') may have several | 
					
						
							| 
									
										
										
										
											2004-04-13 12:14:17 +00:00
										 |  |  |      different output contents depending on some condition, and you want | 
					
						
							|  |  |  |      separate caches for each one. You can do this by passing a cache_id as the | 
					
						
							|  |  |  |      second parameter to the function call. | 
					
						
							|  |  |  |     </para> | 
					
						
							| 
									
										
										
										
											2004-04-13 11:47:32 +00:00
										 |  |  |     <example> | 
					
						
							|  |  |  |      <title>passing a cache_id to display()</title> | 
					
						
							|  |  |  |      <programlisting role="php"> | 
					
						
							|  |  |  | <![CDATA[ | 
					
						
							|  |  |  | <?php | 
					
						
							|  |  |  | require('Smarty.class.php'); | 
					
						
							|  |  |  | $smarty = new Smarty; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $smarty->caching = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $my_cache_id = $_GET['article_id']; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $smarty->display('index.tpl',$my_cache_id); | 
					
						
							|  |  |  | ?> | 
					
						
							|  |  |  | ]]> | 
					
						
							| 
									
										
										
										
											2004-04-13 12:14:17 +00:00
										 |  |  |      </programlisting> | 
					
						
							| 
									
										
										
										
											2004-04-13 11:47:32 +00:00
										 |  |  |     </example> | 
					
						
							| 
									
										
										
										
											2004-04-13 12:14:17 +00:00
										 |  |  |     <para> | 
					
						
							| 
									
										
										
										
											2005-05-24 14:01:49 +00:00
										 |  |  |      Above, we are passing the variable $my_cache_id to | 
					
						
							|  |  |  |      <link linkend="api.display">display()</link> | 
					
						
							|  |  |  |      as the | 
					
						
							| 
									
										
										
										
											2004-04-13 12:14:17 +00:00
										 |  |  |      cache_id. For each unique value of $my_cache_id, a separate cache will be | 
					
						
							|  |  |  |      generated for index.tpl. In this example, "article_id" was passed in the | 
					
						
							|  |  |  |      URL and is used as the cache_id. | 
					
						
							|  |  |  |     </para> | 
					
						
							|  |  |  |     <note> | 
					
						
							|  |  |  |      <title>Technical Note</title> | 
					
						
							|  |  |  |      <para> | 
					
						
							|  |  |  |       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 | 
					
						
							|  |  |  |       cache_id 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_ids 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 | 
					
						
							|  |  |  |       10 characters and is made up of alpha-numerics only, and must be a valid | 
					
						
							|  |  |  |       article_id in the database. Check for this! | 
					
						
							|  |  |  |      </para> | 
					
						
							|  |  |  |     </note> | 
					
						
							|  |  |  |     <para> | 
					
						
							|  |  |  |      Be sure to pass the same cache_id as the | 
					
						
							|  |  |  |      second parameter to <link linkend="api.is.cached">is_cached()</link> and | 
					
						
							|  |  |  |      <link linkend="api.clear.cache">clear_cache()</link>. | 
					
						
							|  |  |  |     </para> | 
					
						
							|  |  |  |     <example> | 
					
						
							| 
									
										
										
										
											2004-04-13 11:47:32 +00:00
										 |  |  |      <title>passing a cache_id to is_cached()</title> | 
					
						
							|  |  |  |      <programlisting role="php"> | 
					
						
							|  |  |  | <![CDATA[ | 
					
						
							|  |  |  | <?php | 
					
						
							|  |  |  | require('Smarty.class.php'); | 
					
						
							|  |  |  | $smarty = new Smarty; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $smarty->caching = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $my_cache_id = $_GET['article_id']; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if(!$smarty->is_cached('index.tpl',$my_cache_id)) { | 
					
						
							| 
									
										
										
										
											2004-04-13 12:14:17 +00:00
										 |  |  |     // No cache available, do variable assignments here. | 
					
						
							|  |  |  |     $contents = get_database_contents(); | 
					
						
							|  |  |  |     $smarty->assign($contents); | 
					
						
							| 
									
										
										
										
											2004-04-13 11:47:32 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $smarty->display('index.tpl',$my_cache_id); | 
					
						
							|  |  |  | ?> | 
					
						
							|  |  |  | ]]> | 
					
						
							| 
									
										
										
										
											2004-04-13 12:14:17 +00:00
										 |  |  |      </programlisting> | 
					
						
							| 
									
										
										
										
											2004-04-13 11:47:32 +00:00
										 |  |  |     </example> | 
					
						
							| 
									
										
										
										
											2004-04-13 12:14:17 +00:00
										 |  |  |     <para> | 
					
						
							|  |  |  |      You can clear all caches for a particular cache_id by passing null as the | 
					
						
							| 
									
										
										
										
											2005-05-24 14:01:49 +00:00
										 |  |  |      first parameter to | 
					
						
							|  |  |  |      <link linkend="api.clear.cache">clear_cache()</link>. | 
					
						
							| 
									
										
										
										
											2004-04-13 12:14:17 +00:00
										 |  |  |     </para> | 
					
						
							|  |  |  |     <example> | 
					
						
							| 
									
										
										
										
											2004-04-13 11:47:32 +00:00
										 |  |  |      <title>clearing all caches for a particular cache_id</title> | 
					
						
							|  |  |  |      <programlisting role="php"> | 
					
						
							|  |  |  | <![CDATA[ | 
					
						
							|  |  |  | <?php | 
					
						
							|  |  |  | require('Smarty.class.php'); | 
					
						
							|  |  |  | $smarty = new Smarty; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $smarty->caching = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // clear all caches with "sports" as the cache_id | 
					
						
							|  |  |  | $smarty->clear_cache(null,"sports"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $smarty->display('index.tpl',"sports"); | 
					
						
							|  |  |  | ?> | 
					
						
							|  |  |  | ]]> | 
					
						
							| 
									
										
										
										
											2004-04-13 12:14:17 +00:00
										 |  |  |      </programlisting> | 
					
						
							| 
									
										
										
										
											2004-04-13 11:47:32 +00:00
										 |  |  |     </example> | 
					
						
							| 
									
										
										
										
											2004-04-13 12:14:17 +00:00
										 |  |  |     <para> | 
					
						
							|  |  |  |      In this manner, you can "group" your caches together by giving them the | 
					
						
							|  |  |  |      same cache_id. | 
					
						
							|  |  |  |     </para> | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |    </sect1> | 
					
						
							|  |  |  |     | 
					
						
							| 
									
										
										
										
											2004-04-13 11:47:32 +00:00
										 |  |  | <!-- Keep this comment at the end of the file
 | 
					
						
							|  |  |  | Local variables: | 
					
						
							|  |  |  | mode: sgml | 
					
						
							|  |  |  | sgml-omittag:t | 
					
						
							|  |  |  | sgml-shorttag:t | 
					
						
							|  |  |  | sgml-minimize-attributes:nil | 
					
						
							|  |  |  | sgml-always-quote-attributes:t | 
					
						
							|  |  |  | sgml-indent-step:1 | 
					
						
							|  |  |  | sgml-indent-data:t | 
					
						
							|  |  |  | indent-tabs-mode:nil | 
					
						
							|  |  |  | sgml-parent-document:nil | 
					
						
							|  |  |  | sgml-default-dtd-file:"../../../../manual.ced" | 
					
						
							|  |  |  | sgml-exposed-tags:nil | 
					
						
							|  |  |  | sgml-local-catalogs:nil | 
					
						
							|  |  |  | sgml-local-ecat-files:nil | 
					
						
							|  |  |  | End: | 
					
						
							|  |  |  | vim600: syn=xml fen fdm=syntax fdl=2 si | 
					
						
							|  |  |  | vim: et tw=78 syn=sgml | 
					
						
							|  |  |  | vi: ts=1 sw=1 | 
					
						
							| 
									
										
										
										
											2004-04-13 12:14:17 +00:00
										 |  |  | --> |