Virtual includes
| From: | Sven Axelsson | Date: | Thu, 02 May 2002 12:29:41 +0000 |
| Subject: | Virtual includes | ||
| Groups: | php.smarty.general | ||
| Request: | Send a blank email to php-smarty-general+get-3980@lists.php.net to get a copy of this message | ||
Virtual paths would no doubt be nice to have when including
templates and other files. I hacked the following simple resource
plugin to use until it is implemented in the Smarty core :^)
Use for instance like this: {include
file="virt:/server-folder/file.php"}
file: resource.virt.php
<?php
function smarty_resource_virt_source($tpl_name, &$tpl_source, &$smarty)
{
$file = map_path($tpl_name);
if (is_file($file))
{
$tpl_source = $smarty->_read_file($file);
return true;
}
return false;
}
function smarty_resource_virt_timestamp($tpl_name, &$tpl_timestamp,
&$smarty)
{
$file = map_path($tpl_name);
if (is_file($file))
{
$tpl_timestamp = filemtime($file);
return true;
}
return false;
}
function smarty_resource_virt_secure($tpl_name, &$smarty)
{
return true;
}
function smarty_resource_virt_trusted($tpl_name, &$smarty)
{
return true;
}
function map_path($path)
{
if (false) // Enable check for Apache module here
{
$apache = apache_lookup_uri($path);
return $apache['filename'];
}
else
{
// Only works with root-based paths.
// Insert better code here...
return realpath($_SERVER['DOCUMENT_ROOT'] . $path);
}
}
?>
/sven axelsson, bok & webb i göteborg ab