*** empty log message ***

This commit is contained in:
andrey
2000-11-27 17:39:40 +00:00
parent 08d75708ce
commit dbfba87dae
4 changed files with 124 additions and 48 deletions

View File

@@ -39,7 +39,7 @@ class Smarty
var $left_delimiter = "{"; // template tag delimiters. var $left_delimiter = "{"; // template tag delimiters.
var $right_delimiter = "}"; var $right_delimiter = "}";
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'
); );
@@ -58,6 +58,7 @@ class Smarty
var $_error_msg = 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
var $_literal_blocks = array(); // keeps literal template blocks
/*======================================================================*\ /*======================================================================*\
@@ -275,15 +276,20 @@ class Smarty
//echo "compile dir: $compile_dir<br>\n"; //echo "compile dir: $compile_dir<br>\n";
//create directory if none exists //create directory if none exists
if(!file_exists($compile_dir)) if(!file_exists($compile_dir)) {
if(!mkdir($compile_dir,0755)) $compile_dir_parts = preg_split('!/+!', $compile_dir);
{ $new_dir = "";
$this->_set_error_msg("problem creating directory \"$compile_dir\""); foreach ($compile_dir_parts as $dir_part) {
return false; $new_dir .= $dir_part."/";
if (!file_exists($new_dir) && !mkdir($new_dir, 0755)) {
$this->_set_error_msg("problem creating directory \"$compile_dir\"");
return false;
}
} }
}
// compile the template file if none exists or has been modified // compile the template file if none exists or has been modified
/* TODO remove 1 from test */ if(!file_exists($compile_dir."/".$tpl_file_name) ||
if(!file_exists($compile_dir."/".$tpl_file_name) || 1 ||
($this->_modified_file($filepath,$compile_dir."/".$tpl_file_name))) ($this->_modified_file($filepath,$compile_dir."/".$tpl_file_name)))
{ {
if(!$this->_compile_file($filepath,$compile_dir."/".$tpl_file_name)) if(!$this->_compile_file($filepath,$compile_dir."/".$tpl_file_name))
@@ -328,6 +334,12 @@ class Smarty
$ldq = preg_quote($this->left_delimiter, "/"); $ldq = preg_quote($this->left_delimiter, "/");
$rdq = preg_quote($this->right_delimiter, "/"); $rdq = preg_quote($this->right_delimiter, "/");
/* Pull out the literal blocks. */
preg_match_all("!{$ldq}literal{$rdq}(.*?){$ldq}/literal{$rdq}!s", $template_contents, $match);
$this->_literal_blocks = $match[1];
$template_contents = preg_replace("!{$ldq}literal{$rdq}(.*?){$ldq}/literal{$rdq}!s",
'{literal}', $template_contents);
/* Gather all template tags. */ /* Gather all template tags. */
preg_match_all("/$ldq\s*(.*?)\s*$rdq/s", $template_contents, $match); preg_match_all("/$ldq\s*(.*?)\s*$rdq/s", $template_contents, $match);
$template_tags = $match[1]; $template_tags = $match[1];
@@ -421,6 +433,10 @@ class Smarty
case '/strip': case '/strip':
return $this->left_delimiter.$tag_command.$this->right_delimiter; return $this->left_delimiter.$tag_command.$this->right_delimiter;
case 'literal':
list (,$literal_block) = each($this->_literal_blocks);
return $literal_block;
default: default:
if (isset($this->custom_tags[$tag_command])) { if (isset($this->custom_tags[$tag_command])) {
return $this->_compile_custom_tag($tag_command, $tag_args); return $this->_compile_custom_tag($tag_command, $tag_args);
@@ -450,14 +466,16 @@ class Smarty
/* TODO syntax error: missing 'file' attribute */ /* TODO syntax error: missing 'file' attribute */
} }
if (empty($attrs['section'])) $output = '<?php include_once "Config_File.php";'."\n";
$section = 'NULL'; $output .= 'if (!is_object($_conf_obj) || get_class($_conf_obj) != "config_file") {'."\n";
else $output .= ' $_conf_obj = new Config_File("'.$this->config_dir.'");'."\n";
$section = '"'.$attrs['section'].'"'; $output .= '}'."\n";
$output .= '$_config = array_merge((array)$_config, $_conf_obj->get('.$attrs['file'].'));'."\n";
$output = '<?php if (!class_exists("Config_File")) { include "Config_File.php"; $conf = new Config_File("'.$this->config_dir."\"); }\n"; if (!empty($attrs['section']))
$output .= '$conf->load_file("'.$attrs['file']."\");\n"; $output .= '$_config = array_merge((array)$_config, $_conf_obj->get('.$attrs['file'].', '.$attrs['section'].')); ';
$output .= '$_config = array_merge((array)$_config, $conf->get("'.$attrs['file'].'", '.$section.")); ?>";
$output .= '?>';
return $output; return $output;
} }
@@ -469,7 +487,8 @@ class Smarty
if (empty($attrs['file'])) { if (empty($attrs['file'])) {
/* TODO syntax error: missing 'file' attribute */ /* TODO syntax error: missing 'file' attribute */
} } else
$attrs['file'] = $this->_dequote($attrs['file']);
return '<?php include "'.$this->template_dir.$this->compile_dir_ext.'/'.$attrs['file'].'"; ?>'; return '<?php include "'.$this->template_dir.$this->compile_dir_ext.'/'.$attrs['file'].'"; ?>';
} }
@@ -484,8 +503,8 @@ class Smarty
/* TODO syntax error: section needs a name */ /* TODO syntax error: section needs a name */
} }
$output .= "unset(\$_sections['$section_name']);\n"; $output .= "unset(\$_sections[$section_name]);\n";
$section_props = "\$_sections['$section_name']['properties']"; $section_props = "\$_sections[$section_name]['properties']";
foreach ($attrs as $attr_name => $attr_value) { foreach ($attrs as $attr_name => $attr_value) {
switch ($attr_name) { switch ($attr_name) {
@@ -723,6 +742,14 @@ class Smarty
/* 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 '$')
and not enclosed in single or double quotes
we single-quote it. */
if ($token{0} != '$' &&
!(($token{0} == '"' || $token[0] == "'") &&
$token{strlen($token)-1} == $token{0})) {
$token = "'".$token."'";
}
$attrs[$attr_name] = $token; $attrs[$attr_name] = $token;
$state = 0; $state = 0;
} else } else
@@ -828,14 +855,27 @@ class Smarty
$this->_parse_vars_props($modifier); $this->_parse_vars_props($modifier);
if (count($modifier) > 0) if (count($modifier) > 0)
$modifier_args = ", ".implode(', ', $modifier); $modifier_args = ', '.implode(', ', $modifier);
else else
$modifier_args = ""; $modifier_args = '';
$output = "$mod_func_name($output$modifier_args)"; $output = "$mod_func_name($output$modifier_args)";
} }
} }
/*======================================================================*\
Function: _dequote
Purpose: Remove starting and ending quotes from the string
\*======================================================================*/
function _dequote($string)
{
if (($string{0} == "'" || $string{0} == '"') &&
$string{strlen($string)-1} == $string{0})
return substr($string, 1, -1);
}
/*======================================================================*\ /*======================================================================*\
Function: _read_file() Function: _read_file()
Purpose: read in a file Purpose: read in a file

View File

@@ -1,12 +1,11 @@
{config_load file=test.conf} {config_load file=test.conf section=my foo}
Title: {#title#|capitalize} Title: {#title#|capitalize}
{$SCRIPT_NAME} {$SCRIPT_NAME}
{strip}
{* 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}
@@ -18,6 +17,5 @@ My interests are:
{sectionelse} {sectionelse}
none none
{/section} {/section}
{/strip}
{html_options output=$FirstName values=$LastName selected="Case"} {html_options output=$FirstName values=$LastName selected="Case"}

View File

@@ -39,7 +39,7 @@ class Smarty
var $left_delimiter = "{"; // template tag delimiters. var $left_delimiter = "{"; // template tag delimiters.
var $right_delimiter = "}"; var $right_delimiter = "}";
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'
); );
@@ -58,6 +58,7 @@ class Smarty
var $_error_msg = 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
var $_literal_blocks = array(); // keeps literal template blocks
/*======================================================================*\ /*======================================================================*\
@@ -275,15 +276,20 @@ class Smarty
//echo "compile dir: $compile_dir<br>\n"; //echo "compile dir: $compile_dir<br>\n";
//create directory if none exists //create directory if none exists
if(!file_exists($compile_dir)) if(!file_exists($compile_dir)) {
if(!mkdir($compile_dir,0755)) $compile_dir_parts = preg_split('!/+!', $compile_dir);
{ $new_dir = "";
$this->_set_error_msg("problem creating directory \"$compile_dir\""); foreach ($compile_dir_parts as $dir_part) {
return false; $new_dir .= $dir_part."/";
if (!file_exists($new_dir) && !mkdir($new_dir, 0755)) {
$this->_set_error_msg("problem creating directory \"$compile_dir\"");
return false;
}
} }
}
// compile the template file if none exists or has been modified // compile the template file if none exists or has been modified
/* TODO remove 1 from test */ if(!file_exists($compile_dir."/".$tpl_file_name) ||
if(!file_exists($compile_dir."/".$tpl_file_name) || 1 ||
($this->_modified_file($filepath,$compile_dir."/".$tpl_file_name))) ($this->_modified_file($filepath,$compile_dir."/".$tpl_file_name)))
{ {
if(!$this->_compile_file($filepath,$compile_dir."/".$tpl_file_name)) if(!$this->_compile_file($filepath,$compile_dir."/".$tpl_file_name))
@@ -328,6 +334,12 @@ class Smarty
$ldq = preg_quote($this->left_delimiter, "/"); $ldq = preg_quote($this->left_delimiter, "/");
$rdq = preg_quote($this->right_delimiter, "/"); $rdq = preg_quote($this->right_delimiter, "/");
/* Pull out the literal blocks. */
preg_match_all("!{$ldq}literal{$rdq}(.*?){$ldq}/literal{$rdq}!s", $template_contents, $match);
$this->_literal_blocks = $match[1];
$template_contents = preg_replace("!{$ldq}literal{$rdq}(.*?){$ldq}/literal{$rdq}!s",
'{literal}', $template_contents);
/* Gather all template tags. */ /* Gather all template tags. */
preg_match_all("/$ldq\s*(.*?)\s*$rdq/s", $template_contents, $match); preg_match_all("/$ldq\s*(.*?)\s*$rdq/s", $template_contents, $match);
$template_tags = $match[1]; $template_tags = $match[1];
@@ -421,6 +433,10 @@ class Smarty
case '/strip': case '/strip':
return $this->left_delimiter.$tag_command.$this->right_delimiter; return $this->left_delimiter.$tag_command.$this->right_delimiter;
case 'literal':
list (,$literal_block) = each($this->_literal_blocks);
return $literal_block;
default: default:
if (isset($this->custom_tags[$tag_command])) { if (isset($this->custom_tags[$tag_command])) {
return $this->_compile_custom_tag($tag_command, $tag_args); return $this->_compile_custom_tag($tag_command, $tag_args);
@@ -450,14 +466,16 @@ class Smarty
/* TODO syntax error: missing 'file' attribute */ /* TODO syntax error: missing 'file' attribute */
} }
if (empty($attrs['section'])) $output = '<?php include_once "Config_File.php";'."\n";
$section = 'NULL'; $output .= 'if (!is_object($_conf_obj) || get_class($_conf_obj) != "config_file") {'."\n";
else $output .= ' $_conf_obj = new Config_File("'.$this->config_dir.'");'."\n";
$section = '"'.$attrs['section'].'"'; $output .= '}'."\n";
$output .= '$_config = array_merge((array)$_config, $_conf_obj->get('.$attrs['file'].'));'."\n";
$output = '<?php if (!class_exists("Config_File")) { include "Config_File.php"; $conf = new Config_File("'.$this->config_dir."\"); }\n"; if (!empty($attrs['section']))
$output .= '$conf->load_file("'.$attrs['file']."\");\n"; $output .= '$_config = array_merge((array)$_config, $_conf_obj->get('.$attrs['file'].', '.$attrs['section'].')); ';
$output .= '$_config = array_merge((array)$_config, $conf->get("'.$attrs['file'].'", '.$section.")); ?>";
$output .= '?>';
return $output; return $output;
} }
@@ -469,7 +487,8 @@ class Smarty
if (empty($attrs['file'])) { if (empty($attrs['file'])) {
/* TODO syntax error: missing 'file' attribute */ /* TODO syntax error: missing 'file' attribute */
} } else
$attrs['file'] = $this->_dequote($attrs['file']);
return '<?php include "'.$this->template_dir.$this->compile_dir_ext.'/'.$attrs['file'].'"; ?>'; return '<?php include "'.$this->template_dir.$this->compile_dir_ext.'/'.$attrs['file'].'"; ?>';
} }
@@ -484,8 +503,8 @@ class Smarty
/* TODO syntax error: section needs a name */ /* TODO syntax error: section needs a name */
} }
$output .= "unset(\$_sections['$section_name']);\n"; $output .= "unset(\$_sections[$section_name]);\n";
$section_props = "\$_sections['$section_name']['properties']"; $section_props = "\$_sections[$section_name]['properties']";
foreach ($attrs as $attr_name => $attr_value) { foreach ($attrs as $attr_name => $attr_value) {
switch ($attr_name) { switch ($attr_name) {
@@ -723,6 +742,14 @@ class Smarty
/* 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 '$')
and not enclosed in single or double quotes
we single-quote it. */
if ($token{0} != '$' &&
!(($token{0} == '"' || $token[0] == "'") &&
$token{strlen($token)-1} == $token{0})) {
$token = "'".$token."'";
}
$attrs[$attr_name] = $token; $attrs[$attr_name] = $token;
$state = 0; $state = 0;
} else } else
@@ -828,14 +855,27 @@ class Smarty
$this->_parse_vars_props($modifier); $this->_parse_vars_props($modifier);
if (count($modifier) > 0) if (count($modifier) > 0)
$modifier_args = ", ".implode(', ', $modifier); $modifier_args = ', '.implode(', ', $modifier);
else else
$modifier_args = ""; $modifier_args = '';
$output = "$mod_func_name($output$modifier_args)"; $output = "$mod_func_name($output$modifier_args)";
} }
} }
/*======================================================================*\
Function: _dequote
Purpose: Remove starting and ending quotes from the string
\*======================================================================*/
function _dequote($string)
{
if (($string{0} == "'" || $string{0} == '"') &&
$string{strlen($string)-1} == $string{0})
return substr($string, 1, -1);
}
/*======================================================================*\ /*======================================================================*\
Function: _read_file() Function: _read_file()
Purpose: read in a file Purpose: read in a file

View File

@@ -1,12 +1,11 @@
{config_load file=test.conf} {config_load file=test.conf section=my foo}
Title: {#title#|capitalize} Title: {#title#|capitalize}
{$SCRIPT_NAME} {$SCRIPT_NAME}
{strip}
{* 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}
@@ -18,6 +17,5 @@ My interests are:
{sectionelse} {sectionelse}
none none
{/section} {/section}
{/strip}
{html_options output=$FirstName values=$LastName selected="Case"} {html_options output=$FirstName values=$LastName selected="Case"}