Menu

#1 Smarty Cache Problem w/alternative template sources

open
nobody
None
5
2004-09-08
2004-09-08
Anonymous
No

email: rich@corticaldesignDOTcom

Scenario:

Smarty has been set up to get its template from a
database source, using a slight adaptation of the
resource plugin from the help manual (PDF version -
page 152) - see adapted source below.

Problem:

Smarty continues caching files using a database-
sourced template despite the fact that the source has
changed in the Database and caching is disabled (using
$smarty->caching=0).

Filename in the templates_c directory:
%%1C^1CA^1CAA6A32%%db%3Ahome.php

When this file is manually deleted from the directory,
Smarty correctly re-processes.

Based on the fact that my resource plugin is echoing my
SQL string for grabbing the template (see source), what
appears to be happening is that calls to
smarty_resource_db_source() only appears to happen
once - when the cache is deleted. Calls to this function
are not made for subsequent refreshes of the page.

$smarty->caching=0 works properly when changing back
to using templates in the file system in the standard
documented fashion.

Smarty Source
------------------------------------------------------
$smarty = new Smarty();
$smarty->caching = 0;
$smarty->register_resource("db", array
("smarty_resource_db_source", "smarty_resource_db_tim
estamp", "smarty_resource_db_secure", "smarty_resourc
e_db_trusted"));
$smarty->template_dir = $smartyTemplateDir;
$smarty->compile_dir = $smartyCompileDir;
$smarty->config_dir = $smartyConfigDir;
$smarty->cache_dir = $smartyCacheDir;
$smarty->register_object("menu", $Menu);
$smarty->register_object("user", $UserManager);
echo "cache check: " . $smarty->is_cached
("db:home") . ".";
$smarty->clear_all_cache();
$smarty->display('db:home');

Smarty Plugin Source
------------------------------------------------------
<?php
/*
* Smarty plugin
* ----------------------------------------------------
---------
* File: resource.db.php
* Type: resource
* Name: db
* Purpose: Fetches templates from a database
* ----------------------------------------------------
---------
*/
function smarty_resource_db_source($tpl_name,
&$tpl_source, &$smarty)
{
// do database call here to fetch your
template,
// populating $tpl_source
global $myDb;
echo $sqlString = "SELECT source FROM
site_templates WHERE name='$tpl_name'";
$myDb->readx($sqlString);
//echo $myDb->resultcount();

if ($myDb->resultcount())
{
$tpl_source = $myDb->data[0]
['source'];
return true;
}
else
{
return false;
}
}

function smarty_resource_db_timestamp($tpl_name,
&$tpl_timestamp, &$smarty)
{
// do database call here to populate
$tpl_timestamp.
global $myDb;
$myDb->readx("SELECT timestamp FROM
site_templates WHERE name='$tpl_name'");
if ($myDb->resultcount())
{
$tpl_timestamp = $myDb->Data[0]
['timestamp'];
return true;
}
else
{
return false;
}
}

function smarty_resource_db_secure($tpl_name,
&$smarty)
{
// assume all templates are secure
return true;
}

function smarty_resource_db_trusted($tpl_name,
&$smarty)
{
// not used for templates
}

?>

Discussion