Get Smarty

Donate

Donate Bitcoin Bitcoin
Paypal

Smarty Icon

You may use the Smarty logo according to the trademark notice.

Smarty Template Engine Smarty Template Engine

For sponsorship, advertising, news or other inquiries, contact us at:

Sites Using Smarty

Check reviews of PHP Web Hosting, compatible with Smarty Templates

Largest classified with thousands of ads at Shoppok

Buy cheap eyeglasses from Cheapglasses123.com and save up to 80%.

Buy wedding dresses on SiteSteer.com - Best Online Wedding Dress Store

Candy Crush Cheats

Tired of playing games? Visit the Game Supply to Sell World of Warcraft Accounts.

Buy prescription glasses from www.australiaglasses.com and save.

Cheap Glasses Now On Sale at GlassesPeople.com. Starts At $7.95.

Where to buy discount wedding dresses and cheap smart dresses free shipping - Weddingdresstrend.com

Multiple Caches Per Page

You can have multiple cache files for a single call to display() or fetch(). Let's say that a call to display('index.tpl') 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 $cache_id as the second parameter to the function call.

Example 14.6. Passing a $cache_id to display()


<?php
require('Smarty.class.php');
$smarty = new Smarty;

$smarty->caching = 1;

$my_cache_id = $_GET['article_id'];

$smarty->display('index.tpl', $my_cache_id);
?>

     

Above, we are passing the variable $my_cache_id to display() as the $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.

Technical Note

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_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!

Be sure to pass the same $cache_id as the second parameter to is_cached() and clear_cache().

Example 14.7. Passing a cache_id to is_cached()


<?php
require('Smarty.class.php');
$smarty = new Smarty;

$smarty->caching = 1;

$my_cache_id = $_GET['article_id'];

if(!$smarty->is_cached('index.tpl',$my_cache_id)) {
    // No cache available, do variable assignments here.
    $contents = get_database_contents();
    $smarty->assign($contents);
}

$smarty->display('index.tpl',$my_cache_id);
?>

     

You can clear all caches for a particular $cache_id by passing NULL as the first parameter to clear_cache().

Example 14.8. Clearing all caches for a particular $cache_id


<?php
require('Smarty.class.php');
$smarty = new Smarty;

$smarty->caching = 1;

// clear all caches with "sports" as the $cache_id
$smarty->clear_cache(null,'sports');

$smarty->display('index.tpl','sports');
?>

     

In this manner, you can group your caches together by giving them the same $cache_id.

Comments
No comments for this page.
Post a Comment
All comments are moderated. Support questions are ignored, use the forums instead.
Author:
Email: (not shown)
What is 3 plus 0? (Are you human?)

Sponsors [info]

UK Web Hosting @webhost.uk.net
Best Web Hosting @rshosting.com
Web Hosting UK @webhostinguk.com
Unlimited Web Hosting @infrenion.com
App Entwicklung @morphodo.com
Buy from China @dhgate.com
dissertationteam.com
Buy Web Hosting @buy-hosting.net
WordPress Hosting Hub @wphostinghub.net