source: pro-violet-viettel/sourcecode/application/third_party/Smarty/sysplugins/smarty_internal_compile_function.php

Last change on this file was 345, checked in by quyenla, 11 years ago

collaborator page

File size: 6.9 KB
Line 
1<?php
2/**
3 * Smarty Internal Plugin Compile Function
4 *
5 * Compiles the {function} {/function} tags
6 *
7 * @package Smarty
8 * @subpackage Compiler
9 * @author Uwe Tews
10 */
11
12/**
13 * Smarty Internal Plugin Compile Function Class
14 *
15 * @package Smarty
16 * @subpackage Compiler
17 */
18class Smarty_Internal_Compile_Function extends Smarty_Internal_CompileBase {
19
20    /**
21     * Attribute definition: Overwrites base class.
22     *
23     * @var array
24     * @see Smarty_Internal_CompileBase
25     */
26    public $required_attributes = array('name');
27    /**
28     * Attribute definition: Overwrites base class.
29     *
30     * @var array
31     * @see Smarty_Internal_CompileBase
32     */
33    public $shorttag_order = array('name');
34    /**
35     * Attribute definition: Overwrites base class.
36     *
37     * @var array
38     * @see Smarty_Internal_CompileBase
39     */
40    public $optional_attributes = array('_any');
41
42    /**
43     * Compiles code for the {function} tag
44     *
45     * @param array $args array with attributes from parser
46     * @param object $compiler compiler object
47     * @param array $parameter array with compilation parameter
48     * @return boolean true
49     */
50    public function compile($args, $compiler, $parameter)
51    {
52        // check and get attributes
53        $_attr = $this->getAttributes($compiler, $args);
54
55        if ($_attr['nocache'] === true) {
56            $compiler->trigger_template_error('nocache option not allowed', $compiler->lex->taglineno);
57        }
58        unset($_attr['nocache']);
59        $save = array($_attr, $compiler->parser->current_buffer,
60            $compiler->template->has_nocache_code, $compiler->template->required_plugins);
61        $this->openTag($compiler, 'function', $save);
62        $_name = trim($_attr['name'], "'\"");
63        unset($_attr['name']);
64        // set flag that we are compiling a template function
65        $compiler->compiles_template_function = true;
66        $compiler->template->properties['function'][$_name]['parameter'] = array();
67        $_smarty_tpl = $compiler->template;
68        foreach ($_attr as $_key => $_data) {
69            eval ('$tmp='.$_data.';');
70            $compiler->template->properties['function'][$_name]['parameter'][$_key] = $tmp;
71        }
72        $compiler->smarty->template_functions[$_name]['parameter'] = $compiler->template->properties['function'][$_name]['parameter'];
73        if ($compiler->template->caching) {
74            $output = '';
75        } else {
76            $output = "<?php if (!function_exists('smarty_template_function_{$_name}')) {
77    function smarty_template_function_{$_name}(\$_smarty_tpl,\$params) {
78    \$saved_tpl_vars = \$_smarty_tpl->tpl_vars;
79    foreach (\$_smarty_tpl->smarty->template_functions['{$_name}']['parameter'] as \$key => \$value) {\$_smarty_tpl->tpl_vars[\$key] = new Smarty_variable(\$value);};
80    foreach (\$params as \$key => \$value) {\$_smarty_tpl->tpl_vars[\$key] = new Smarty_variable(\$value);}?>";
81        }
82        // Init temporay context
83        $compiler->template->required_plugins = array('compiled' => array(), 'nocache' => array());
84        $compiler->parser->current_buffer = new _smarty_template_buffer($compiler->parser);
85        $compiler->parser->current_buffer->append_subtree(new _smarty_tag($compiler->parser, $output));
86        $compiler->template->has_nocache_code = false;
87        $compiler->has_code = false;
88        $compiler->template->properties['function'][$_name]['compiled'] = '';
89        return true;
90    }
91
92}
93
94/**
95 * Smarty Internal Plugin Compile Functionclose Class
96 *
97 * @package Smarty
98 * @subpackage Compiler
99 */
100class Smarty_Internal_Compile_Functionclose extends Smarty_Internal_CompileBase {
101
102    /**
103     * Compiles code for the {/function} tag
104     *
105     * @param array $args array with attributes from parser
106     * @param object $compiler compiler object
107     * @param array $parameter array with compilation parameter
108     * @return boolean true
109     */
110    public function compile($args, $compiler, $parameter)
111    {
112        $_attr = $this->getAttributes($compiler, $args);
113        $saved_data = $this->closeTag($compiler, array('function'));
114        $_name = trim($saved_data[0]['name'], "'\"");
115        // build plugin include code
116        $plugins_string = '';
117        if (!empty($compiler->template->required_plugins['compiled'])) {
118            $plugins_string = '<?php ';
119            foreach($compiler->template->required_plugins['compiled'] as $tmp) {
120                foreach($tmp as $data) {
121                    $plugins_string .= "if (!is_callable('{$data['function']}')) include '{$data['file']}';\n";
122                }
123            }
124            $plugins_string .= '?>';
125        }
126        if (!empty($compiler->template->required_plugins['nocache'])) {
127            $plugins_string .= "<?php echo '/*%%SmartyNocache:{$compiler->template->properties['nocache_hash']}%%*/<?php ";
128            foreach($compiler->template->required_plugins['nocache'] as $tmp) {
129                foreach($tmp as $data) {
130                    $plugins_string .= "if (!is_callable(\'{$data['function']}\')) include \'{$data['file']}\';\n";
131                }
132            }
133            $plugins_string .= "?>/*/%%SmartyNocache:{$compiler->template->properties['nocache_hash']}%%*/';?>\n";
134        }
135         // remove last line break from function definition
136         $last = count($compiler->parser->current_buffer->subtrees) - 1;
137         if ($compiler->parser->current_buffer->subtrees[$last] instanceof _smarty_linebreak) {
138             unset($compiler->parser->current_buffer->subtrees[$last]);
139         }
140        // if caching save template function for possible nocache call
141        if ($compiler->template->caching) {
142            $compiler->template->properties['function'][$_name]['compiled'] .= $plugins_string
143             . $compiler->parser->current_buffer->to_smarty_php();
144            $compiler->template->properties['function'][$_name]['nocache_hash'] = $compiler->template->properties['nocache_hash'];
145            $compiler->template->properties['function'][$_name]['has_nocache_code'] = $compiler->template->has_nocache_code;
146            $compiler->template->properties['function'][$_name]['called_functions'] = $compiler->called_functions;
147            $compiler->called_functions = array();
148            $compiler->smarty->template_functions[$_name] = $compiler->template->properties['function'][$_name];
149            $compiler->has_code = false;
150            $output = true;
151        } else {
152            $output = $plugins_string . $compiler->parser->current_buffer->to_smarty_php() . "<?php \$_smarty_tpl->tpl_vars = \$saved_tpl_vars;}}?>\n";
153        }
154        // reset flag that we are compiling a template function
155        $compiler->compiles_template_function = false;
156        // restore old compiler status
157        $compiler->parser->current_buffer = $saved_data[1];
158        $compiler->template->has_nocache_code = $compiler->template->has_nocache_code | $saved_data[2];
159        $compiler->template->required_plugins = $saved_data[3];
160        return $output;
161    }
162
163}
164
165?>
Note: See TracBrowser for help on using the repository browser.