diff --git a/Smarty.addons.php b/Smarty.addons.php index c45a4691..c1976d8c 100644 --- a/Smarty.addons.php +++ b/Smarty.addons.php @@ -4,165 +4,54 @@ * Project: Smarty: the PHP compiled template engine * File: Smarty.functions.php * Author: Monte Ohrt + * Andrei Zmievski * - * This is the smarty custom function file. - * To add your own functions, name the function - * smarty_[funcname], then add the function name - * to the $registered_functions array in the - * smarty.class.php file. You may then call your - * function from a template file like so: {funcname [$var]...} */ /*======================================================================*\ - Function: htmlesc - Purpose: html escape template vars + Function: smarty_mod_escape + Purpose: Escape the string according to escapement type \*======================================================================*/ - -function smarty_htmlesc($var) +function smarty_mod_escape($string, $esc_type = 'html') { - print htmlspecialchars($var); -} + switch ($esc_type) { + case 'html': + return htmlspecialchars($string); -/*======================================================================*\ - Function: urlesc - Purpose: URL escape template vars -\*======================================================================*/ + case 'url': + return urlencode($string); -function smarty_urlesc($var) -{ - print urlencode($var); -} - -/*======================================================================*\ - Function: default - Purpose: display a default value for empty template vars -\*======================================================================*/ - -function smarty_default($var,$default_val) -{ - if(empty($var)) - print $default_val; - else - print $var; -} - -/*======================================================================*\ - Function: configload - Purpose: load vars from a config file - - Notes: config files must be in this format: - - key = "val" - key2 = "val2" -\*======================================================================*/ - -function smarty_configload($config_file) -{ - - global $_config_vars; - - if(!is_array($_config_vars)) - $_config_vars = array(); - - // only load in the config file - if(! ($fd = fopen($config_file,"r"))) - { - print (""); - return false; + default: + return $string; } - $config_contents = fread($fd,filesize($config_file)); - fclose($fd); - - $contents_array = preg_split("/[\r\n]+/", $config_contents); - - // read in the variables from the config file - foreach($contents_array as $current_line) - { - if (preg_match("/^\w+/",$current_line)) - { - if(preg_match("/^([^\=]+)=(.*)/", $current_line, $preg_results)) - { - $config_key = $preg_results[1]; - $config_val = trim($preg_results[2]); - - // remove tabs and spaces from key - $key = preg_replace("/[ \t]/", "", $config_key); - - // is value surrounded by quotes? - if (!preg_match("/^['\"]/", $config_val)) - { - $val = $config_val; - } - else - { - // Strip the leading and trailing quotes - $val = preg_replace("/^['\"]/","",$config_val); - $val = preg_replace("/['\"]$/","",$val); - } - - // store the key and val in the config array - $_config_vars[$key] = $val; - } - } - } - - return true; - } + /*======================================================================*\ - Function: configclear - Purpose: clear config vars + Function: smarty_mod_truncate + Purpose: Truncate a string to a certain length if necessary, + optionally splitting in the middle of a word, and + appending the $etc string. \*======================================================================*/ - -function smarty_configclear() +function smarty_mod_truncate($string, $length = 80, $etc = '...', $break_words = false) { - - global $_config_vars; - $_config_vars = array(); - - return true; - + if (strlen($string) > $length) { + $length -= strlen($etc); + $fragment = substr($string, 0, $length+1); + if ($break_words) + $fragment = substr($fragment, 0, -1); + else + $fragment = preg_replace('/\s+(\S+)?$/', '', $fragment); + return $fragment.$etc; + } else + return $string; } -/*======================================================================*\ - Function: configprint - Purpose: print a var from config -\*======================================================================*/ -function smarty_configprint($var) +function smarty_mod_spacify($string, $spacify_char = ' ') { - - global $_config_vars; - - if (isset($_config_vars[$var])) - print $_config_vars[$var]; - else - print ""; - - return true; - + return implode($spacify_char, preg_split('//', $string, -1, PREG_SPLIT_NO_EMPTY)); } -/*======================================================================*\ - Function: configset - Purpose: set a var from config -\*======================================================================*/ - -function smarty_configset($var,&$setvar) -{ - - global $_config_vars; - - if (isset($_config_vars[$var])) - $setvar = $_config_vars[$var]; - else - return false; - - return true; - -} - - ?> diff --git a/Smarty.class.php b/Smarty.class.php index d16701f3..2f4decc3 100644 --- a/Smarty.class.php +++ b/Smarty.class.php @@ -43,6 +43,7 @@ class Smarty // registered template functions // NOTE: leave off the "smarty_" prefix on the actual PHP function name + /* var $registered_functions = array( "htmlesc", "urlesc", "default", @@ -51,13 +52,18 @@ class Smarty "configprint", "configset" ); + */ - var $_modifiers = array( "lower" => "smarty_mod_lower", - "upper" => "smarty_mod_upper" + var $_modifiers = array( 'lower' => 'strtolower', + 'upper' => 'strtoupper', + 'capitalize' => 'ucwords', + 'escape' => 'smarty_mod_escape', + 'truncate' => 'smarty_mod_truncate', + 'spacify' => 'smarty_mod_spacify' ); // internal vars - var $errorMsg = false; // error messages + var $_error_msg = false; // error messages var $_tpl_vars = array(); var $_sectionelse_stack = array(); // keeps track of whether section had 'else' part @@ -162,7 +168,7 @@ class Smarty // exit if recursion depth is met if($this->max_recursion_depth != 0 && $depth >= $this->max_recursion_depth) { - $this->_set_errorMsg("recursion depth of $depth reached on $tpl_dir/$curr_file. exiting."); + $this->_set_error_msg("recursion depth of $depth reached on $tpl_dir/$curr_file. exiting."); return false; } if(is_dir($tpl_dir)) @@ -198,7 +204,7 @@ class Smarty else { // invalid file type, skipping - $this->_set_errorMsg("Invalid filetype for $filepath, skipping"); + $this->_set_error_msg("Invalid filetype for $filepath, skipping"); continue; } } @@ -206,7 +212,7 @@ class Smarty } else { - $this->_set_errorMsg("Directory \"$tpl_dir\" does not exist or is not a directory."); + $this->_set_error_msg("Directory \"$tpl_dir\" does not exist or is not a directory."); return false; } return true; @@ -234,7 +240,7 @@ class Smarty if(!file_exists($compile_dir)) if(!mkdir($compile_dir,0755)) { - $this->_set_errorMsg("problem creating directory \"$compile_dir\""); + $this->_set_error_msg("problem creating directory \"$compile_dir\""); return false; } // compile the template file if none exists or has been modified @@ -253,7 +259,7 @@ class Smarty } else { - $this->_set_errorMsg("problem matching \"$filepath.\""); + $this->_set_error_msg("problem matching \"$filepath.\""); return false; } return true; @@ -772,7 +778,7 @@ class Smarty { if(! ($fd = fopen($filename,"r"))) { - $this->_set_errorMsg("problem reading \"$filename.\""); + $this->_set_error_msg("problem reading \"$filename.\""); return false; } $contents = fread($fd,filesize($filename)); @@ -789,7 +795,7 @@ class Smarty { if(!($fd = fopen($filename,"w"))) { - $this->_set_errorMsg("problem writing \"$filename.\""); + $this->_set_error_msg("problem writing \"$filename.\""); return false; } fwrite($fd,$contents); @@ -798,13 +804,13 @@ class Smarty } /*======================================================================*\ - Function: _set_errorMsg() + Function: _set_error_msg() Purpose: set the error message \*======================================================================*/ - function _set_errorMsg($errorMsg) + function _set_error_msg($error_msg) { - $this->errorMsg="smarty error: $errorMsg"; + $this->_error_msg="smarty error: $error_msg"; return true; } diff --git a/demo/index.php b/demo/index.php index a79592d2..2eedb540 100644 --- a/demo/index.php +++ b/demo/index.php @@ -4,7 +4,7 @@ require("Smarty.class.php"); $smarty = new Smarty; -$smarty->assign("Name","Fred"); +$smarty->assign("Name","Fred Irving Johnathan Bradley Peppergill"); $smarty->assign("FirstName",array("John","Mary","James","Henry")); $smarty->assign("LastName",array("Doe","Smith","Johnson","Case")); $smarty->assign("Class",array(array("A","B","C","D"), array("E", "F", "G", "H"), diff --git a/demo/templates/index.tpl b/demo/templates/index.tpl index 3002fc9d..5f83ce96 100644 --- a/demo/templates/index.tpl +++ b/demo/templates/index.tpl @@ -1,9 +1,9 @@ {config_load file=test.conf} -Title: {#title#} +Title: {#title#|capitalize} {* A simple variable test *} -hello, my name is {$Name}. +hello, my name is {$Name} My interests are: {section name=outer loop=$FirstName} diff --git a/index.php b/index.php index a79592d2..2eedb540 100644 --- a/index.php +++ b/index.php @@ -4,7 +4,7 @@ require("Smarty.class.php"); $smarty = new Smarty; -$smarty->assign("Name","Fred"); +$smarty->assign("Name","Fred Irving Johnathan Bradley Peppergill"); $smarty->assign("FirstName",array("John","Mary","James","Henry")); $smarty->assign("LastName",array("Doe","Smith","Johnson","Case")); $smarty->assign("Class",array(array("A","B","C","D"), array("E", "F", "G", "H"), diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index d16701f3..2f4decc3 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -43,6 +43,7 @@ class Smarty // registered template functions // NOTE: leave off the "smarty_" prefix on the actual PHP function name + /* var $registered_functions = array( "htmlesc", "urlesc", "default", @@ -51,13 +52,18 @@ class Smarty "configprint", "configset" ); + */ - var $_modifiers = array( "lower" => "smarty_mod_lower", - "upper" => "smarty_mod_upper" + var $_modifiers = array( 'lower' => 'strtolower', + 'upper' => 'strtoupper', + 'capitalize' => 'ucwords', + 'escape' => 'smarty_mod_escape', + 'truncate' => 'smarty_mod_truncate', + 'spacify' => 'smarty_mod_spacify' ); // internal vars - var $errorMsg = false; // error messages + var $_error_msg = false; // error messages var $_tpl_vars = array(); var $_sectionelse_stack = array(); // keeps track of whether section had 'else' part @@ -162,7 +168,7 @@ class Smarty // exit if recursion depth is met if($this->max_recursion_depth != 0 && $depth >= $this->max_recursion_depth) { - $this->_set_errorMsg("recursion depth of $depth reached on $tpl_dir/$curr_file. exiting."); + $this->_set_error_msg("recursion depth of $depth reached on $tpl_dir/$curr_file. exiting."); return false; } if(is_dir($tpl_dir)) @@ -198,7 +204,7 @@ class Smarty else { // invalid file type, skipping - $this->_set_errorMsg("Invalid filetype for $filepath, skipping"); + $this->_set_error_msg("Invalid filetype for $filepath, skipping"); continue; } } @@ -206,7 +212,7 @@ class Smarty } else { - $this->_set_errorMsg("Directory \"$tpl_dir\" does not exist or is not a directory."); + $this->_set_error_msg("Directory \"$tpl_dir\" does not exist or is not a directory."); return false; } return true; @@ -234,7 +240,7 @@ class Smarty if(!file_exists($compile_dir)) if(!mkdir($compile_dir,0755)) { - $this->_set_errorMsg("problem creating directory \"$compile_dir\""); + $this->_set_error_msg("problem creating directory \"$compile_dir\""); return false; } // compile the template file if none exists or has been modified @@ -253,7 +259,7 @@ class Smarty } else { - $this->_set_errorMsg("problem matching \"$filepath.\""); + $this->_set_error_msg("problem matching \"$filepath.\""); return false; } return true; @@ -772,7 +778,7 @@ class Smarty { if(! ($fd = fopen($filename,"r"))) { - $this->_set_errorMsg("problem reading \"$filename.\""); + $this->_set_error_msg("problem reading \"$filename.\""); return false; } $contents = fread($fd,filesize($filename)); @@ -789,7 +795,7 @@ class Smarty { if(!($fd = fopen($filename,"w"))) { - $this->_set_errorMsg("problem writing \"$filename.\""); + $this->_set_error_msg("problem writing \"$filename.\""); return false; } fwrite($fd,$contents); @@ -798,13 +804,13 @@ class Smarty } /*======================================================================*\ - Function: _set_errorMsg() + Function: _set_error_msg() Purpose: set the error message \*======================================================================*/ - function _set_errorMsg($errorMsg) + function _set_error_msg($error_msg) { - $this->errorMsg="smarty error: $errorMsg"; + $this->_error_msg="smarty error: $error_msg"; return true; } diff --git a/templates/index.tpl b/templates/index.tpl index 3002fc9d..5f83ce96 100644 --- a/templates/index.tpl +++ b/templates/index.tpl @@ -1,9 +1,9 @@ {config_load file=test.conf} -Title: {#title#} +Title: {#title#|capitalize} {* A simple variable test *} -hello, my name is {$Name}. +hello, my name is {$Name} My interests are: {section name=outer loop=$FirstName}