*** empty log message ***

This commit is contained in:
andrey
2000-12-04 21:48:51 +00:00
parent dbfba87dae
commit 977334922f
5 changed files with 203 additions and 75 deletions

View File

@@ -8,6 +8,26 @@
* *
*/ */
/*============================================*\
Modifiers
\*============================================*/
function _smarty_mod_handler()
{
$args = func_get_args();
$func_name = array_shift($args);
$var = $args[0];
if (is_array($var)) {
foreach ($var as $key => $val) {
$args[0] = $val;
$var[$key] = call_user_func_array($func_name, $args);
}
return $var;
} else
return call_user_func_array($func_name, $args);
}
/*======================================================================*\ /*======================================================================*\
Function: smarty_mod_escape Function: smarty_mod_escape
@@ -55,17 +75,31 @@ function smarty_mod_spacify($string, $spacify_char = ' ')
} }
function smarty_mod_date_format($string, $format)
{
return strftime($format, $string);
}
function smarty_mod_replace($string, $search, $replace)
{
return str_replace($search, $replace, $string);
}
/*============================================*\ /*============================================*\
Custom tag functions Custom tag functions
\*============================================*/ \*============================================*/
/*======================================================================*\ /*======================================================================*\
Function: smarty_func_html_options Function: smarty_func_html_options
Purpose: Returns the list of <option> tags generated from Purpose: Prints the list of <option> tags generated from
the passed parameters the passed parameters
\*======================================================================*/ \*======================================================================*/
function smarty_func_html_options() function smarty_func_html_options()
{ {
$print_result = true;
extract(func_get_arg(0)); extract(func_get_arg(0));
settype($output, 'array'); settype($output, 'array');
@@ -88,6 +122,76 @@ function smarty_func_html_options()
$html_result .= ">".$output[$i]."</option>\n"; $html_result .= ">".$output[$i]."</option>\n";
} }
if ($print_result)
print $html_result;
else
return $html_result;
}
/*======================================================================*\
Function: smarty_func_html_select_date
Purpose: Prints the dropdowns for date selection.
\*======================================================================*/
function smarty_func_html_select_date()
{
/* Default values. */
$prefix = "Date_";
$time = time();
$start_year = strftime("%Y");
$end_year = $start_year;
$display_days = true;
$display_months = true;
$display_years = true;
$month_format = "%B";
$day_format = "%02d";
$year_as_text = false;
extract(func_get_arg(0));
$html_result = "";
if ($display_months) {
$month_names = array();
for ($i = 1; $i <= 12; $i++)
$month_names[] = strftime($month_format, mktime(0, 0, 0, $i, 1, 2000));
$html_result .= '<select name="'.$date_prefix.'Month">'."\n";
$html_result .= smarty_func_html_options(array('output' => $month_names,
'values' => range(1, 12),
'selected' => strftime("%m", $time),
'print_result' => false));
$html_result .= "</select>\n";
}
if ($display_days) {
$days = range(1, 31);
array_walk($days, create_function('&$x', '$x = sprintf("'.$day_format.'", $x);'));
$html_result .= '<select name="'.$date_prefix.'Day">'."\n";
$html_result .= smarty_func_html_options(array('output' => $days,
'values' => range(1, 31),
'selected' => strftime("%d", $time),
'print_result' => false));
$html_result .= "</select>\n";
}
if ($display_years) {
if ($year_as_text) {
$html_result .= '<input type="text" name="'.$date_prefix.'Year" value="'.strftime('%Y', $time).'" size="4" maxlength="4">';
} else {
$years = range($start_year, $end_year);
$html_result .= '<select name="'.$date_prefix.'Year">'."\n";
$html_result .= smarty_func_html_options(array('output' => $years,
'values' => $years,
'selected' => strftime("%Y", $time),
'print_result' => false));
$html_result .= "</select>\n";
}
}
print $html_result; print $html_result;
} }

View File

@@ -41,7 +41,8 @@ class Smarty
var $config_dir = "./configs"; // directory where config files are located var $config_dir = "./configs"; // directory where config files are located
var $custom_tags = array( 'html_options' => 'smarty_func_html_options' var $custom_tags = array( 'html_options' => 'smarty_func_html_options',
'html_select_date' => 'smarty_func_html_select_date'
); );
var $modifiers = array( 'lower' => 'strtolower', var $modifiers = array( 'lower' => 'strtolower',
@@ -49,7 +50,9 @@ class Smarty
'capitalize' => 'ucwords', 'capitalize' => 'ucwords',
'escape' => 'smarty_mod_escape', 'escape' => 'smarty_mod_escape',
'truncate' => 'smarty_mod_truncate', 'truncate' => 'smarty_mod_truncate',
'spacify' => 'smarty_mod_spacify' 'spacify' => 'smarty_mod_spacify',
'date_format' => 'smarty_mod_date_format',
'replace' => 'smarty_mod_replace'
); );
var $global_assign = array( 'SCRIPT_NAME' var $global_assign = array( 'SCRIPT_NAME'
); );
@@ -362,7 +365,8 @@ class Smarty
/* Reformat data between 'strip' and '/strip' tags, removing spaces, tabs and newlines. */ /* Reformat data between 'strip' and '/strip' tags, removing spaces, tabs and newlines. */
if (preg_match_all("!{$ldq}strip{$rdq}.*?{$ldq}/strip{$rdq}!s", $compiled_contents, $match)) { if (preg_match_all("!{$ldq}strip{$rdq}.*?{$ldq}/strip{$rdq}!s", $compiled_contents, $match)) {
$strip_tags = $match[0]; $strip_tags = $match[0];
$strip_tags_modified = preg_replace("!$ldq/?strip$rdq|[\t ]+$|^[\t ]+|/[\r\n]+!m", '', $strip_tags); $strip_tags_modified = preg_replace("!$ldq/?strip$rdq|[\t ]+$|^[\t ]+!m", '', $strip_tags);
$strip_tags_modified = preg_replace('![\r\n]+!m', ' ', $strip_tags_modified);
for ($i = 0; $i < count($strip_tags); $i++) for ($i = 0; $i < count($strip_tags); $i++)
$compiled_contents = preg_replace("!{$ldq}strip{$rdq}.*?{$ldq}/strip{$rdq}!s", $compiled_contents = preg_replace("!{$ldq}strip{$rdq}.*?{$ldq}/strip{$rdq}!s",
$strip_tags_modified[$i], $compiled_contents, 1); $strip_tags_modified[$i], $compiled_contents, 1);
@@ -403,6 +407,9 @@ class Smarty
case 'else': case 'else':
return '<?php else: ?>'; return '<?php else: ?>';
case 'elseif':
return $this->_compile_if_tag($tag_args, true);
case '/if': case '/if':
return '<?php endif; ?>'; return '<?php endif; ?>';
@@ -451,10 +458,13 @@ class Smarty
{ {
$attrs = $this->_parse_attrs($tag_args); $attrs = $this->_parse_attrs($tag_args);
$function = $this->custom_tags[$tag_command]; $function = $this->custom_tags[$tag_command];
foreach ($attrs as $arg_name => $arg_value) foreach ($attrs as $arg_name => $arg_value) {
if (is_bool($arg_value))
$arg_value = $arg_value ? 'true' : 'false';
$arg_list[] = "'$arg_name' => $arg_value"; $arg_list[] = "'$arg_name' => $arg_value";
}
return "<?php $function(array(".implode(',', $arg_list).")); ?>"; return "<?php $function(array(".implode(',', (array)$arg_list).")); ?>";
} }
@@ -493,9 +503,9 @@ class Smarty
return '<?php include "'.$this->template_dir.$this->compile_dir_ext.'/'.$attrs['file'].'"; ?>'; return '<?php include "'.$this->template_dir.$this->compile_dir_ext.'/'.$attrs['file'].'"; ?>';
} }
function _compile_section_start($tokens) function _compile_section_start($tag_args)
{ {
$attrs = $this->_parse_attrs($tokens); $attrs = $this->_parse_attrs($tag_args);
$output = "<?php\n"; $output = "<?php\n";
$section_name = $attrs['name']; $section_name = $attrs['name'];
@@ -509,11 +519,7 @@ class Smarty
foreach ($attrs as $attr_name => $attr_value) { foreach ($attrs as $attr_name => $attr_value) {
switch ($attr_name) { switch ($attr_name) {
case 'loop': case 'loop':
$output .= " $output .= "{$section_props}['loop'] = count($attr_value);\n";
if (is_array($attr_value))
{$section_props}['loop'] = count($attr_value);
else
{$section_props}['loop'] = $attr_value;\n";
break; break;
default: default:
@@ -544,7 +550,7 @@ class Smarty
return $output; return $output;
} }
function _compile_if_tag($tag_args) function _compile_if_tag($tag_args, $elseif = false)
{ {
/* Tokenize args for 'if' tag. */ /* Tokenize args for 'if' tag. */
preg_match_all('/(?: preg_match_all('/(?:
@@ -638,7 +644,10 @@ class Smarty
} }
} }
return '<?php if ('.implode(' ', $tokens).'): ?>'; if ($elseif)
return '<?php elseif ('.implode(' ', $tokens).'): ?>';
else
return '<?php if ('.implode(' ', $tokens).'): ?>';
} }
function _parse_is_expr($is_arg, $tokens) function _parse_is_expr($is_arg, $tokens)
@@ -694,15 +703,13 @@ class Smarty
function _parse_attrs($tag_args) function _parse_attrs($tag_args)
{ {
/* Tokenize tag attributes. */ /* Tokenize tag attributes. */
preg_match_all('/(?: preg_match_all('/(?:"[^"\\\\]*(?:\\\\.[^"\\\\]*)*" |
"[^"\\\\]*(?:\\\\.[^"\\\\]*)*" | # match all double quoted strings allowed escaped double quotes \'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\' | (?>[^"\'= ]+)
\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\' | # match all single quoted strings allowed escaped single quotes )+ |
[=] | # match equal sign [=]
[^"\'\s=]+ # match any other token that is not any of the above /x', $tag_args, $match);
)/x', $tag_args, $match);
$tokens = $match[0]; $tokens = $match[0];
$this->_parse_vars_props($tokens);
$attrs = array(); $attrs = array();
/* Parse state: /* Parse state:
0 - expecting attr name 0 - expecting attr name
@@ -729,27 +736,30 @@ class Smarty
current token and don't switch state. current token and don't switch state.
If the token is '=', then we go to state 2. */ If the token is '=', then we go to state 2. */
if (preg_match('!\w+!', $token)) { if ($token == '=') {
$attrs[$attr_name] = "";
$attr_name = $token;
} else if ($token == '=') {
$state = 2; $state = 2;
} else } else
/* TODO syntax error: expecting attr name or '=' */; /* TODO syntax error: expecting '=' */;
break; break;
case 2: case 2:
/* If token is not '=', we set the attribute value and go to /* If token is not '=', we set the attribute value and go to
state 0. */ state 0. */
if ($token != '=') { if ($token != '=') {
/* If the token is not variable (doesn't start with '$') /* We booleanize the token if it's a non-quoted possible
and not enclosed in single or double quotes boolean value. */
we single-quote it. */ if (preg_match('!^(on|yes|true)$!', $token))
if ($token{0} != '$' && $token = true;
!(($token{0} == '"' || $token[0] == "'") && else if (preg_match('!^(off|no|false)$!', $token))
$token{strlen($token)-1} == $token{0})) { $token = false;
/* If the token is not variable (doesn't start with
'$', '#', or '%') and not enclosed in single or
double quotes we single-quote it. */
else if (!in_array($token{0}, array('$', '#', '%')) &&
!(($token{0} == '"' || $token[0] == "'") &&
$token{strlen($token)-1} == $token{0}))
$token = "'".$token."'"; $token = "'".$token."'";
}
$attrs[$attr_name] = $token; $attrs[$attr_name] = $token;
$state = 0; $state = 0;
} else } else
@@ -758,6 +768,8 @@ class Smarty
} }
} }
$this->_parse_vars_props($attrs);
return $attrs; return $attrs;
} }
@@ -859,7 +871,7 @@ class Smarty
else else
$modifier_args = ''; $modifier_args = '';
$output = "$mod_func_name($output$modifier_args)"; $output = "_smarty_mod_handler('$mod_func_name', $output$modifier_args)";
} }
} }

View File

@@ -1,11 +1,11 @@
{config_load file=test.conf section=my foo} {config_load file=test.conf section="my foo"}
Title: {#title#|capitalize} Title: {#title#|capitalize}
{$SCRIPT_NAME} {$SCRIPT_NAME}
{* A simple variable test *} {* A simple variable test *}
hello, my name is {$Name} hello, my name is {$Name|upper}
My interests are: My interests are:
{section name=outer loop=$FirstName} {section name=outer loop=$FirstName}
@@ -18,4 +18,4 @@ My interests are:
none none
{/section} {/section}
{html_options output=$FirstName values=$LastName selected="Case"} {html_options values=$LastName output=$FirstName|replace:"a":"@"}

View File

@@ -41,7 +41,8 @@ class Smarty
var $config_dir = "./configs"; // directory where config files are located var $config_dir = "./configs"; // directory where config files are located
var $custom_tags = array( 'html_options' => 'smarty_func_html_options' var $custom_tags = array( 'html_options' => 'smarty_func_html_options',
'html_select_date' => 'smarty_func_html_select_date'
); );
var $modifiers = array( 'lower' => 'strtolower', var $modifiers = array( 'lower' => 'strtolower',
@@ -49,7 +50,9 @@ class Smarty
'capitalize' => 'ucwords', 'capitalize' => 'ucwords',
'escape' => 'smarty_mod_escape', 'escape' => 'smarty_mod_escape',
'truncate' => 'smarty_mod_truncate', 'truncate' => 'smarty_mod_truncate',
'spacify' => 'smarty_mod_spacify' 'spacify' => 'smarty_mod_spacify',
'date_format' => 'smarty_mod_date_format',
'replace' => 'smarty_mod_replace'
); );
var $global_assign = array( 'SCRIPT_NAME' var $global_assign = array( 'SCRIPT_NAME'
); );
@@ -362,7 +365,8 @@ class Smarty
/* Reformat data between 'strip' and '/strip' tags, removing spaces, tabs and newlines. */ /* Reformat data between 'strip' and '/strip' tags, removing spaces, tabs and newlines. */
if (preg_match_all("!{$ldq}strip{$rdq}.*?{$ldq}/strip{$rdq}!s", $compiled_contents, $match)) { if (preg_match_all("!{$ldq}strip{$rdq}.*?{$ldq}/strip{$rdq}!s", $compiled_contents, $match)) {
$strip_tags = $match[0]; $strip_tags = $match[0];
$strip_tags_modified = preg_replace("!$ldq/?strip$rdq|[\t ]+$|^[\t ]+|/[\r\n]+!m", '', $strip_tags); $strip_tags_modified = preg_replace("!$ldq/?strip$rdq|[\t ]+$|^[\t ]+!m", '', $strip_tags);
$strip_tags_modified = preg_replace('![\r\n]+!m', ' ', $strip_tags_modified);
for ($i = 0; $i < count($strip_tags); $i++) for ($i = 0; $i < count($strip_tags); $i++)
$compiled_contents = preg_replace("!{$ldq}strip{$rdq}.*?{$ldq}/strip{$rdq}!s", $compiled_contents = preg_replace("!{$ldq}strip{$rdq}.*?{$ldq}/strip{$rdq}!s",
$strip_tags_modified[$i], $compiled_contents, 1); $strip_tags_modified[$i], $compiled_contents, 1);
@@ -403,6 +407,9 @@ class Smarty
case 'else': case 'else':
return '<?php else: ?>'; return '<?php else: ?>';
case 'elseif':
return $this->_compile_if_tag($tag_args, true);
case '/if': case '/if':
return '<?php endif; ?>'; return '<?php endif; ?>';
@@ -451,10 +458,13 @@ class Smarty
{ {
$attrs = $this->_parse_attrs($tag_args); $attrs = $this->_parse_attrs($tag_args);
$function = $this->custom_tags[$tag_command]; $function = $this->custom_tags[$tag_command];
foreach ($attrs as $arg_name => $arg_value) foreach ($attrs as $arg_name => $arg_value) {
if (is_bool($arg_value))
$arg_value = $arg_value ? 'true' : 'false';
$arg_list[] = "'$arg_name' => $arg_value"; $arg_list[] = "'$arg_name' => $arg_value";
}
return "<?php $function(array(".implode(',', $arg_list).")); ?>"; return "<?php $function(array(".implode(',', (array)$arg_list).")); ?>";
} }
@@ -493,9 +503,9 @@ class Smarty
return '<?php include "'.$this->template_dir.$this->compile_dir_ext.'/'.$attrs['file'].'"; ?>'; return '<?php include "'.$this->template_dir.$this->compile_dir_ext.'/'.$attrs['file'].'"; ?>';
} }
function _compile_section_start($tokens) function _compile_section_start($tag_args)
{ {
$attrs = $this->_parse_attrs($tokens); $attrs = $this->_parse_attrs($tag_args);
$output = "<?php\n"; $output = "<?php\n";
$section_name = $attrs['name']; $section_name = $attrs['name'];
@@ -509,11 +519,7 @@ class Smarty
foreach ($attrs as $attr_name => $attr_value) { foreach ($attrs as $attr_name => $attr_value) {
switch ($attr_name) { switch ($attr_name) {
case 'loop': case 'loop':
$output .= " $output .= "{$section_props}['loop'] = count($attr_value);\n";
if (is_array($attr_value))
{$section_props}['loop'] = count($attr_value);
else
{$section_props}['loop'] = $attr_value;\n";
break; break;
default: default:
@@ -544,7 +550,7 @@ class Smarty
return $output; return $output;
} }
function _compile_if_tag($tag_args) function _compile_if_tag($tag_args, $elseif = false)
{ {
/* Tokenize args for 'if' tag. */ /* Tokenize args for 'if' tag. */
preg_match_all('/(?: preg_match_all('/(?:
@@ -638,7 +644,10 @@ class Smarty
} }
} }
return '<?php if ('.implode(' ', $tokens).'): ?>'; if ($elseif)
return '<?php elseif ('.implode(' ', $tokens).'): ?>';
else
return '<?php if ('.implode(' ', $tokens).'): ?>';
} }
function _parse_is_expr($is_arg, $tokens) function _parse_is_expr($is_arg, $tokens)
@@ -694,15 +703,13 @@ class Smarty
function _parse_attrs($tag_args) function _parse_attrs($tag_args)
{ {
/* Tokenize tag attributes. */ /* Tokenize tag attributes. */
preg_match_all('/(?: preg_match_all('/(?:"[^"\\\\]*(?:\\\\.[^"\\\\]*)*" |
"[^"\\\\]*(?:\\\\.[^"\\\\]*)*" | # match all double quoted strings allowed escaped double quotes \'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\' | (?>[^"\'= ]+)
\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\' | # match all single quoted strings allowed escaped single quotes )+ |
[=] | # match equal sign [=]
[^"\'\s=]+ # match any other token that is not any of the above /x', $tag_args, $match);
)/x', $tag_args, $match);
$tokens = $match[0]; $tokens = $match[0];
$this->_parse_vars_props($tokens);
$attrs = array(); $attrs = array();
/* Parse state: /* Parse state:
0 - expecting attr name 0 - expecting attr name
@@ -729,27 +736,30 @@ class Smarty
current token and don't switch state. current token and don't switch state.
If the token is '=', then we go to state 2. */ If the token is '=', then we go to state 2. */
if (preg_match('!\w+!', $token)) { if ($token == '=') {
$attrs[$attr_name] = "";
$attr_name = $token;
} else if ($token == '=') {
$state = 2; $state = 2;
} else } else
/* TODO syntax error: expecting attr name or '=' */; /* TODO syntax error: expecting '=' */;
break; break;
case 2: case 2:
/* If token is not '=', we set the attribute value and go to /* If token is not '=', we set the attribute value and go to
state 0. */ state 0. */
if ($token != '=') { if ($token != '=') {
/* If the token is not variable (doesn't start with '$') /* We booleanize the token if it's a non-quoted possible
and not enclosed in single or double quotes boolean value. */
we single-quote it. */ if (preg_match('!^(on|yes|true)$!', $token))
if ($token{0} != '$' && $token = true;
!(($token{0} == '"' || $token[0] == "'") && else if (preg_match('!^(off|no|false)$!', $token))
$token{strlen($token)-1} == $token{0})) { $token = false;
/* If the token is not variable (doesn't start with
'$', '#', or '%') and not enclosed in single or
double quotes we single-quote it. */
else if (!in_array($token{0}, array('$', '#', '%')) &&
!(($token{0} == '"' || $token[0] == "'") &&
$token{strlen($token)-1} == $token{0}))
$token = "'".$token."'"; $token = "'".$token."'";
}
$attrs[$attr_name] = $token; $attrs[$attr_name] = $token;
$state = 0; $state = 0;
} else } else
@@ -758,6 +768,8 @@ class Smarty
} }
} }
$this->_parse_vars_props($attrs);
return $attrs; return $attrs;
} }
@@ -859,7 +871,7 @@ class Smarty
else else
$modifier_args = ''; $modifier_args = '';
$output = "$mod_func_name($output$modifier_args)"; $output = "_smarty_mod_handler('$mod_func_name', $output$modifier_args)";
} }
} }

View File

@@ -1,11 +1,11 @@
{config_load file=test.conf section=my foo} {config_load file=test.conf section="my foo"}
Title: {#title#|capitalize} Title: {#title#|capitalize}
{$SCRIPT_NAME} {$SCRIPT_NAME}
{* A simple variable test *} {* A simple variable test *}
hello, my name is {$Name} hello, my name is {$Name|upper}
My interests are: My interests are:
{section name=outer loop=$FirstName} {section name=outer loop=$FirstName}
@@ -18,4 +18,4 @@ My interests are:
none none
{/section} {/section}
{html_options output=$FirstName values=$LastName selected="Case"} {html_options values=$LastName output=$FirstName|replace:"a":"@"}