diff --git a/NEWS b/NEWS
index 29673085..6e20d552 100644
--- a/NEWS
+++ b/NEWS
@@ -1,10 +1,13 @@
+ - added regex_replace modifier, documented (Monte)
+ - added debugging console feature, documented (Monte)
+ - added custom function assign_debug_info, documented (Monte)
- added 'scope' attribute for {config_load}, 'global' is now deprecated but
is still supported. (Andrei)
- reduced template symbol table pollution by moving config array into the
class itself. (Andrei)
- fixed a bug with passing quoted arguments to modifiers inside {if}
statements. (Andrei, Sam Beckwith)
- - added security features for third party template editing. (Monte)
+ - added security features for third party template editing, documented (Monte)
- added assign custom function, documented. (Monte)
- fixed bug with template header using version instead of _version. (Monte)
- fixed a problem with putting $ followed by numbers inside {strip} and
diff --git a/Smarty.addons.php b/Smarty.addons.php
index 575c1d5c..82a7fbba 100644
--- a/Smarty.addons.php
+++ b/Smarty.addons.php
@@ -177,6 +177,15 @@ function smarty_mod_replace($string, $search, $replace)
return str_replace($search, $replace, $string);
}
+/*======================================================================*\
+ Function: smarty_mod_regex_replace
+ Purpose: regular epxression search/replace
+\*======================================================================*/
+function smarty_mod_regex_replace($string, $search, $replace)
+{
+ return preg_replace($search, $replace, $string);
+}
+
/*======================================================================*\
Function: smarty_mod_strip_tags
Purpose: strip html tags from text
@@ -598,6 +607,21 @@ function smarty_func_counter() {
return true;
}
+/*======================================================================*\
+ Function: smarty_func_assign_debug_info
+ Purpose: assign debug info to the template
+\*======================================================================*/
+function smarty_func_assign_debug_info($args, &$smarty_obj) {
+ $assigned_vars = $smarty_obj->_tpl_vars;
+ ksort($assigned_vars);
+ $included_templates = $smarty_obj->_included_tpls;
+ sort($included_templates);
+ $smarty_obj->assign("_debug_keys",array_keys($assigned_vars));
+ $smarty_obj->assign("_debug_vals",array_values($assigned_vars));
+ $smarty_obj->assign("_debug_tpls",$included_templates);
+ return true;
+}
+
/* vim: set expandtab: */
?>
diff --git a/Smarty.class.php b/Smarty.class.php
index 297deb5f..f03ac825 100644
--- a/Smarty.class.php
+++ b/Smarty.class.php
@@ -59,7 +59,7 @@ class Smarty
var $template_dir = './templates'; // name of directory for templates
var $compile_dir = './templates_c'; // name of directory for compiled templates
var $config_dir = './configs'; // directory where config files are located
-
+
var $global_assign = array( 'HTTP_SERVER_VARS' => array( 'SCRIPT_NAME' )
); // variables from the GLOBALS array
// that are implicitly assigned
@@ -114,7 +114,7 @@ class Smarty
var $left_delimiter = '{'; // template tag delimiters.
var $right_delimiter = '}';
-
+
var $compiler_funcs = array(
);
@@ -124,7 +124,8 @@ class Smarty
'math' => 'smarty_func_math',
'fetch' => 'smarty_func_fetch',
'counter' => 'smarty_func_counter',
- 'assign' => 'smarty_func_assign'
+ 'assign' => 'smarty_func_assign',
+ 'assign_debug_info' => 'smarty_func_assign_debug_info'
);
var $custom_mods = array( 'lower' => 'strtolower',
@@ -136,6 +137,7 @@ class Smarty
'date_format' => 'smarty_mod_date_format',
'string_format' => 'smarty_mod_string_format',
'replace' => 'smarty_mod_replace',
+ 'regex_replace' => 'smarty_mod_regex_replace',
'strip_tags' => 'smarty_mod_strip_tags',
'default' => 'smarty_mod_default',
'count_characters' => 'smarty_mod_count_characters',
@@ -145,6 +147,8 @@ class Smarty
);
var $show_info_header = false; // display HTML info header at top of page output
+ var $show_info_include = true; // display HTML comments at top & bottom of
+ // each included template
var $compiler_class = 'Smarty_Compiler'; // the compiler class used by
// Smarty to compile templates
@@ -166,6 +170,7 @@ class Smarty
var $_smarty_md5 = 'f8d698aea36fcbead2b9d5359ffca76f'; // md5 checksum of the string 'Smarty'
var $_version = '1.4.2'; // Smarty version number
var $_extract = false; // flag for custom functions
+ var $_included_tpls = array(); // list of included templates
/*======================================================================*\
Function: Smarty
@@ -485,8 +490,10 @@ class Smarty
\*======================================================================*/
function fetch($tpl_file, $cache_id = null, $display = false)
{
- global $HTTP_SERVER_VARS;
+ global $HTTP_SERVER_VARS, $QUERY_STRING, $HTTP_COOKIE_VARS;
+ $this->_included_tpls[] = $tpl_file;
+
if ($this->caching) {
// cache name = template path + cache_id
$cache_tpl_md5 = md5(realpath($this->template_dir.'/'.$tpl_file));
@@ -528,18 +535,30 @@ class Smarty
} else {
$info_header = '';
}
-
+
// if we just need to display the results, don't perform output
// buffering - for speed
if ($display && !$this->caching) {
echo $info_header;
$this->_process_template($tpl_file, $compile_path);
+ if($this->show_info_include) {
+ echo "\n\n";
+ }
include($compile_path);
+ if($this->show_info_include) {
+ echo "\n\n";
+ }
} else {
ob_start();
echo $info_header;
$this->_process_template($tpl_file, $compile_path);
+ if($this->show_info_include) {
+ echo "\n\n";
+ }
include($compile_path);
+ if($this->show_info_include) {
+ echo "\n\n";
+ }
$results = ob_get_contents();
ob_end_clean();
}
@@ -557,6 +576,72 @@ class Smarty
}
}
+/*======================================================================*\
+ Function: _display_debug_info
+ Purpose: display debugging information
+\*======================================================================*/
+ function _display_debug_info()
+ {
+ asort($this->_debug_tpl);
+ reset($this->_debug_tpl);
+ ksort($this->_tpl_vars);
+ reset($this->_tpl_vars);
+ echo "\n";
+ }
+
+/*======================================================================*\
+ Function: _print_debug_array
+ Purpose: display debugging information
+\*======================================================================*/
+ function _print_debug_array($array,$level)
+ {
+ foreach($array as $key => $val) {
+ for($x=0; $x<$level; $x++) {
+ echo '_smarty_console.document.write(" "); ';
+ echo "\n";
+ }
+ $search = array('!"!','![\r\t\n]!');
+ $replace = array("'",' ');
+ echo '_smarty_console.document.write("{\$'.$key.'} = '.preg_replace($search,$replace,htmlspecialchars(substr($val,0,50))).'
"); ';
+ echo "\n";
+ if(is_array($val)) {
+ $this->_print_debug_array($val,$level+1);
+ }
+ }
+ }
+
/*======================================================================*\
Function: _process_template()
Purpose:
@@ -751,8 +836,16 @@ class Smarty
array_unshift($this->_config, $this->_config[0]);
$this->_process_template($_smarty_include_tpl_file, $compile_path);
+ if($this->show_info_include) {
+ echo "\n\n";
+ }
include($compile_path);
+ if($this->show_info_include) {
+ echo "\n\n";
+ }
+ $this->_included_tpls[] = $_smarty_include_tpl_file;
+
array_shift($this->_config);
}
diff --git a/demo/templates/debug.tpl b/demo/templates/debug.tpl
new file mode 100644
index 00000000..fad4204d
--- /dev/null
+++ b/demo/templates/debug.tpl
@@ -0,0 +1,21 @@
+{* Smarty *}
+
+{assign_debug_info}
+
+
diff --git a/demo/templates/footer.tpl b/demo/templates/footer.tpl
index e04310fd..25dcaeaf 100644
--- a/demo/templates/footer.tpl
+++ b/demo/templates/footer.tpl
@@ -1,2 +1,3 @@
+{include file="debug.tpl"}