Commit ccdf702f authored by Marius Cramer's avatar Marius Cramer

Changed: Template class tag processing. Tags can now contain any amount of attributes.

parent daf67b7c
......@@ -864,7 +864,7 @@ if (!defined('vlibTemplateClassLoaded')) {
$regex = '/(<|<\/|{|{\/|<!--|<!--\/){1}\s*';
$regex.= 'tmpl_([\w]+)\s*';
$regex.= '(?:';
$regex.= '((?:(?:';
$regex.= '(?:';
$regex.= '(name|format|escape|op|value|file)';
$regex.= '\s*=\s*';
......@@ -873,30 +873,10 @@ if (!defined('vlibTemplateClassLoaded')) {
$regex.= '((?<=[\"\'])';
$regex.= '[^\"\']*|[a-z0-9_\.]*)';
$regex.= '[\"\']?';
$regex.= ')?\s*';
$regex.= '(?:';
$regex.= '(?:';
$regex.= '(name|format|escape|op|value)';
$regex.= '\s*=\s*';
$regex.= ')';
$regex.= '(?:[\"\'])?';
$regex.= '((?<=[\"\'])';
$regex.= '[^\"\']*|[a-z0-9_\.]*)';
$regex.= '[\"\']?';
$regex.= ')?\s*';
$regex.= '(?:';
$regex.= '(?:';
$regex.= '(name|format|escape|op|value)';
$regex.= '\s*=\s*';
$regex.= ')';
$regex.= '(?:[\"\'])?';
$regex.= '((?<=[\"\'])';
$regex.= '[^\"\']*|[a-z0-9_\.]*)';
$regex.= '[\"\']?';
$regex.= ')?\s*';
$regex.= ')?\s*)*?)';
$regex.= '(?:>|\/>|}|-->){1}';
$regex.= '([\r\n|\n|\r])?/i';
$data = preg_replace_callback($regex, array($this, _parseTag), $data);
$regex.= '/i';
$data = preg_replace_callback($regex, array($this, '_parseTag'), $data);
if ($this->_cache) { // add cache if need be
$this->_createCache($data);
......@@ -1026,8 +1006,7 @@ if (!defined('vlibTemplateClassLoaded')) {
* @access private
* @return string used for eval'ing
*/
private function _parseIf($varname, $value = null, $op = null, $namespace = null)
{
function _parseIf ($varname, $value=null, $op=null, $namespace=null, $format=null) {
if (isset($namespace)) $namespace = substr($namespace, 0, -1);
$comp_str = ''; // used for extended if statements
......@@ -1065,9 +1044,19 @@ if (!defined('vlibTemplateClassLoaded')) {
}
}
if ($this->OPTIONS['GLOBAL_VARS'] && empty($namespace)) {
return '(('.$retstr.'[\''.$varname.'\'] !== null) ? '.$retstr.'[\''.$varname.'\'] : $this->_vars[\''.$varname.'\'])'.$comp_str;
} else {
return $retstr."['".$varname."']".$comp_str;
$retstr = '(('.$retstr.'[\''.$varname.'\'] !== null) ? '.$retstr.'[\''.$varname.'\'] : $this->_vars[\''.$varname.'\'])';
if(isset($format) && isset($value) && $format == 'version') {
return 'version_compare(' . $retstr . ', \'' . $value . '\', ' . (!empty($op) ? $op : '==') . ')';
} else {
return $retstr.$comp_str;
}
}
else {
if(isset($format) && isset($value) && $format == 'version') {
return 'version_compare(' . $retstr."['".$varname."']" . ', \'' . $value . '\', ' . (!empty($op) ? $op : '==') . ')';
} else {
return $retstr."['".$varname."']".$comp_str;
}
}
}
......@@ -1186,28 +1175,38 @@ if (!defined('vlibTemplateClassLoaded')) {
$wholetag = $args[0];
$openclose = $args[1];
$tag = strtolower($args[2]);
$newline = $args[9];
if ($tag == 'else') return '<?php } else { ?>'.$newline;
if ($tag == 'else') return '<?php } else { ?>';
if ($tag == 'tmpl_include') return $wholetag; // ignore tmpl_include tags
if (preg_match("/^<\/|{\/|<!--\/$/s", $openclose) || preg_match("/^end[if|loop|unless|comment]$/", $tag)) {
if ($tag == 'loop' || $tag == 'endloop') array_pop($this->_namespace);
if ($tag == 'comment' || $tag == 'endcomment') {
return '<?php */ ?>'.$newline;
} else {
return '<?php } ?>'.$newline;
return '<?php */ ?>';
}
else {
return '<?php } ?>';
}
}
//* arrange attributes
for ($i=3; $i < 8; $i=($i+2)) {
if (empty($args[$i]) && empty($args[($i+1)])) break;
$key = (empty($args[$i])) ? 'name' : strtolower($args[$i]);
if ($key == 'name' && preg_match('/^(php)?include$/', $tag)) $key = 'file';
$$key = $args[($i+1)];
// arrange attributes
$tmp_atts = $args[3];
$atts = preg_split('/\s+/', $tmp_atts);
foreach($atts as $att) {
$regex = '/(?:';
$regex.= '(name|format|escape|op|value|file)';
$regex.= '\s*=\s*';
$regex.= ')?';
$regex.= '(?:[\"\'])?';
$regex.= '((?<=[\"\'])';
$regex.= '[^\"\']*|[a-z0-9_\.]*)';
$regex.= '[\"\']?/';
if(preg_match($regex, $att, $match)) {
$key = (empty($match[1])) ? 'name' : strtolower($match[1]);
if ($key == 'name' && preg_match('/^(php)?include$/', $tag)) $key = 'file';
$$key = $match[2];
}
}
$var = ($this->OPTIONS['CASELESS']) ? strtolower($name) : $name;
if ($this->_debug && !empty($var)) {
......@@ -1229,37 +1228,47 @@ if (!defined('vlibTemplateClassLoaded')) {
if (empty($escape) && (!empty($this->OPTIONS['DEFAULT_ESCAPE']) && strtolower($this->OPTIONS['DEFAULT_ESCAPE']) != 'none')) {
$escape = strtolower($this->OPTIONS['DEFAULT_ESCAPE']);
}
return '<?php '.$this->_parseVar ($wholetag, $tag, $var, @$escape, @$format, @$namespace)." ?>$newline";
return '<?php '.$this->_parseVar ($wholetag, $tag, $var, @$escape, @$format, @$namespace).' ?>'."\n";
break;
case 'if':
return '<?php if ('. $this->_parseIf($var, @$value, @$op, @$namespace) .') { ?>'.$newline;
return '<?php if ('. $this->_parseIf($var, @$value, @$op, @$namespace, @$format) .') { ?>';
break;
case 'unless':
return '<?php if (!'. $this->_parseIf($var, @$value, @$op, @$namespace) .') { ?>'.$newline;
return '<?php if (!'. $this->_parseIf($var, @$value, @$op, @$namespace, @$format) .') { ?>';
break;
case 'elseif':
return '<?php } elseif ('. $this->_parseIf($var, @$value, @$op, @$namespace) .') { ?>'.$newline;
return '<?php } elseif ('. $this->_parseIf($var, @$value, @$op, @$namespace, @$format) .') { ?>';
break;
case 'loop':
return '<?php '. $this->_parseLoop($var) .'?>'.$newline;
return '<?php '. $this->_parseLoop($var) .'?>';
break;
case 'comment':
if (empty($var)) { // full open/close style comment
return '<?php /* ?>'.$newline;
} else { // just ignore tag if it was a one line comment
return '<?php /* ?>';
}
else { // just ignore tag if it was a one line comment
return;
}
break;
case 'phpinclude':
if ($this->OPTIONS['ENABLE_PHPINCLUDE']) {
return '<?php include(\''.$file.'\'); ?>'.$newline;
return '<?php include(\''.$file.'\'); ?>';
}
break;
case 'include':
return '<?php $this->_getData($this->_fileSearch(\''.$file.'\'), 1); ?>';
break;
case 'dyninclude':
return '<?php $this->_getData($this->_fileSearch($this->_dyninclude[\''.$name.'\']), 1); ?>';
break;
default:
if ($this->OPTIONS['STRICT']) vlibTemplateError::raiseError('VT_ERROR_INVALID_TAG', KILL, htmlspecialchars($wholetag, ENT_QUOTES));
......
......@@ -932,7 +932,7 @@ if (!defined('vlibTemplateClassLoaded')) {
$regex = '/(<|<\/|{|{\/|<!--|<!--\/){1}\s*';
$regex.= 'tmpl_([\w]+)\s*';
$regex.= '(?:';
$regex.= '((?:(?:';
$regex.= '(?:';
$regex.= '(name|format|escape|op|value|file)';
$regex.= '\s*=\s*';
......@@ -941,40 +941,9 @@ if (!defined('vlibTemplateClassLoaded')) {
$regex.= '((?<=[\"\'])';
$regex.= '[^\"\']*|[a-z0-9_\.]*)';
$regex.= '[\"\']?';
$regex.= ')?\s*';
$regex.= '(?:';
$regex.= '(?:';
$regex.= '(name|format|escape|op|value)';
$regex.= '\s*=\s*';
$regex.= ')';
$regex.= '(?:[\"\'])?';
$regex.= '((?<=[\"\'])';
$regex.= '[^\"\']*|[a-z0-9_\.]*)';
$regex.= '[\"\']?';
$regex.= ')?\s*';
$regex.= '(?:';
$regex.= '(?:';
$regex.= '(name|format|escape|op|value)';
$regex.= '\s*=\s*';
$regex.= ')';
$regex.= '(?:[\"\'])?';
$regex.= '((?<=[\"\'])';
$regex.= '[^\"\']*|[a-z0-9_\.]*)';
$regex.= '[\"\']?';
$regex.= ')?\s*';
$regex.= '(?:';
$regex.= '(?:';
$regex.= '(name|format|escape|op|value)';
$regex.= '\s*=\s*';
$regex.= ')';
$regex.= '(?:[\"\'])?';
$regex.= '((?<=[\"\'])';
$regex.= '[^\"\']*|[a-z0-9_\.]*)';
$regex.= '[\"\']?';
$regex.= ')?\s*';
$regex.= ')?\s*)*?)';
$regex.= '(?:>|\/>|}|-->){1}';
$regex.= '/i';
//$regex.= '([\r\n|\n|\r])?/ie';
$data = preg_replace_callback($regex, array($this, '_parseTag'), $data);
if ($this->_cache) { // add cache if need be
......@@ -1300,28 +1269,37 @@ if (!defined('vlibTemplateClassLoaded')) {
$wholetag = $args[0];
$openclose = $args[1];
$tag = strtolower($args[2]);
$newline = $args[11];
//echo "1#$newline#2";
if ($tag == 'else') return '<?php } else { ?>'.$newline;
if ($tag == 'else') return '<?php } else { ?>';
if ($tag == 'tmpl_include') return $wholetag; // ignore tmpl_include tags
if (preg_match("/^<\/|{\/|<!--\/$/s", $openclose) || preg_match("/^end[if|loop|unless|comment]$/", $tag)) {
if ($tag == 'loop' || $tag == 'endloop') array_pop($this->_namespace);
if ($tag == 'comment' || $tag == 'endcomment') {
return '<?php */ ?>'.$newline;
return '<?php */ ?>';
}
else {
return '<?php } ?>'.$newline;
return '<?php } ?>';
}
}
// arrange attributes
for ($i=3; $i < 10; $i=($i+2)) {
if (empty($args[$i]) && empty($args[($i+1)])) break;
$key = (empty($args[$i])) ? 'name' : strtolower($args[$i]);
if ($key == 'name' && preg_match('/^(php)?include$/', $tag)) $key = 'file';
$$key = $args[($i+1)];
$tmp_atts = $args[3];
$atts = preg_split('/\s+/', $tmp_atts);
foreach($atts as $att) {
$regex = '/(?:';
$regex.= '(name|format|escape|op|value|file)';
$regex.= '\s*=\s*';
$regex.= ')?';
$regex.= '(?:[\"\'])?';
$regex.= '((?<=[\"\'])';
$regex.= '[^\"\']*|[a-z0-9_\.]*)';
$regex.= '[\"\']?/';
if(preg_match($regex, $att, $match)) {
$key = (empty($match[1])) ? 'name' : strtolower($match[1]);
if ($key == 'name' && preg_match('/^(php)?include$/', $tag)) $key = 'file';
$$key = $match[2];
}
}
$var = ($this->OPTIONS['CASELESS']) ? strtolower($name) : $name;
......@@ -1345,28 +1323,28 @@ if (!defined('vlibTemplateClassLoaded')) {
if (empty($escape) && (!empty($this->OPTIONS['DEFAULT_ESCAPE']) && strtolower($this->OPTIONS['DEFAULT_ESCAPE']) != 'none')) {
$escape = strtolower($this->OPTIONS['DEFAULT_ESCAPE']);
}
return '<?php '.$this->_parseVar ($wholetag, $tag, $var, @$escape, @$format, @$namespace).' ?>'.$newline."\n";
return '<?php '.$this->_parseVar ($wholetag, $tag, $var, @$escape, @$format, @$namespace).' ?>'."\n";
break;
case 'if':
return '<?php if ('. $this->_parseIf($var, @$value, @$op, @$namespace, @$format) .') { ?>'.$newline;
return '<?php if ('. $this->_parseIf($var, @$value, @$op, @$namespace, @$format) .') { ?>';
break;
case 'unless':
return '<?php if (!'. $this->_parseIf($var, @$value, @$op, @$namespace, @$format) .') { ?>'.$newline;
return '<?php if (!'. $this->_parseIf($var, @$value, @$op, @$namespace, @$format) .') { ?>';
break;
case 'elseif':
return '<?php } elseif ('. $this->_parseIf($var, @$value, @$op, @$namespace, @$format) .') { ?>'.$newline;
return '<?php } elseif ('. $this->_parseIf($var, @$value, @$op, @$namespace, @$format) .') { ?>';
break;
case 'loop':
return '<?php '. $this->_parseLoop($var) .'?>'.$newline;
return '<?php '. $this->_parseLoop($var) .'?>';
break;
case 'comment':
if (empty($var)) { // full open/close style comment
return '<?php /* ?>'.$newline;
return '<?php /* ?>';
}
else { // just ignore tag if it was a one line comment
return;
......@@ -1375,7 +1353,7 @@ if (!defined('vlibTemplateClassLoaded')) {
case 'phpinclude':
if ($this->OPTIONS['ENABLE_PHPINCLUDE']) {
return '<?php include(\''.$file.'\'); ?>'.$newline;
return '<?php include(\''.$file.'\'); ?>';
}
break;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment