mirror of
https://github.com/smarty-php/smarty.git
synced 2025-08-05 10:54:27 +02:00
*** empty log message ***
This commit is contained in:
@@ -4,165 +4,54 @@
|
|||||||
* Project: Smarty: the PHP compiled template engine
|
* Project: Smarty: the PHP compiled template engine
|
||||||
* File: Smarty.functions.php
|
* File: Smarty.functions.php
|
||||||
* Author: Monte Ohrt <monte@ispi.net>
|
* Author: Monte Ohrt <monte@ispi.net>
|
||||||
|
* Andrei Zmievski <andrei@ispi.net>
|
||||||
*
|
*
|
||||||
* 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
|
Function: smarty_mod_escape
|
||||||
Purpose: html escape template vars
|
Purpose: Escape the string according to escapement type
|
||||||
\*======================================================================*/
|
\*======================================================================*/
|
||||||
|
function smarty_mod_escape($string, $esc_type = 'html')
|
||||||
function smarty_htmlesc($var)
|
|
||||||
{
|
{
|
||||||
print htmlspecialchars($var);
|
switch ($esc_type) {
|
||||||
}
|
case 'html':
|
||||||
|
return htmlspecialchars($string);
|
||||||
|
|
||||||
/*======================================================================*\
|
case 'url':
|
||||||
Function: urlesc
|
return urlencode($string);
|
||||||
Purpose: URL escape template vars
|
|
||||||
\*======================================================================*/
|
|
||||||
|
|
||||||
function smarty_urlesc($var)
|
default:
|
||||||
{
|
return $string;
|
||||||
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 ("<!-- problem reading \"$config_file.\" -->");
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
$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
|
Function: smarty_mod_truncate
|
||||||
Purpose: clear config vars
|
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_mod_truncate($string, $length = 80, $etc = '...', $break_words = false)
|
||||||
function smarty_configclear()
|
|
||||||
{
|
{
|
||||||
|
if (strlen($string) > $length) {
|
||||||
global $_config_vars;
|
$length -= strlen($etc);
|
||||||
$_config_vars = array();
|
$fragment = substr($string, 0, $length+1);
|
||||||
|
if ($break_words)
|
||||||
return true;
|
$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 = ' ')
|
||||||
{
|
{
|
||||||
|
return implode($spacify_char, preg_split('//', $string, -1, PREG_SPLIT_NO_EMPTY));
|
||||||
global $_config_vars;
|
|
||||||
|
|
||||||
if (isset($_config_vars[$var]))
|
|
||||||
print $_config_vars[$var];
|
|
||||||
else
|
|
||||||
print "<!-- no value for $var in $config_file -->";
|
|
||||||
|
|
||||||
return true;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*======================================================================*\
|
|
||||||
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;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
@@ -43,6 +43,7 @@ class Smarty
|
|||||||
|
|
||||||
// registered template functions
|
// registered template functions
|
||||||
// NOTE: leave off the "smarty_" prefix on the actual PHP function name
|
// NOTE: leave off the "smarty_" prefix on the actual PHP function name
|
||||||
|
/*
|
||||||
var $registered_functions = array( "htmlesc",
|
var $registered_functions = array( "htmlesc",
|
||||||
"urlesc",
|
"urlesc",
|
||||||
"default",
|
"default",
|
||||||
@@ -51,13 +52,18 @@ class Smarty
|
|||||||
"configprint",
|
"configprint",
|
||||||
"configset"
|
"configset"
|
||||||
);
|
);
|
||||||
|
*/
|
||||||
|
|
||||||
var $_modifiers = array( "lower" => "smarty_mod_lower",
|
var $_modifiers = array( 'lower' => 'strtolower',
|
||||||
"upper" => "smarty_mod_upper"
|
'upper' => 'strtoupper',
|
||||||
|
'capitalize' => 'ucwords',
|
||||||
|
'escape' => 'smarty_mod_escape',
|
||||||
|
'truncate' => 'smarty_mod_truncate',
|
||||||
|
'spacify' => 'smarty_mod_spacify'
|
||||||
);
|
);
|
||||||
|
|
||||||
// internal vars
|
// internal vars
|
||||||
var $errorMsg = false; // error messages
|
var $_error_msg = false; // error messages
|
||||||
var $_tpl_vars = array();
|
var $_tpl_vars = array();
|
||||||
var $_sectionelse_stack = array(); // keeps track of whether section had 'else' part
|
var $_sectionelse_stack = array(); // keeps track of whether section had 'else' part
|
||||||
|
|
||||||
@@ -162,7 +168,7 @@ class Smarty
|
|||||||
// exit if recursion depth is met
|
// exit if recursion depth is met
|
||||||
if($this->max_recursion_depth != 0 && $depth >= $this->max_recursion_depth)
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
if(is_dir($tpl_dir))
|
if(is_dir($tpl_dir))
|
||||||
@@ -198,7 +204,7 @@ class Smarty
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
// invalid file type, skipping
|
// invalid file type, skipping
|
||||||
$this->_set_errorMsg("Invalid filetype for $filepath, skipping");
|
$this->_set_error_msg("Invalid filetype for $filepath, skipping");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -206,7 +212,7 @@ class Smarty
|
|||||||
}
|
}
|
||||||
else
|
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 false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@@ -234,7 +240,7 @@ class Smarty
|
|||||||
if(!file_exists($compile_dir))
|
if(!file_exists($compile_dir))
|
||||||
if(!mkdir($compile_dir,0755))
|
if(!mkdir($compile_dir,0755))
|
||||||
{
|
{
|
||||||
$this->_set_errorMsg("problem creating directory \"$compile_dir\"");
|
$this->_set_error_msg("problem creating directory \"$compile_dir\"");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// compile the template file if none exists or has been modified
|
// compile the template file if none exists or has been modified
|
||||||
@@ -253,7 +259,7 @@ class Smarty
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
$this->_set_errorMsg("problem matching \"$filepath.\"");
|
$this->_set_error_msg("problem matching \"$filepath.\"");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@@ -772,7 +778,7 @@ class Smarty
|
|||||||
{
|
{
|
||||||
if(! ($fd = fopen($filename,"r")))
|
if(! ($fd = fopen($filename,"r")))
|
||||||
{
|
{
|
||||||
$this->_set_errorMsg("problem reading \"$filename.\"");
|
$this->_set_error_msg("problem reading \"$filename.\"");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$contents = fread($fd,filesize($filename));
|
$contents = fread($fd,filesize($filename));
|
||||||
@@ -789,7 +795,7 @@ class Smarty
|
|||||||
{
|
{
|
||||||
if(!($fd = fopen($filename,"w")))
|
if(!($fd = fopen($filename,"w")))
|
||||||
{
|
{
|
||||||
$this->_set_errorMsg("problem writing \"$filename.\"");
|
$this->_set_error_msg("problem writing \"$filename.\"");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
fwrite($fd,$contents);
|
fwrite($fd,$contents);
|
||||||
@@ -798,13 +804,13 @@ class Smarty
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*======================================================================*\
|
/*======================================================================*\
|
||||||
Function: _set_errorMsg()
|
Function: _set_error_msg()
|
||||||
Purpose: set the error message
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -4,7 +4,7 @@ require("Smarty.class.php");
|
|||||||
|
|
||||||
$smarty = new Smarty;
|
$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("FirstName",array("John","Mary","James","Henry"));
|
||||||
$smarty->assign("LastName",array("Doe","Smith","Johnson","Case"));
|
$smarty->assign("LastName",array("Doe","Smith","Johnson","Case"));
|
||||||
$smarty->assign("Class",array(array("A","B","C","D"), array("E", "F", "G", "H"),
|
$smarty->assign("Class",array(array("A","B","C","D"), array("E", "F", "G", "H"),
|
||||||
|
@@ -1,9 +1,9 @@
|
|||||||
{config_load file=test.conf}
|
{config_load file=test.conf}
|
||||||
|
|
||||||
Title: {#title#}
|
Title: {#title#|capitalize}
|
||||||
|
|
||||||
{* A simple variable test *}
|
{* A simple variable test *}
|
||||||
hello, my name is {$Name}.
|
hello, my name is {$Name}
|
||||||
|
|
||||||
My interests are:
|
My interests are:
|
||||||
{section name=outer loop=$FirstName}
|
{section name=outer loop=$FirstName}
|
||||||
|
@@ -4,7 +4,7 @@ require("Smarty.class.php");
|
|||||||
|
|
||||||
$smarty = new Smarty;
|
$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("FirstName",array("John","Mary","James","Henry"));
|
||||||
$smarty->assign("LastName",array("Doe","Smith","Johnson","Case"));
|
$smarty->assign("LastName",array("Doe","Smith","Johnson","Case"));
|
||||||
$smarty->assign("Class",array(array("A","B","C","D"), array("E", "F", "G", "H"),
|
$smarty->assign("Class",array(array("A","B","C","D"), array("E", "F", "G", "H"),
|
||||||
|
@@ -43,6 +43,7 @@ class Smarty
|
|||||||
|
|
||||||
// registered template functions
|
// registered template functions
|
||||||
// NOTE: leave off the "smarty_" prefix on the actual PHP function name
|
// NOTE: leave off the "smarty_" prefix on the actual PHP function name
|
||||||
|
/*
|
||||||
var $registered_functions = array( "htmlesc",
|
var $registered_functions = array( "htmlesc",
|
||||||
"urlesc",
|
"urlesc",
|
||||||
"default",
|
"default",
|
||||||
@@ -51,13 +52,18 @@ class Smarty
|
|||||||
"configprint",
|
"configprint",
|
||||||
"configset"
|
"configset"
|
||||||
);
|
);
|
||||||
|
*/
|
||||||
|
|
||||||
var $_modifiers = array( "lower" => "smarty_mod_lower",
|
var $_modifiers = array( 'lower' => 'strtolower',
|
||||||
"upper" => "smarty_mod_upper"
|
'upper' => 'strtoupper',
|
||||||
|
'capitalize' => 'ucwords',
|
||||||
|
'escape' => 'smarty_mod_escape',
|
||||||
|
'truncate' => 'smarty_mod_truncate',
|
||||||
|
'spacify' => 'smarty_mod_spacify'
|
||||||
);
|
);
|
||||||
|
|
||||||
// internal vars
|
// internal vars
|
||||||
var $errorMsg = false; // error messages
|
var $_error_msg = false; // error messages
|
||||||
var $_tpl_vars = array();
|
var $_tpl_vars = array();
|
||||||
var $_sectionelse_stack = array(); // keeps track of whether section had 'else' part
|
var $_sectionelse_stack = array(); // keeps track of whether section had 'else' part
|
||||||
|
|
||||||
@@ -162,7 +168,7 @@ class Smarty
|
|||||||
// exit if recursion depth is met
|
// exit if recursion depth is met
|
||||||
if($this->max_recursion_depth != 0 && $depth >= $this->max_recursion_depth)
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
if(is_dir($tpl_dir))
|
if(is_dir($tpl_dir))
|
||||||
@@ -198,7 +204,7 @@ class Smarty
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
// invalid file type, skipping
|
// invalid file type, skipping
|
||||||
$this->_set_errorMsg("Invalid filetype for $filepath, skipping");
|
$this->_set_error_msg("Invalid filetype for $filepath, skipping");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -206,7 +212,7 @@ class Smarty
|
|||||||
}
|
}
|
||||||
else
|
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 false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@@ -234,7 +240,7 @@ class Smarty
|
|||||||
if(!file_exists($compile_dir))
|
if(!file_exists($compile_dir))
|
||||||
if(!mkdir($compile_dir,0755))
|
if(!mkdir($compile_dir,0755))
|
||||||
{
|
{
|
||||||
$this->_set_errorMsg("problem creating directory \"$compile_dir\"");
|
$this->_set_error_msg("problem creating directory \"$compile_dir\"");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// compile the template file if none exists or has been modified
|
// compile the template file if none exists or has been modified
|
||||||
@@ -253,7 +259,7 @@ class Smarty
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
$this->_set_errorMsg("problem matching \"$filepath.\"");
|
$this->_set_error_msg("problem matching \"$filepath.\"");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@@ -772,7 +778,7 @@ class Smarty
|
|||||||
{
|
{
|
||||||
if(! ($fd = fopen($filename,"r")))
|
if(! ($fd = fopen($filename,"r")))
|
||||||
{
|
{
|
||||||
$this->_set_errorMsg("problem reading \"$filename.\"");
|
$this->_set_error_msg("problem reading \"$filename.\"");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$contents = fread($fd,filesize($filename));
|
$contents = fread($fd,filesize($filename));
|
||||||
@@ -789,7 +795,7 @@ class Smarty
|
|||||||
{
|
{
|
||||||
if(!($fd = fopen($filename,"w")))
|
if(!($fd = fopen($filename,"w")))
|
||||||
{
|
{
|
||||||
$this->_set_errorMsg("problem writing \"$filename.\"");
|
$this->_set_error_msg("problem writing \"$filename.\"");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
fwrite($fd,$contents);
|
fwrite($fd,$contents);
|
||||||
@@ -798,13 +804,13 @@ class Smarty
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*======================================================================*\
|
/*======================================================================*\
|
||||||
Function: _set_errorMsg()
|
Function: _set_error_msg()
|
||||||
Purpose: set the error message
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,9 +1,9 @@
|
|||||||
{config_load file=test.conf}
|
{config_load file=test.conf}
|
||||||
|
|
||||||
Title: {#title#}
|
Title: {#title#|capitalize}
|
||||||
|
|
||||||
{* A simple variable test *}
|
{* A simple variable test *}
|
||||||
hello, my name is {$Name}.
|
hello, my name is {$Name}
|
||||||
|
|
||||||
My interests are:
|
My interests are:
|
||||||
{section name=outer loop=$FirstName}
|
{section name=outer loop=$FirstName}
|
||||||
|
Reference in New Issue
Block a user