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 */
12 /**
13 * Smarty Internal Plugin Compile Function Class
14 */
15 class Smarty_Internal_Compile_Function extends Smarty_Internal_CompileBase {
16 // attribute definitions
17 public $required_attributes = array('name');
18 public $shorttag_order = array('name');
19 public $optional_attributes = array('_any');
21 /**
22 * Compiles code for the {function} tag
23 *
24 * @param array $args array with attributes from parser
25 * @param object $compiler compiler object
26 * @param array $parameter array with compilation parameter
27 * @return boolean true
28 */
29 public function compile($args, $compiler, $parameter)
30 {
31 $this->compiler = $compiler;
32 // check and get attributes
33 $_attr = $this->_get_attributes($args);
35 if ($_attr['nocache'] === true) {
36 $this->compiler->trigger_template_error('nocache option not allowed', $this->compiler->lex->taglineno);
37 }
38 unset($_attr['nocache']);
39 $save = array($_attr, $compiler->parser->current_buffer,
40 $compiler->template->has_nocache_code, $compiler->template->required_plugins);
41 $this->_open_tag('function', $save);
42 $_name = trim($_attr['name'], "'\"");
43 unset($_attr['name']);
44 $compiler->template->properties['function'][$_name]['parameter'] = array();
45 foreach ($_attr as $_key => $_data) {
46 $compiler->template->properties['function'][$_name]['parameter'][$_key] = $_data;
47 }
48 $compiler->smarty->template_functions[$_name]['parameter'] = $compiler->template->properties['function'][$_name]['parameter'];
49 if ($compiler->template->caching) {
50 $output = '';
51 } else {
52 $output = "<?php if (!function_exists('smarty_template_function_{$_name}')) {
53 function smarty_template_function_{$_name}(\$_smarty_tpl,\$params) {
54 \$saved_tpl_vars = \$_smarty_tpl->tpl_vars;
55 foreach (\$_smarty_tpl->template_functions['{$_name}']['parameter'] as \$key => \$value) {\$_smarty_tpl->tpl_vars[\$key] = new Smarty_variable(trim(\$value,'\''));};
56 foreach (\$params as \$key => \$value) {\$_smarty_tpl->tpl_vars[\$key] = new Smarty_variable(\$value);}?>";
57 }
58 // Init temporay context
59 $compiler->template->required_plugins = array('compiled' => array(), 'nocache' => array());
60 $compiler->parser->current_buffer = new _smarty_template_buffer($compiler->parser);
61 $compiler->parser->current_buffer->append_subtree(new _smarty_tag($compiler->parser, $output));
62 $compiler->template->has_nocache_code = false;
63 $compiler->has_code = false;
64 $compiler->template->properties['function'][$_name]['compiled'] = '';
65 return true;
66 }
67 }
69 /**
70 * Smarty Internal Plugin Compile Functionclose Class
71 */
72 class Smarty_Internal_Compile_Functionclose extends Smarty_Internal_CompileBase {
73 /**
74 * Compiles code for the {/function} tag
75 *
76 * @param array $args array with attributes from parser
77 * @param object $compiler compiler object
78 * @param array $parameter array with compilation parameter
79 * @return boolean true
80 */
81 public function compile($args, $compiler, $parameter)
82 {
83 $this->compiler = $compiler;
84 $_attr = $this->_get_attributes($args);
85 $saved_data = $this->_close_tag(array('function'));
86 $_name = trim($saved_data[0]['name'], "'\"");
87 // build plugin include code
88 $plugins_string = '';
89 if (!empty($compiler->template->required_plugins['compiled'])) {
90 $plugins_string = '<?php ';
91 foreach($compiler->template->required_plugins['compiled'] as $tmp) {
92 foreach($tmp as $data) {
93 $plugins_string .= "if (!is_callable('{$data['function']}')) include '{$data['file']}';\n";
94 }
95 }
96 $plugins_string .= '?>';
97 }
98 if (!empty($compiler->template->required_plugins['nocache'])) {
99 $plugins_string .= "<?php echo '/*%%SmartyNocache:{$compiler->template->properties['nocache_hash']}%%*/<?php ";
100 foreach($compiler->template->required_plugins['nocache'] as $tmp) {
101 foreach($tmp as $data) {
102 $plugins_string .= "if (!is_callable(\'{$data['function']}\')) include \'{$data['file']}\';\n";
103 }
104 }
105 $plugins_string .= "?>/*/%%SmartyNocache:{$compiler->template->properties['nocache_hash']}%%*/';?>\n";
106 }
107 // remove last line break from function definition
108 $last = count($compiler->parser->current_buffer->subtrees) - 1;
109 if ($compiler->parser->current_buffer->subtrees[$last] instanceof _smarty_linebreak) {
110 unset($compiler->parser->current_buffer->subtrees[$last]);
111 }
112 // if caching save template function for possible nocache call
113 if ($compiler->template->caching) {
114 $compiler->template->properties['function'][$_name]['compiled'] .= $plugins_string
115 . $compiler->parser->current_buffer->to_smarty_php();
116 $compiler->template->properties['function'][$_name]['nocache_hash'] = $compiler->template->properties['nocache_hash'];
117 $compiler->template->properties['function'][$_name]['has_nocache_code'] = $compiler->template->has_nocache_code;
118 $compiler->smarty->template_functions[$_name] = $compiler->template->properties['function'][$_name];
119 $compiler->has_code = false;
120 $output = true;
121 } else {
122 $output = $plugins_string . $compiler->parser->current_buffer->to_smarty_php() . "<?php \$_smarty_tpl->tpl_vars = \$saved_tpl_vars;}}?>\n";
123 }
124 // restore old compiler status
125 $compiler->parser->current_buffer = $saved_data[1];
126 $compiler->template->has_nocache_code = $compiler->template->has_nocache_code | $saved_data[2];
127 $compiler->template->required_plugins = $saved_data[3];
128 return $output;
129 }
130 }
132 ?>