Re: different template paths
| From: | Ralph Meijer | Date: | Fri, 20 Jul 2001 10:58:30 +0000 |
| Subject: | Re: different template paths | ||
| References: | 1 2 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-1224@lists.php.net to get a copy of this message | ||
Ok, I've been playing around a bit and patched Smarty.class.php to do what
I want. I created a method which alters the resource given to {include} or
fetch() (which covers display() as well). This ensures that templates
get compiled only once and caching still works (not tested, but should work):
---8<--- cut here ---8<---
--- Smarty.class.php.orig Fri Jul 20 12:43:24 2001
+++ Smarty.class.php Fri Jul 20 12:23:49 2001
@@ -497,6 +497,8 @@
{
global $HTTP_SERVER_VARS, $QUERY_STRING, $HTTP_COOKIE_VARS;
+ $tpl_file = $this->rewrite_resource($tpl_file);
+
$this->_inclusion_depth = 0;
$this->_included_tpls = array();
@@ -793,6 +795,8 @@
\*======================================================================*/
function _smarty_include($_smarty_include_tpl_file, $_smarty_include_vars)
{
+ $_smarty_include_tpl_file = $this->rewrite_resource($_smarty_include_tpl_file);
+
$this->_included_tpls[] = array('template' => $_smarty_include_tpl_file,
'depth' => ++$this->_inclusion_depth);
@@ -1000,6 +1004,15 @@
function _trigger_error_msg($error_msg, $error_type = E_USER_WARNING)
{
trigger_error("Smarty error: $error_msg", $error_type);
+ }
+
+/*======================================================================*\
+ Function: rewrite_resource
+ Purpose: rewrite resource identifier to do magic
+\*======================================================================*/
+ function rewrite_resource($resource)
+ {
+ return $resource;
}
}
---8<--- cut here ---8<---
And here my usage of this method (it is not complete because I don't
check if a resource type different from 'file' is used, but you get
the idea):
---8<--- cut here ---8<---
class MySmarty extends Smarty {
function rewrite_resource($resource) {
global $skin;
$search_path = array(
$this->template_dir."/$skin",
$this->template_dir."/default"
);
if (substr($file, 0, 1) != "/") {
reset($search_path);
while (list(,$v) = each($search_path)) {
$filename = $v.'/'.$resource;
if (file_exists($filename) && is_readable($filename)) {
$resource = $filename;
break;
}
}
}
# strip $this->template_dir
if (strpos($resource, $this->template_dir) === 0) {
$resource = substr($resource, strlen($this->template_dir) + 1);
}
return $resource;
}
};
---8<--- cut here ---8<---
Greetz,
Ralphm