⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 smarty_compiler.class.php

📁 smarty+AJAX实例
💻 PHP
📖 第 1 页 / 共 5 页
字号:
        if (isset($assign_var)) {            $output .= "ob_start();\n";        }        $output .=            "\$_smarty_tpl_vars = \$this->_tpl_vars;\n";        $_params = "array('smarty_include_tpl_file' => " . $include_file . ", 'smarty_include_vars' => array(".implode(',', (array)$arg_list)."))";        $output .= "\$this->_smarty_include($_params);\n" .        "\$this->_tpl_vars = \$_smarty_tpl_vars;\n" .        "unset(\$_smarty_tpl_vars);\n";        if (isset($assign_var)) {            $output .= "\$this->assign(" . $assign_var . ", ob_get_contents()); ob_end_clean();\n";        }        $output .= ' ?>';        return $output;    }    /**     * Compile {include ...} tag     *     * @param string $tag_args     * @return string     */    function _compile_include_php_tag($tag_args)    {        $attrs = $this->_parse_attrs($tag_args);        if (empty($attrs['file'])) {            $this->_syntax_error("missing 'file' attribute in include_php tag", E_USER_ERROR, __FILE__, __LINE__);        }        $assign_var = (empty($attrs['assign'])) ? '' : $this->_dequote($attrs['assign']);        $once_var = (empty($attrs['once']) || $attrs['once']=='false') ? 'false' : 'true';        $arg_list = array();        foreach($attrs as $arg_name => $arg_value) {            if($arg_name != 'file' AND $arg_name != 'once' AND $arg_name != 'assign') {                if(is_bool($arg_value))                    $arg_value = $arg_value ? 'true' : 'false';                $arg_list[] = "'$arg_name' => $arg_value";            }        }        $_params = "array('smarty_file' => " . $attrs['file'] . ", 'smarty_assign' => '$assign_var', 'smarty_once' => $once_var, 'smarty_include_vars' => array(".implode(',', $arg_list)."))";        return "<?php require_once(SMARTY_CORE_DIR . 'core.smarty_include_php.php');\nsmarty_core_smarty_include_php($_params, \$this); ?>" . $this->_additional_newline;    }    /**     * Compile {section ...} tag     *     * @param string $tag_args     * @return string     */    function _compile_section_start($tag_args)    {        $attrs = $this->_parse_attrs($tag_args);        $arg_list = array();        $output = '<?php ';        $section_name = $attrs['name'];        if (empty($section_name)) {            $this->_syntax_error("missing section name", E_USER_ERROR, __FILE__, __LINE__);        }        $output .= "unset(\$this->_sections[$section_name]);\n";        $section_props = "\$this->_sections[$section_name]";        foreach ($attrs as $attr_name => $attr_value) {            switch ($attr_name) {                case 'loop':                    $output .= "{$section_props}['loop'] = is_array(\$_loop=$attr_value) ? count(\$_loop) : max(0, (int)\$_loop); unset(\$_loop);\n";                    break;                case 'show':                    if (is_bool($attr_value))                        $show_attr_value = $attr_value ? 'true' : 'false';                    else                        $show_attr_value = "(bool)$attr_value";                    $output .= "{$section_props}['show'] = $show_attr_value;\n";                    break;                case 'name':                    $output .= "{$section_props}['$attr_name'] = $attr_value;\n";                    break;                case 'max':                case 'start':                    $output .= "{$section_props}['$attr_name'] = (int)$attr_value;\n";                    break;                case 'step':                    $output .= "{$section_props}['$attr_name'] = ((int)$attr_value) == 0 ? 1 : (int)$attr_value;\n";                    break;                default:                    $this->_syntax_error("unknown section attribute - '$attr_name'", E_USER_ERROR, __FILE__, __LINE__);                    break;            }        }        if (!isset($attrs['show']))            $output .= "{$section_props}['show'] = true;\n";        if (!isset($attrs['loop']))            $output .= "{$section_props}['loop'] = 1;\n";        if (!isset($attrs['max']))            $output .= "{$section_props}['max'] = {$section_props}['loop'];\n";        else            $output .= "if ({$section_props}['max'] < 0)\n" .                       "    {$section_props}['max'] = {$section_props}['loop'];\n";        if (!isset($attrs['step']))            $output .= "{$section_props}['step'] = 1;\n";        if (!isset($attrs['start']))            $output .= "{$section_props}['start'] = {$section_props}['step'] > 0 ? 0 : {$section_props}['loop']-1;\n";        else {            $output .= "if ({$section_props}['start'] < 0)\n" .                       "    {$section_props}['start'] = max({$section_props}['step'] > 0 ? 0 : -1, {$section_props}['loop'] + {$section_props}['start']);\n" .                       "else\n" .                       "    {$section_props}['start'] = min({$section_props}['start'], {$section_props}['step'] > 0 ? {$section_props}['loop'] : {$section_props}['loop']-1);\n";        }        $output .= "if ({$section_props}['show']) {\n";        if (!isset($attrs['start']) && !isset($attrs['step']) && !isset($attrs['max'])) {            $output .= "    {$section_props}['total'] = {$section_props}['loop'];\n";        } else {            $output .= "    {$section_props}['total'] = min(ceil(({$section_props}['step'] > 0 ? {$section_props}['loop'] - {$section_props}['start'] : {$section_props}['start']+1)/abs({$section_props}['step'])), {$section_props}['max']);\n";        }        $output .= "    if ({$section_props}['total'] == 0)\n" .                   "        {$section_props}['show'] = false;\n" .                   "} else\n" .                   "    {$section_props}['total'] = 0;\n";        $output .= "if ({$section_props}['show']):\n";        $output .= "            for ({$section_props}['index'] = {$section_props}['start'], {$section_props}['iteration'] = 1;                 {$section_props}['iteration'] <= {$section_props}['total'];                 {$section_props}['index'] += {$section_props}['step'], {$section_props}['iteration']++):\n";        $output .= "{$section_props}['rownum'] = {$section_props}['iteration'];\n";        $output .= "{$section_props}['index_prev'] = {$section_props}['index'] - {$section_props}['step'];\n";        $output .= "{$section_props}['index_next'] = {$section_props}['index'] + {$section_props}['step'];\n";        $output .= "{$section_props}['first']      = ({$section_props}['iteration'] == 1);\n";        $output .= "{$section_props}['last']       = ({$section_props}['iteration'] == {$section_props}['total']);\n";        $output .= "?>";        return $output;    }    /**     * Compile {foreach ...} tag.     *     * @param string $tag_args     * @return string     */    function _compile_foreach_start($tag_args)    {        $attrs = $this->_parse_attrs($tag_args);        $arg_list = array();        if (empty($attrs['from'])) {            return $this->_syntax_error("foreach: missing 'from' attribute", E_USER_ERROR, __FILE__, __LINE__);        }        $from = $attrs['from'];        if (empty($attrs['item'])) {            return $this->_syntax_error("foreach: missing 'item' attribute", E_USER_ERROR, __FILE__, __LINE__);        }        $item = $this->_dequote($attrs['item']);        if (!preg_match('~^\w+$~', $item)) {            return $this->_syntax_error("'foreach: item' must be a variable name (literal string)", E_USER_ERROR, __FILE__, __LINE__);        }        if (isset($attrs['key'])) {            $key  = $this->_dequote($attrs['key']);            if (!preg_match('~^\w+$~', $key)) {                return $this->_syntax_error("foreach: 'key' must to be a variable name (literal string)", E_USER_ERROR, __FILE__, __LINE__);            }            $key_part = "\$this->_tpl_vars['$key'] => ";        } else {            $key = null;            $key_part = '';        }        if (isset($attrs['name'])) {            $name = $attrs['name'];        } else {            $name = null;        }        $output = '<?php ';        $output .= "\$_from = $from; if (!is_array(\$_from) && !is_object(\$_from)) { settype(\$_from, 'array'); }";        if (isset($name)) {            $foreach_props = "\$this->_foreach[$name]";            $output .= "{$foreach_props} = array('total' => count(\$_from), 'iteration' => 0);\n";            $output .= "if ({$foreach_props}['total'] > 0):\n";            $output .= "    foreach (\$_from as $key_part\$this->_tpl_vars['$item']):\n";            $output .= "        {$foreach_props}['iteration']++;\n";        } else {            $output .= "if (count(\$_from)):\n";            $output .= "    foreach (\$_from as $key_part\$this->_tpl_vars['$item']):\n";        }        $output .= '?>';        return $output;    }    /**     * Compile {capture} .. {/capture} tags     *     * @param boolean $start true if this is the {capture} tag     * @param string $tag_args     * @return string     */    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'";            if (isset($attrs['assign']))                $assign = $attrs['assign'];            else                $assign = null;            $output = "<?php ob_start(); ?>";            $this->_capture_stack[] = array($buffer, $assign);        } else {            list($buffer, $assign) = array_pop($this->_capture_stack);            $output = "<?php \$this->_smarty_vars['capture'][$buffer] = ob_get_contents(); ";            if (isset($assign)) {                $output .= " \$this->assign($assign, ob_get_contents());";            }            $output .= "ob_end_clean(); ?>";        }        return $output;    }    /**     * Compile {if ...} tag     *     * @param string $tag_args     * @param boolean $elseif if true, uses elseif instead of if     * @return string     */    function _compile_if_tag($tag_args, $elseif = false)    {        /* Tokenize args for 'if' tag. */        preg_match_all('~(?>                ' . $this->_obj_call_regexp . '(?:' . $this->_mod_regexp . '*)? | # valid object call                ' . $this->_var_regexp . '(?:' . $this->_mod_regexp . '*)?    | # var or quoted string                \-?0[xX][0-9a-fA-F]+|\-?\d+(?:\.\d+)?|\.\d+|!==|===|==|!=|<>|<<|>>|<=|>=|\&\&|\|\||\(|\)|,|\!|\^|=|\&|\~|<|>|\||\%|\+|\-|\/|\*|\@    | # valid non-word token                \b\w+\b                                                        | # valid word token                \S+                                                           # anything else                )~x', $tag_args, $match);        $tokens = $match[0];        if(empty($tokens)) {            $_error_msg = $elseif ? "'elseif'" : "'if'";            $_error_msg .= ' statement requires arguments';             $this->_syntax_error($_error_msg, E_USER_ERROR, __FILE__, __LINE__);        }                                    // make sure we have balanced parenthesis        $token_count = array_count_values($tokens);        if(isset($token_count['(']) && $token_count['('] != $token_count[')']) {            $this->_syntax_error("unbalanced parenthesis in if statement", E_USER_ERROR, __FILE__, __LINE__);        }        $is_arg_stack = array();        for ($i = 0; $i < count($tokens); $i++) {            $token = &$tokens[$i];            switch (strtolower($token)) {                case '!':                case '%':                case '!==':                case '==':                case '===':                case '>':                case '<':                case '!=':                case '<>':                case '<<':                case '>>':                case '<=':                case '>=':                case '&&':                case '||':                case '|':                case '^':                case '&':                case '~':                case ')':                case ',':                case '+':                case '-':                case '*':                case '/':                case '@':                    break;                case 'eq':                    $token = '==';                    break;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -