3 * Template Lite compile IF tag - template internal module
6 * Name: compile_parse_is_expr
9 function compile_compile_if($arguments, $elseif, $while, &$object)
14 $_is_arg_stack = array();
16 // extract arguments from the equation
17 preg_match_all('/(?>(' . $object->_const_regexp
. '|\/?' . $object->_var_regexp
. '|\/?' . $object->_svar_regexp
. '|\/?' . $object->_func_regexp
. ')(?:' . $object->_mod_regexp
. '*)?|\-?0[xX][0-9a-fA-F]+|\-?\d+(?:\.\d+)?|\.\d+|!==|===|==|!=|<>|<<|>>|<=|>=|\&\&|\|\||\(|\)|,|\!|\^|=|\&|\~|<|>|\%|\+|\-|\/|\*|\@|\b\w+\b|\S+)/x', $arguments, $_match);
20 // make sure we have balanced parenthesis
21 $_args_count = array_count_values($_args);
22 if(isset($_args_count['(']) && $_args_count['('] != $_args_count[')'])
24 throw new Template_Exception("unbalanced parenthesis in if statement", $object);
27 $count_args = count($_args);
28 for ($i = 0, $for_max = $count_args; $i < $for_max; $i++
)
31 switch (strtolower($_arg))
93 array_push($_is_arg_stack, $i);
96 if ($_args[$i-1] == ')')
98 $is_arg_start = array_pop($is_arg_stack);
102 $_is_arg_count = count($_args);
103 $is_arg = implode(' ', array_slice($_args, $is_arg_start, $i - $is_arg_start));
104 $_arg_tokens = $object->_parse_is_expr($is_arg, array_slice($_args, $i+
1));
105 array_splice($_args, $is_arg_start, count($_args), $_arg_tokens);
106 $i = $_is_arg_count - count($_args);
110 if (defined($_arg) && preg_match('/^'.$object->_const_regexp
.'$/', $_arg))
112 $_arg = "constant('".$_arg."')";
115 preg_match('/(?:(' . $object->_var_regexp
. '|' . $object->_svar_regexp
. '|' . $object->_func_regexp
. ')(' . $object->_mod_regexp
. '*)(?:\s*[,\.]\s*)?)(?:\s+(.*))?/xs', $_arg, $_match);
116 if (isset($_match[0]{0}) && ($_match[0]{0} == '$' ||
($_match[0]{0} == '#' && $_match[0]{strlen($_match[0]) - 1} == '#') ||
$_match[0]{0} == "'" ||
$_match[0]{0} == '"' ||
$_match[0]{0} == '%'))
118 // process a variable
119 $_arg = $object->_parse_variables(array($_match[1]), array($_match[2]));
121 elseif (is_numeric($_arg))
123 // pass the number through
125 elseif (function_exists($_match[0]) ||
$_match[0] == "empty" ||
$_match[0] == "isset" ||
$_match[0] == "unset" ||
strtolower($_match[0]) == "true" ||
strtolower($_match[0]) == "false" ||
strtolower($_match[0]) == "null")
127 // pass the function through
129 elseif (empty($_arg))
131 // pass the empty argument through
135 throw new Template_Exception("unidentified token '$_arg'", $object);
143 return implode(' ', $_args);
149 return '<?php elseif ('.implode(' ', $_args).'): ?>';
153 return '<?php if ('.implode(' ', $_args).'): ?>';