mirror of
https://github.com/smarty-php/smarty.git
synced 2025-08-07 11:54:26 +02:00
More cache work.
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
<?
|
<?
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Project: Smarty: the PHP compiled template engine
|
* Project: Smarty: the PHP compiled template engine
|
||||||
* File: Smarty.addons.php
|
* File: Smarty.addons.php
|
||||||
@@ -37,6 +36,23 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
/*============================================*\
|
||||||
|
Inserts handler
|
||||||
|
\*============================================*/
|
||||||
|
|
||||||
|
function _smarty_insert_handler($args, $caching, $delimiter)
|
||||||
|
{
|
||||||
|
if ($caching) {
|
||||||
|
$arg_string = serialize($args);
|
||||||
|
return "$delimiter{insert_cache $arg_string}$delimiter";
|
||||||
|
} else {
|
||||||
|
$function_name = 'insert_'.$args['name'];
|
||||||
|
return $function_name($args);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*============================================*\
|
/*============================================*\
|
||||||
Modifiers
|
Modifiers
|
||||||
\*============================================*/
|
\*============================================*/
|
||||||
|
132
Smarty.class.php
132
Smarty.class.php
@@ -168,20 +168,20 @@ class Smarty
|
|||||||
|
|
||||||
|
|
||||||
/*======================================================================*\
|
/*======================================================================*\
|
||||||
Function: register_func
|
Function: register_function
|
||||||
Purpose: Registers custom function to be used in templates
|
Purpose: Registers custom function to be used in templates
|
||||||
\*======================================================================*/
|
\*======================================================================*/
|
||||||
function register_func($function, $function_impl)
|
function register_function($function, $function_impl)
|
||||||
{
|
{
|
||||||
$this->custom_funcs[$function] = $function_impl;
|
$this->custom_funcs[$function] = $function_impl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*======================================================================*\
|
/*======================================================================*\
|
||||||
Function: register_mod
|
Function: register_modifier
|
||||||
Purpose: Registers modifier to be used in templates
|
Purpose: Registers modifier to be used in templates
|
||||||
\*======================================================================*/
|
\*======================================================================*/
|
||||||
function register_mod($modifier, $modifier_impl)
|
function register_modifier($modifier, $modifier_impl)
|
||||||
{
|
{
|
||||||
$this->custom_mods[$modifier] = $modifier_impl;
|
$this->custom_mods[$modifier] = $modifier_impl;
|
||||||
}
|
}
|
||||||
@@ -189,13 +189,24 @@ class Smarty
|
|||||||
|
|
||||||
/*======================================================================*\
|
/*======================================================================*\
|
||||||
Function: clear_cache()
|
Function: clear_cache()
|
||||||
Purpose: clear cached content for the given template
|
Purpose: clear cached content for the given template and cache id
|
||||||
\*======================================================================*/
|
\*======================================================================*/
|
||||||
|
|
||||||
function clear_cache($tpl_file)
|
function clear_cache($tpl_file, $cache_id = null)
|
||||||
{
|
{
|
||||||
$cache_tpl_md5 = md5(realpath($this->template_dir.'/'.$tpl_file));
|
$cache_tpl_md5 = md5(realpath($this->template_dir.'/'.$tpl_file));
|
||||||
return $this->_clear_tpl_cache_dir($cache_tpl_md5);
|
$cache_dir = $this->cache_dir.'/'.$cache_tpl_md5;
|
||||||
|
|
||||||
|
if (!is_dir($cache_dir))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (isset($cache_id)) {
|
||||||
|
$cache_id_md5 = md5($cache_id);
|
||||||
|
$cache_id_dir = substr($cache_id_md5, 0, 2);
|
||||||
|
$cache_file = "$cache_dir/$cache_id_dir/{$cache_tpl_md5}_$cache_id_md5.cache";
|
||||||
|
return (bool)(is_file($cache_file) && unlink($cache_file));
|
||||||
|
} else
|
||||||
|
return $this->_clear_tpl_cache_dir($cache_tpl_md5);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -222,18 +233,19 @@ class Smarty
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*======================================================================*\
|
/*======================================================================*\
|
||||||
Function: is_cached()
|
Function: is_cached()
|
||||||
Purpose: test to see if valid cache exists for this template
|
Purpose: test to see if valid cache exists for this template
|
||||||
\*======================================================================*/
|
\*======================================================================*/
|
||||||
|
|
||||||
function is_cached($tpl_file)
|
function is_cached($tpl_file, $cache_id = null)
|
||||||
{
|
{
|
||||||
// cache id = template path + the invoked script
|
// cache name = template path + cache_id
|
||||||
$cache_tpl_md5 = md5(realpath($this->template_dir.'/'.$tpl_file));
|
$cache_tpl_md5 = md5(realpath($this->template_dir.'/'.$tpl_file));
|
||||||
$cache_path_md5 = md5($HTTP_SERVER_VARS['SERVER_NAME'].'/'.$HTTP_SERVER_VARS['PHP_SELF']);
|
$cache_id_md5 = md5($cache_id);
|
||||||
$cache_path_dir = substr($cache_path_md5, 0, 2);
|
$cache_id_dir = substr($cache_id_md5, 0, 2);
|
||||||
$cache_file = $this->cache_dir."/$cache_tpl_md5/$cache_path_dir/$cache_tpl_md5_$cache_path_md5.cache";
|
$cache_file = $this->cache_dir."/$cache_tpl_md5/$cache_id_dir/{$cache_tpl_md5}_$cache_id_md5.cache";
|
||||||
|
|
||||||
if (file_exists($cache_file) &&
|
if (file_exists($cache_file) &&
|
||||||
($this->cache_lifetime == 0 ||
|
($this->cache_lifetime == 0 ||
|
||||||
@@ -271,9 +283,9 @@ class Smarty
|
|||||||
Purpose: executes & displays the template results
|
Purpose: executes & displays the template results
|
||||||
\*======================================================================*/
|
\*======================================================================*/
|
||||||
|
|
||||||
function display($tpl_file)
|
function display($tpl_file, $cache_id = null)
|
||||||
{
|
{
|
||||||
$this->fetch($tpl_file, true);
|
$this->fetch($tpl_file, $cache_id, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*======================================================================*\
|
/*======================================================================*\
|
||||||
@@ -281,16 +293,16 @@ class Smarty
|
|||||||
Purpose: executes & returns or displays the template results
|
Purpose: executes & returns or displays the template results
|
||||||
\*======================================================================*/
|
\*======================================================================*/
|
||||||
|
|
||||||
function fetch($tpl_file, $display = false)
|
function fetch($tpl_file, $cache_id = null, $display = false)
|
||||||
{
|
{
|
||||||
global $HTTP_SERVER_VARS;
|
global $HTTP_SERVER_VARS;
|
||||||
|
|
||||||
if ($this->caching) {
|
if ($this->caching) {
|
||||||
// cache id = template path + the invoked script
|
// cache name = template path + cache_id
|
||||||
$cache_tpl_md5 = md5(realpath($this->template_dir.'/'.$tpl_file));
|
$cache_tpl_md5 = md5(realpath($this->template_dir.'/'.$tpl_file));
|
||||||
$cache_path_md5 = md5($HTTP_SERVER_VARS['SERVER_NAME'].'/'.$HTTP_SERVER_VARS['PHP_SELF']);
|
$cache_id_md5 = md5($cache_id);
|
||||||
$cache_path_dir = substr($cache_path_md5, 0, 2);
|
$cache_id_dir = substr($cache_id_md5, 0, 2);
|
||||||
$cache_file = $this->cache_dir."/$cache_tpl_md5/$cache_path_dir/$cache_tpl_md5_$cache_path_md5.cache";
|
$cache_file = $this->cache_dir."/$cache_tpl_md5/$cache_id_dir/{$cache_tpl_md5}_$cache_id_md5.cache";
|
||||||
|
|
||||||
if (file_exists($cache_file) &&
|
if (file_exists($cache_file) &&
|
||||||
($this->cache_lifetime == 0 ||
|
($this->cache_lifetime == 0 ||
|
||||||
@@ -438,8 +450,8 @@ class Smarty
|
|||||||
function _create_dir_structure($dir)
|
function _create_dir_structure($dir)
|
||||||
{
|
{
|
||||||
if (!file_exists($dir)) {
|
if (!file_exists($dir)) {
|
||||||
$dir_parts = preg_split('!/+!', $dir);
|
$dir_parts = preg_split('!/+!', $dir, -1, PREG_SPLIT_NO_EMPTY);
|
||||||
$new_dir = '';
|
$new_dir = ($dir{0} == '/') ? '/' : '';
|
||||||
foreach ($dir_parts as $dir_part) {
|
foreach ($dir_parts as $dir_part) {
|
||||||
$new_dir .= $dir_part;
|
$new_dir .= $dir_part;
|
||||||
if (!file_exists($new_dir) && !mkdir($new_dir, 0755)) {
|
if (!file_exists($new_dir) && !mkdir($new_dir, 0755)) {
|
||||||
@@ -532,22 +544,17 @@ class Smarty
|
|||||||
\*======================================================================*/
|
\*======================================================================*/
|
||||||
function _process_cached_inserts($results)
|
function _process_cached_inserts($results)
|
||||||
{
|
{
|
||||||
preg_match_all('!'.$this->_smarty_md5.'{insert_cache (.*)}'.$this->_smarty_md5.'\n??!Uis',
|
preg_match_all('!'.$this->_smarty_md5.'{insert_cache (.*)}'.$this->_smarty_md5.'!Uis',
|
||||||
$results, $match);
|
$results, $match);
|
||||||
list($cached_inserts, $insert_args) = $match;
|
list($cached_inserts, $insert_args) = $match;
|
||||||
|
|
||||||
for ($i = 0; $i < count($cached_inserts); $i++) {
|
for ($i = 0; $i < count($cached_inserts); $i++) {
|
||||||
$attrs = $this->_parse_attrs($insert_args[$i], false);
|
$args = unserialize($insert_args[$i]);
|
||||||
$name = $this->_dequote($attrs['name']);
|
$name = $args['name'];
|
||||||
|
unset($args['name']);
|
||||||
$arg_list = array();
|
|
||||||
foreach ($attrs as $arg_name => $arg_value) {
|
|
||||||
if ($arg_name == 'name') continue;
|
|
||||||
$arg_list[$arg_name] = $arg_value;
|
|
||||||
}
|
|
||||||
|
|
||||||
$function_name = 'insert_' . $name;
|
$function_name = 'insert_' . $name;
|
||||||
$replace = $function_name($arg_list);
|
$replace = $function_name($args);
|
||||||
|
|
||||||
$results = str_replace($cached_inserts[$i], $replace, $results);
|
$results = str_replace($cached_inserts[$i], $replace, $results);
|
||||||
}
|
}
|
||||||
@@ -555,6 +562,11 @@ class Smarty
|
|||||||
return $results;
|
return $results;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*======================================================================*\
|
||||||
|
Function: _compile_tag
|
||||||
|
Purpose: Compile a template tag
|
||||||
|
\*======================================================================*/
|
||||||
function _compile_tag($template_tag)
|
function _compile_tag($template_tag)
|
||||||
{
|
{
|
||||||
/* Matched comment. */
|
/* Matched comment. */
|
||||||
@@ -661,6 +673,11 @@ class Smarty
|
|||||||
return "<?php $function(array(".implode(',', (array)$arg_list).")); ?>";
|
return "<?php $function(array(".implode(',', (array)$arg_list).")); ?>";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*======================================================================*\
|
||||||
|
Function: _compile_insert_tag
|
||||||
|
Purpose: Compile {insert ...} tag
|
||||||
|
\*======================================================================*/
|
||||||
function _compile_insert_tag($tag_args)
|
function _compile_insert_tag($tag_args)
|
||||||
{
|
{
|
||||||
$attrs = $this->_parse_attrs($tag_args);
|
$attrs = $this->_parse_attrs($tag_args);
|
||||||
@@ -670,19 +687,20 @@ class Smarty
|
|||||||
$this->_syntax_error("missing insert name");
|
$this->_syntax_error("missing insert name");
|
||||||
}
|
}
|
||||||
|
|
||||||
if($this->caching)
|
|
||||||
return $this->_smarty_md5."{insert_cache $tag_args}".$this->_smarty_md5;
|
|
||||||
|
|
||||||
foreach ($attrs as $arg_name => $arg_value) {
|
foreach ($attrs as $arg_name => $arg_value) {
|
||||||
if ($arg_name == 'name') continue;
|
|
||||||
if (is_bool($arg_value))
|
if (is_bool($arg_value))
|
||||||
$arg_value = $arg_value ? 'true' : 'false';
|
$arg_value = $arg_value ? 'true' : 'false';
|
||||||
$arg_list[] = "'$arg_name' => $arg_value";
|
$arg_list[] = "'$arg_name' => $arg_value";
|
||||||
}
|
}
|
||||||
|
|
||||||
return "<?php echo insert_$name(array(".implode(',', (array)$arg_list).")); ?>";
|
return "<?php echo _smarty_insert_handler(array(".implode(',', (array)$arg_list)."), \$this->caching, \$this->_smarty_md5); ?>";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*======================================================================*\
|
||||||
|
Function: _compile_config_load_tag
|
||||||
|
Purpose: Compile {config_load ...} tag
|
||||||
|
\*======================================================================*/
|
||||||
function _compile_config_load_tag($tag_args)
|
function _compile_config_load_tag($tag_args)
|
||||||
{
|
{
|
||||||
$attrs = $this->_parse_attrs($tag_args);
|
$attrs = $this->_parse_attrs($tag_args);
|
||||||
@@ -707,6 +725,10 @@ class Smarty
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*======================================================================*\
|
||||||
|
Function: _compile_include_tag
|
||||||
|
Purpose: Compile {include ...} tag
|
||||||
|
\*======================================================================*/
|
||||||
function _compile_include_tag($tag_args)
|
function _compile_include_tag($tag_args)
|
||||||
{
|
{
|
||||||
$attrs = $this->_parse_attrs($tag_args);
|
$attrs = $this->_parse_attrs($tag_args);
|
||||||
@@ -728,17 +750,24 @@ class Smarty
|
|||||||
}
|
}
|
||||||
|
|
||||||
return "<?php " .
|
return "<?php " .
|
||||||
"function $include_func_name(\$file_name, \$def_vars, \$include_vars)\n" .
|
"if (!function_exists('$include_func_name')) {\n".
|
||||||
"{\n" .
|
" function $include_func_name(\$file_name, \$def_vars, \$include_vars)\n" .
|
||||||
" extract(\$def_vars);\n" .
|
" {\n" .
|
||||||
" extract(\$include_vars);\n" .
|
" extract(\$def_vars);\n" .
|
||||||
" include \"\$file_name.php\";\n" .
|
" extract(\$include_vars);\n" .
|
||||||
|
" include \"\$file_name.php\";\n" .
|
||||||
|
" }\n" .
|
||||||
"}\n" .
|
"}\n" .
|
||||||
"$include_func_name(\"$include_file_name\", get_defined_vars(), array(".implode(',', (array)$arg_list)."));\n?>\n";
|
"$include_func_name(\"$include_file_name\", get_defined_vars(), array(".implode(',', (array)$arg_list)."));\n?>\n";
|
||||||
} else
|
} else
|
||||||
return '<?php include "'.$this->compile_dir.'/'.$attrs['file'].'.php"; ?>';
|
return '<?php include "'.$this->compile_dir.'/'.$attrs['file'].'.php"; ?>';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*======================================================================*\
|
||||||
|
Function: _compile_section_start
|
||||||
|
Purpose: Compile {section ...} tag
|
||||||
|
\*======================================================================*/
|
||||||
function _compile_section_start($tag_args)
|
function _compile_section_start($tag_args)
|
||||||
{
|
{
|
||||||
$attrs = $this->_parse_attrs($tag_args);
|
$attrs = $this->_parse_attrs($tag_args);
|
||||||
@@ -795,6 +824,11 @@ class Smarty
|
|||||||
return $output;
|
return $output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*======================================================================*\
|
||||||
|
Function: _compile_if_tag
|
||||||
|
Purpose: Compile {if ...} tag
|
||||||
|
\*======================================================================*/
|
||||||
function _compile_if_tag($tag_args, $elseif = false)
|
function _compile_if_tag($tag_args, $elseif = false)
|
||||||
{
|
{
|
||||||
/* Tokenize args for 'if' tag. */
|
/* Tokenize args for 'if' tag. */
|
||||||
@@ -950,6 +984,11 @@ class Smarty
|
|||||||
return $tokens;
|
return $tokens;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*======================================================================*\
|
||||||
|
Function: _parse_attrs
|
||||||
|
Purpose: Parse attribute string
|
||||||
|
\*======================================================================*/
|
||||||
function _parse_attrs($tag_args, $quote = true)
|
function _parse_attrs($tag_args, $quote = true)
|
||||||
{
|
{
|
||||||
/* Tokenize tag attributes. */
|
/* Tokenize tag attributes. */
|
||||||
@@ -1019,6 +1058,11 @@ class Smarty
|
|||||||
return $attrs;
|
return $attrs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*======================================================================*\
|
||||||
|
Function: _preg_grep
|
||||||
|
Purpose: Emulate PHP's preg_grep()
|
||||||
|
\*======================================================================*/
|
||||||
function _preg_grep($pattern, $array)
|
function _preg_grep($pattern, $array)
|
||||||
{
|
{
|
||||||
$result = array();
|
$result = array();
|
||||||
@@ -1216,11 +1260,11 @@ class Smarty
|
|||||||
|
|
||||||
/*======================================================================*\
|
/*======================================================================*\
|
||||||
Function: _clear_tpl_cache_dir
|
Function: _clear_tpl_cache_dir
|
||||||
Purpose: Clear the specified template cache directory
|
Purpose: Clear the specified template cache
|
||||||
\*======================================================================*/
|
\*======================================================================*/
|
||||||
function _clear_tpl_cache_dir($tpl_cache_dir)
|
function _clear_tpl_cache_dir($cache_tpl_md5)
|
||||||
{
|
{
|
||||||
$cache_dir = $this->cache_dir.'/'.$tpl_cache_dir;
|
$cache_dir = $this->cache_dir.'/'.$cache_tpl_md5;
|
||||||
|
|
||||||
if (!is_dir($cache_dir))
|
if (!is_dir($cache_dir))
|
||||||
return false;
|
return false;
|
||||||
|
@@ -17,7 +17,7 @@ $smarty->assign("Class",array(array("A","B","C","D"), array("E", "F", "G", "H"),
|
|||||||
$smarty->assign("contacts", array(array("phone" => "1", "fax" => "2", "cell" => "3"),
|
$smarty->assign("contacts", array(array("phone" => "1", "fax" => "2", "cell" => "3"),
|
||||||
array("phone" => "555-4444", "fax" => "555-3333", "cell" => "760-1234")));
|
array("phone" => "555-4444", "fax" => "555-3333", "cell" => "760-1234")));
|
||||||
|
|
||||||
$smarty->display("index.tpl");
|
$smarty->display('index.tpl');
|
||||||
|
|
||||||
function insert_foo($args)
|
function insert_foo($args)
|
||||||
{
|
{
|
||||||
|
@@ -48,7 +48,7 @@ testing strip tags
|
|||||||
</PRE>
|
</PRE>
|
||||||
|
|
||||||
{insert name = foo
|
{insert name = foo
|
||||||
arg1=5}
|
arg1=$now arg2=true}
|
||||||
|
|
||||||
test: {$now|date_format:"%I:%M %p"}
|
test: {$now|date_format:"%I:%M %p"}
|
||||||
|
|
||||||
|
@@ -17,7 +17,7 @@ $smarty->assign("Class",array(array("A","B","C","D"), array("E", "F", "G", "H"),
|
|||||||
$smarty->assign("contacts", array(array("phone" => "1", "fax" => "2", "cell" => "3"),
|
$smarty->assign("contacts", array(array("phone" => "1", "fax" => "2", "cell" => "3"),
|
||||||
array("phone" => "555-4444", "fax" => "555-3333", "cell" => "760-1234")));
|
array("phone" => "555-4444", "fax" => "555-3333", "cell" => "760-1234")));
|
||||||
|
|
||||||
$smarty->display("index.tpl");
|
$smarty->display('index.tpl');
|
||||||
|
|
||||||
function insert_foo($args)
|
function insert_foo($args)
|
||||||
{
|
{
|
||||||
|
@@ -168,20 +168,20 @@ class Smarty
|
|||||||
|
|
||||||
|
|
||||||
/*======================================================================*\
|
/*======================================================================*\
|
||||||
Function: register_func
|
Function: register_function
|
||||||
Purpose: Registers custom function to be used in templates
|
Purpose: Registers custom function to be used in templates
|
||||||
\*======================================================================*/
|
\*======================================================================*/
|
||||||
function register_func($function, $function_impl)
|
function register_function($function, $function_impl)
|
||||||
{
|
{
|
||||||
$this->custom_funcs[$function] = $function_impl;
|
$this->custom_funcs[$function] = $function_impl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*======================================================================*\
|
/*======================================================================*\
|
||||||
Function: register_mod
|
Function: register_modifier
|
||||||
Purpose: Registers modifier to be used in templates
|
Purpose: Registers modifier to be used in templates
|
||||||
\*======================================================================*/
|
\*======================================================================*/
|
||||||
function register_mod($modifier, $modifier_impl)
|
function register_modifier($modifier, $modifier_impl)
|
||||||
{
|
{
|
||||||
$this->custom_mods[$modifier] = $modifier_impl;
|
$this->custom_mods[$modifier] = $modifier_impl;
|
||||||
}
|
}
|
||||||
@@ -189,13 +189,24 @@ class Smarty
|
|||||||
|
|
||||||
/*======================================================================*\
|
/*======================================================================*\
|
||||||
Function: clear_cache()
|
Function: clear_cache()
|
||||||
Purpose: clear cached content for the given template
|
Purpose: clear cached content for the given template and cache id
|
||||||
\*======================================================================*/
|
\*======================================================================*/
|
||||||
|
|
||||||
function clear_cache($tpl_file)
|
function clear_cache($tpl_file, $cache_id = null)
|
||||||
{
|
{
|
||||||
$cache_tpl_md5 = md5(realpath($this->template_dir.'/'.$tpl_file));
|
$cache_tpl_md5 = md5(realpath($this->template_dir.'/'.$tpl_file));
|
||||||
return $this->_clear_tpl_cache_dir($cache_tpl_md5);
|
$cache_dir = $this->cache_dir.'/'.$cache_tpl_md5;
|
||||||
|
|
||||||
|
if (!is_dir($cache_dir))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (isset($cache_id)) {
|
||||||
|
$cache_id_md5 = md5($cache_id);
|
||||||
|
$cache_id_dir = substr($cache_id_md5, 0, 2);
|
||||||
|
$cache_file = "$cache_dir/$cache_id_dir/{$cache_tpl_md5}_$cache_id_md5.cache";
|
||||||
|
return (bool)(is_file($cache_file) && unlink($cache_file));
|
||||||
|
} else
|
||||||
|
return $this->_clear_tpl_cache_dir($cache_tpl_md5);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -222,18 +233,19 @@ class Smarty
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*======================================================================*\
|
/*======================================================================*\
|
||||||
Function: is_cached()
|
Function: is_cached()
|
||||||
Purpose: test to see if valid cache exists for this template
|
Purpose: test to see if valid cache exists for this template
|
||||||
\*======================================================================*/
|
\*======================================================================*/
|
||||||
|
|
||||||
function is_cached($tpl_file)
|
function is_cached($tpl_file, $cache_id = null)
|
||||||
{
|
{
|
||||||
// cache id = template path + the invoked script
|
// cache name = template path + cache_id
|
||||||
$cache_tpl_md5 = md5(realpath($this->template_dir.'/'.$tpl_file));
|
$cache_tpl_md5 = md5(realpath($this->template_dir.'/'.$tpl_file));
|
||||||
$cache_path_md5 = md5($HTTP_SERVER_VARS['SERVER_NAME'].'/'.$HTTP_SERVER_VARS['PHP_SELF']);
|
$cache_id_md5 = md5($cache_id);
|
||||||
$cache_path_dir = substr($cache_path_md5, 0, 2);
|
$cache_id_dir = substr($cache_id_md5, 0, 2);
|
||||||
$cache_file = $this->cache_dir."/$cache_tpl_md5/$cache_path_dir/$cache_tpl_md5_$cache_path_md5.cache";
|
$cache_file = $this->cache_dir."/$cache_tpl_md5/$cache_id_dir/{$cache_tpl_md5}_$cache_id_md5.cache";
|
||||||
|
|
||||||
if (file_exists($cache_file) &&
|
if (file_exists($cache_file) &&
|
||||||
($this->cache_lifetime == 0 ||
|
($this->cache_lifetime == 0 ||
|
||||||
@@ -271,9 +283,9 @@ class Smarty
|
|||||||
Purpose: executes & displays the template results
|
Purpose: executes & displays the template results
|
||||||
\*======================================================================*/
|
\*======================================================================*/
|
||||||
|
|
||||||
function display($tpl_file)
|
function display($tpl_file, $cache_id = null)
|
||||||
{
|
{
|
||||||
$this->fetch($tpl_file, true);
|
$this->fetch($tpl_file, $cache_id, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*======================================================================*\
|
/*======================================================================*\
|
||||||
@@ -281,16 +293,16 @@ class Smarty
|
|||||||
Purpose: executes & returns or displays the template results
|
Purpose: executes & returns or displays the template results
|
||||||
\*======================================================================*/
|
\*======================================================================*/
|
||||||
|
|
||||||
function fetch($tpl_file, $display = false)
|
function fetch($tpl_file, $cache_id = null, $display = false)
|
||||||
{
|
{
|
||||||
global $HTTP_SERVER_VARS;
|
global $HTTP_SERVER_VARS;
|
||||||
|
|
||||||
if ($this->caching) {
|
if ($this->caching) {
|
||||||
// cache id = template path + the invoked script
|
// cache name = template path + cache_id
|
||||||
$cache_tpl_md5 = md5(realpath($this->template_dir.'/'.$tpl_file));
|
$cache_tpl_md5 = md5(realpath($this->template_dir.'/'.$tpl_file));
|
||||||
$cache_path_md5 = md5($HTTP_SERVER_VARS['SERVER_NAME'].'/'.$HTTP_SERVER_VARS['PHP_SELF']);
|
$cache_id_md5 = md5($cache_id);
|
||||||
$cache_path_dir = substr($cache_path_md5, 0, 2);
|
$cache_id_dir = substr($cache_id_md5, 0, 2);
|
||||||
$cache_file = $this->cache_dir."/$cache_tpl_md5/$cache_path_dir/$cache_tpl_md5_$cache_path_md5.cache";
|
$cache_file = $this->cache_dir."/$cache_tpl_md5/$cache_id_dir/{$cache_tpl_md5}_$cache_id_md5.cache";
|
||||||
|
|
||||||
if (file_exists($cache_file) &&
|
if (file_exists($cache_file) &&
|
||||||
($this->cache_lifetime == 0 ||
|
($this->cache_lifetime == 0 ||
|
||||||
@@ -438,8 +450,8 @@ class Smarty
|
|||||||
function _create_dir_structure($dir)
|
function _create_dir_structure($dir)
|
||||||
{
|
{
|
||||||
if (!file_exists($dir)) {
|
if (!file_exists($dir)) {
|
||||||
$dir_parts = preg_split('!/+!', $dir);
|
$dir_parts = preg_split('!/+!', $dir, -1, PREG_SPLIT_NO_EMPTY);
|
||||||
$new_dir = '';
|
$new_dir = ($dir{0} == '/') ? '/' : '';
|
||||||
foreach ($dir_parts as $dir_part) {
|
foreach ($dir_parts as $dir_part) {
|
||||||
$new_dir .= $dir_part;
|
$new_dir .= $dir_part;
|
||||||
if (!file_exists($new_dir) && !mkdir($new_dir, 0755)) {
|
if (!file_exists($new_dir) && !mkdir($new_dir, 0755)) {
|
||||||
@@ -532,22 +544,17 @@ class Smarty
|
|||||||
\*======================================================================*/
|
\*======================================================================*/
|
||||||
function _process_cached_inserts($results)
|
function _process_cached_inserts($results)
|
||||||
{
|
{
|
||||||
preg_match_all('!'.$this->_smarty_md5.'{insert_cache (.*)}'.$this->_smarty_md5.'\n??!Uis',
|
preg_match_all('!'.$this->_smarty_md5.'{insert_cache (.*)}'.$this->_smarty_md5.'!Uis',
|
||||||
$results, $match);
|
$results, $match);
|
||||||
list($cached_inserts, $insert_args) = $match;
|
list($cached_inserts, $insert_args) = $match;
|
||||||
|
|
||||||
for ($i = 0; $i < count($cached_inserts); $i++) {
|
for ($i = 0; $i < count($cached_inserts); $i++) {
|
||||||
$attrs = $this->_parse_attrs($insert_args[$i], false);
|
$args = unserialize($insert_args[$i]);
|
||||||
$name = $this->_dequote($attrs['name']);
|
$name = $args['name'];
|
||||||
|
unset($args['name']);
|
||||||
$arg_list = array();
|
|
||||||
foreach ($attrs as $arg_name => $arg_value) {
|
|
||||||
if ($arg_name == 'name') continue;
|
|
||||||
$arg_list[$arg_name] = $arg_value;
|
|
||||||
}
|
|
||||||
|
|
||||||
$function_name = 'insert_' . $name;
|
$function_name = 'insert_' . $name;
|
||||||
$replace = $function_name($arg_list);
|
$replace = $function_name($args);
|
||||||
|
|
||||||
$results = str_replace($cached_inserts[$i], $replace, $results);
|
$results = str_replace($cached_inserts[$i], $replace, $results);
|
||||||
}
|
}
|
||||||
@@ -555,6 +562,11 @@ class Smarty
|
|||||||
return $results;
|
return $results;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*======================================================================*\
|
||||||
|
Function: _compile_tag
|
||||||
|
Purpose: Compile a template tag
|
||||||
|
\*======================================================================*/
|
||||||
function _compile_tag($template_tag)
|
function _compile_tag($template_tag)
|
||||||
{
|
{
|
||||||
/* Matched comment. */
|
/* Matched comment. */
|
||||||
@@ -661,6 +673,11 @@ class Smarty
|
|||||||
return "<?php $function(array(".implode(',', (array)$arg_list).")); ?>";
|
return "<?php $function(array(".implode(',', (array)$arg_list).")); ?>";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*======================================================================*\
|
||||||
|
Function: _compile_insert_tag
|
||||||
|
Purpose: Compile {insert ...} tag
|
||||||
|
\*======================================================================*/
|
||||||
function _compile_insert_tag($tag_args)
|
function _compile_insert_tag($tag_args)
|
||||||
{
|
{
|
||||||
$attrs = $this->_parse_attrs($tag_args);
|
$attrs = $this->_parse_attrs($tag_args);
|
||||||
@@ -670,19 +687,20 @@ class Smarty
|
|||||||
$this->_syntax_error("missing insert name");
|
$this->_syntax_error("missing insert name");
|
||||||
}
|
}
|
||||||
|
|
||||||
if($this->caching)
|
|
||||||
return $this->_smarty_md5."{insert_cache $tag_args}".$this->_smarty_md5;
|
|
||||||
|
|
||||||
foreach ($attrs as $arg_name => $arg_value) {
|
foreach ($attrs as $arg_name => $arg_value) {
|
||||||
if ($arg_name == 'name') continue;
|
|
||||||
if (is_bool($arg_value))
|
if (is_bool($arg_value))
|
||||||
$arg_value = $arg_value ? 'true' : 'false';
|
$arg_value = $arg_value ? 'true' : 'false';
|
||||||
$arg_list[] = "'$arg_name' => $arg_value";
|
$arg_list[] = "'$arg_name' => $arg_value";
|
||||||
}
|
}
|
||||||
|
|
||||||
return "<?php echo insert_$name(array(".implode(',', (array)$arg_list).")); ?>";
|
return "<?php echo _smarty_insert_handler(array(".implode(',', (array)$arg_list)."), \$this->caching, \$this->_smarty_md5); ?>";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*======================================================================*\
|
||||||
|
Function: _compile_config_load_tag
|
||||||
|
Purpose: Compile {config_load ...} tag
|
||||||
|
\*======================================================================*/
|
||||||
function _compile_config_load_tag($tag_args)
|
function _compile_config_load_tag($tag_args)
|
||||||
{
|
{
|
||||||
$attrs = $this->_parse_attrs($tag_args);
|
$attrs = $this->_parse_attrs($tag_args);
|
||||||
@@ -707,6 +725,10 @@ class Smarty
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*======================================================================*\
|
||||||
|
Function: _compile_include_tag
|
||||||
|
Purpose: Compile {include ...} tag
|
||||||
|
\*======================================================================*/
|
||||||
function _compile_include_tag($tag_args)
|
function _compile_include_tag($tag_args)
|
||||||
{
|
{
|
||||||
$attrs = $this->_parse_attrs($tag_args);
|
$attrs = $this->_parse_attrs($tag_args);
|
||||||
@@ -728,17 +750,24 @@ class Smarty
|
|||||||
}
|
}
|
||||||
|
|
||||||
return "<?php " .
|
return "<?php " .
|
||||||
"function $include_func_name(\$file_name, \$def_vars, \$include_vars)\n" .
|
"if (!function_exists('$include_func_name')) {\n".
|
||||||
"{\n" .
|
" function $include_func_name(\$file_name, \$def_vars, \$include_vars)\n" .
|
||||||
" extract(\$def_vars);\n" .
|
" {\n" .
|
||||||
" extract(\$include_vars);\n" .
|
" extract(\$def_vars);\n" .
|
||||||
" include \"\$file_name.php\";\n" .
|
" extract(\$include_vars);\n" .
|
||||||
|
" include \"\$file_name.php\";\n" .
|
||||||
|
" }\n" .
|
||||||
"}\n" .
|
"}\n" .
|
||||||
"$include_func_name(\"$include_file_name\", get_defined_vars(), array(".implode(',', (array)$arg_list)."));\n?>\n";
|
"$include_func_name(\"$include_file_name\", get_defined_vars(), array(".implode(',', (array)$arg_list)."));\n?>\n";
|
||||||
} else
|
} else
|
||||||
return '<?php include "'.$this->compile_dir.'/'.$attrs['file'].'.php"; ?>';
|
return '<?php include "'.$this->compile_dir.'/'.$attrs['file'].'.php"; ?>';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*======================================================================*\
|
||||||
|
Function: _compile_section_start
|
||||||
|
Purpose: Compile {section ...} tag
|
||||||
|
\*======================================================================*/
|
||||||
function _compile_section_start($tag_args)
|
function _compile_section_start($tag_args)
|
||||||
{
|
{
|
||||||
$attrs = $this->_parse_attrs($tag_args);
|
$attrs = $this->_parse_attrs($tag_args);
|
||||||
@@ -795,6 +824,11 @@ class Smarty
|
|||||||
return $output;
|
return $output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*======================================================================*\
|
||||||
|
Function: _compile_if_tag
|
||||||
|
Purpose: Compile {if ...} tag
|
||||||
|
\*======================================================================*/
|
||||||
function _compile_if_tag($tag_args, $elseif = false)
|
function _compile_if_tag($tag_args, $elseif = false)
|
||||||
{
|
{
|
||||||
/* Tokenize args for 'if' tag. */
|
/* Tokenize args for 'if' tag. */
|
||||||
@@ -950,6 +984,11 @@ class Smarty
|
|||||||
return $tokens;
|
return $tokens;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*======================================================================*\
|
||||||
|
Function: _parse_attrs
|
||||||
|
Purpose: Parse attribute string
|
||||||
|
\*======================================================================*/
|
||||||
function _parse_attrs($tag_args, $quote = true)
|
function _parse_attrs($tag_args, $quote = true)
|
||||||
{
|
{
|
||||||
/* Tokenize tag attributes. */
|
/* Tokenize tag attributes. */
|
||||||
@@ -1019,6 +1058,11 @@ class Smarty
|
|||||||
return $attrs;
|
return $attrs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*======================================================================*\
|
||||||
|
Function: _preg_grep
|
||||||
|
Purpose: Emulate PHP's preg_grep()
|
||||||
|
\*======================================================================*/
|
||||||
function _preg_grep($pattern, $array)
|
function _preg_grep($pattern, $array)
|
||||||
{
|
{
|
||||||
$result = array();
|
$result = array();
|
||||||
@@ -1216,11 +1260,11 @@ class Smarty
|
|||||||
|
|
||||||
/*======================================================================*\
|
/*======================================================================*\
|
||||||
Function: _clear_tpl_cache_dir
|
Function: _clear_tpl_cache_dir
|
||||||
Purpose: Clear the specified template cache directory
|
Purpose: Clear the specified template cache
|
||||||
\*======================================================================*/
|
\*======================================================================*/
|
||||||
function _clear_tpl_cache_dir($tpl_cache_dir)
|
function _clear_tpl_cache_dir($cache_tpl_md5)
|
||||||
{
|
{
|
||||||
$cache_dir = $this->cache_dir.'/'.$tpl_cache_dir;
|
$cache_dir = $this->cache_dir.'/'.$cache_tpl_md5;
|
||||||
|
|
||||||
if (!is_dir($cache_dir))
|
if (!is_dir($cache_dir))
|
||||||
return false;
|
return false;
|
||||||
|
@@ -48,7 +48,7 @@ testing strip tags
|
|||||||
</PRE>
|
</PRE>
|
||||||
|
|
||||||
{insert name = foo
|
{insert name = foo
|
||||||
arg1=5}
|
arg1=$now arg2=true}
|
||||||
|
|
||||||
test: {$now|date_format:"%I:%M %p"}
|
test: {$now|date_format:"%I:%M %p"}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user