📄 smarty_compiler.class.php
字号:
return $output; }/*======================================================================*\ Function: _compile_foreach_start Purpose: Compile {foreach ...} tag\*======================================================================*/ function _compile_foreach_start($tag_args) { $attrs = $this->_parse_attrs($tag_args); $arg_list = array(); if (empty($attrs['from'])) { $this->_syntax_error("missing 'from' attribute"); } if (empty($attrs['item'])) { $this->_syntax_error("missing 'item' attribute"); } $from = $attrs['from']; $item = $this->_dequote($attrs['item']); if (isset($attrs['name'])) $name = $attrs['name']; $output = '<?php '; if (isset($name)) { $output .= "if (isset(\$this->_foreach[$name])) unset(\$this->_foreach[$name]);\n"; $foreach_props = "\$this->_foreach[$name]"; } $key_part = ''; foreach ($attrs as $attr_name => $attr_value) { switch ($attr_name) { case 'key': $key = $this->_dequote($attrs['key']); $key_part = "\$this->_tpl_vars['$key'] => "; break; case 'name': $output .= "{$foreach_props}['$attr_name'] = $attr_value;\n"; break; } } if (isset($name)) { $output .= "{$foreach_props}['total'] = count((array)$from);\n"; $output .= "{$foreach_props}['show'] = {$foreach_props}['total'] > 0;\n"; $output .= "if ({$foreach_props}['show']):\n"; $output .= "{$foreach_props}['iteration'] = 0;\n"; $output .= " foreach ((array)$from as $key_part\$this->_tpl_vars['$item']):\n"; $output .= " {$foreach_props}['iteration']++;\n"; $output .= " {$foreach_props}['first'] = ({$foreach_props}['iteration'] == 1);\n"; $output .= " {$foreach_props}['last'] = ({$foreach_props}['iteration'] == {$foreach_props}['total']);\n"; } else { $output .= "if (count((array)$from)):\n"; $output .= " foreach ((array)$from as $key_part\$this->_tpl_vars['$item']):\n"; } $output .= '?>'; return $output; }/*======================================================================*\ Function: _compile_capture_tag Purpose: Compile {capture} .. {/capture} tags\*======================================================================*/ function _compile_capture_tag($start, $tag_args = '') { $attrs = $this->_parse_attrs($tag_args); if ($start) { if (isset($attrs['name'])) $buffer = $attrs['name']; else $buffer = "'default'"; $output = "<?php ob_start(); ?>"; $this->_capture_stack[] = $buffer; } else { $buffer = array_pop($this->_capture_stack); $output = "<?php \$this->_smarty_vars['capture'][$buffer] = ob_get_contents(); ob_end_clean(); ?>"; } return $output; }/*======================================================================*\ Function: _compile_if_tag Purpose: Compile {if ...} tag\*======================================================================*/ function _compile_if_tag($tag_args, $elseif = false) { /* Tokenize args for 'if' tag. */ preg_match_all('/(?: "[^"\\\\]*(?:\\\\.[^"\\\\]*)*" | # match all double quoted strings allowing escaped double quotes \'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\' | # match all single quoted strings allowing escaped single quotes [(),] | # match parentheses and commas [^\s(),]+ # match any other token that is not any of the above )/x', $tag_args, $match); $tokens = $match[0]; $this->_parse_vars_props($tokens); $is_arg_stack = array(); for ($i = 0; $i < count($tokens); $i++) { $token = &$tokens[$i]; switch ($token) { case 'eq': $token = '=='; break; case 'ne': case 'neq': $token = '!='; break; case 'lt': $token = '<'; break; case 'le': case 'lte': $token = '<='; break; case 'gt': $token = '>'; break; case 'ge': case 'gte': $token = '>='; break; case 'and': $token = '&&'; break; case 'or': $token = '||'; break; case 'not': $token = '!'; break; case 'mod': $token = '%'; break; case '(': array_push($is_arg_stack, $i); break; case 'is': /* If last token was a ')', we operate on the parenthesized expression. The start of the expression is on the stack. Otherwise, we operate on the last encountered token. */ if ($tokens[$i-1] == ')') $is_arg_start = array_pop($is_arg_stack); else $is_arg_start = $i-1; /* Construct the argument for 'is' expression, so it knows what to operate on. */ $is_arg = implode(' ', array_slice($tokens, $is_arg_start, $i - $is_arg_start)); /* Pass all tokens from next one until the end to the 'is' expression parsing function. The function will return modified tokens, where the first one is the result of the 'is' expression and the rest are the tokens it didn't touch. */ $new_tokens = $this->_parse_is_expr($is_arg, array_slice($tokens, $i+1)); /* Replace the old tokens with the new ones. */ array_splice($tokens, $is_arg_start, count($tokens), $new_tokens); /* Adjust argument start so that it won't change from the current position for the next iteration. */ $i = $is_arg_start; break; default: if($this->security && $tokens[$i+1] == '(' && preg_match('!^[a-zA-Z_]\w+$!', $tokens[$i]) && !in_array($tokens[$i], $this->security_settings['IF_FUNCS'])) { $this->_syntax_error("(secure mode) '".$tokens[$i]."' not allowed in if statement"); } break; } } if ($elseif) return '<?php elseif ('.implode(' ', $tokens).'): ?>'; else return '<?php if ('.implode(' ', $tokens).'): ?>'; }/*======================================================================*\ Function: _parse_is_expr Purpose: Parse is expression\*======================================================================*/ function _parse_is_expr($is_arg, $tokens) { $expr_end = 0; $negate_expr = false; if (($first_token = array_shift($tokens)) == 'not') { $negate_expr = true; $expr_type = array_shift($tokens); } else $expr_type = $first_token; switch ($expr_type) { case 'even': if (@$tokens[$expr_end] == 'by') { $expr_end++; $expr_arg = $tokens[$expr_end++]; $expr = "!(($is_arg / $expr_arg) % $expr_arg)"; } else $expr = "!($is_arg % 2)"; break; case 'odd': if (@$tokens[$expr_end] == 'by') { $expr_end++; $expr_arg = $tokens[$expr_end++]; $expr = "(($is_arg / $expr_arg) % $expr_arg)"; } else $expr = "($is_arg % 2)"; break; case 'div': if (@$tokens[$expr_end] == 'by') { $expr_end++; $expr_arg = $tokens[$expr_end++]; $expr = "!($is_arg % $expr_arg)"; } else { $this->_syntax_error("expecting 'by' after 'div'"); } break; default: $this->_syntax_error("unknown 'is' expression - '$expr_type'"); break; } if ($negate_expr) { $expr = "!($expr)"; } array_splice($tokens, 0, $expr_end, $expr); return $tokens; }/*======================================================================*\ Function: _parse_attrs Purpose: Parse attribute string\*======================================================================*/ function _parse_attrs($tag_args, $quote = true) { /* Tokenize tag attributes. */ preg_match_all('/(?:"[^"\\\\]*(?:\\\\.[^"\\\\]*)*" | \'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\' | (?>[^"\'=\s]+) )+ | [=] /x', $tag_args, $match); $tokens = $match[0]; $var_delims = array('$', '#', '%'); $attrs = array(); /* Parse state: 0 - expecting attribute name 1 - expecting '=' 2 - expecting attribute value (not '=') */ $state = 0; foreach ($tokens as $token) { switch ($state) { case 0: /* If the token is a valid identifier, we set attribute name and go to state 1. */ if (preg_match('!^\w+$!', $token)) { $attr_name = $token; $state = 1; } else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -