3  * Smarty Internal Plugin Compile Function
 
   5  * Compiles the {function} {/function} tags
 
  13  * Smarty Internal Plugin Compile Function Class
 
  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'); 
 
  22      * Compiles code for the {function} tag
 
  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
 
  29     public function compile($args, $compiler, $parameter)
 
  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);
 
  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                 $_smarty_tpl = $compiler->template;
 
  46         foreach ($_attr as $_key => $_data) {
 
  47                 eval ('$tmp='.$_data.';');
 
  48             $compiler->template->properties['function'][$_name]['parameter'][$_key] = $tmp;
 
  50         $compiler->smarty->template_functions[$_name]['parameter'] = $compiler->template->properties['function'][$_name]['parameter'];
 
  51         if ($compiler->template->caching) {
 
  54             $output = "<?php if (!function_exists('smarty_template_function_{$_name}')) {
 
  55     function smarty_template_function_{$_name}(\$_smarty_tpl,\$params) {
 
  56     \$saved_tpl_vars = \$_smarty_tpl->tpl_vars;
 
  57     foreach (\$_smarty_tpl->template_functions['{$_name}']['parameter'] as \$key => \$value) {\$_smarty_tpl->tpl_vars[\$key] = new Smarty_variable(\$value);};
 
  58     foreach (\$params as \$key => \$value) {\$_smarty_tpl->tpl_vars[\$key] = new Smarty_variable(\$value);}?>";
 
  60         // Init temporay context
 
  61         $compiler->template->required_plugins = array('compiled' => array(), 'nocache' => array());
 
  62         $compiler->parser->current_buffer = new _smarty_template_buffer($compiler->parser);
 
  63         $compiler->parser->current_buffer->append_subtree(new _smarty_tag($compiler->parser, $output));
 
  64         $compiler->template->has_nocache_code = false;
 
  65         $compiler->has_code = false;
 
  66         $compiler->template->properties['function'][$_name]['compiled'] = '';
 
  72  * Smarty Internal Plugin Compile Functionclose Class
 
  74 class Smarty_Internal_Compile_Functionclose extends Smarty_Internal_CompileBase {
 
  76      * Compiles code for the {/function} tag
 
  78      * @param array $args array with attributes from parser
 
  79      * @param object $compiler compiler object
 
  80      * @param array $parameter array with compilation parameter
 
  81      * @return boolean true
 
  83     public function compile($args, $compiler, $parameter)
 
  85         $this->compiler = $compiler;
 
  86         $_attr = $this->_get_attributes($args);
 
  87         $saved_data = $this->_close_tag(array('function'));
 
  88         $_name = trim($saved_data[0]['name'], "'\""); 
 
  89         // build plugin include code
 
  91         if (!empty($compiler->template->required_plugins['compiled'])) {
 
  92             $plugins_string = '<?php ';
 
  93             foreach($compiler->template->required_plugins['compiled'] as $tmp) {
 
  94                 foreach($tmp as $data) {
 
  95                     $plugins_string .= "if (!is_callable('{$data['function']}')) include '{$data['file']}';\n";
 
  98             $plugins_string .= '?>';
 
 100         if (!empty($compiler->template->required_plugins['nocache'])) {
 
 101             $plugins_string .= "<?php echo '/*%%SmartyNocache:{$compiler->template->properties['nocache_hash']}%%*/<?php ";
 
 102             foreach($compiler->template->required_plugins['nocache'] as $tmp) {
 
 103                 foreach($tmp as $data) {
 
 104                     $plugins_string .= "if (!is_callable(\'{$data['function']}\')) include \'{$data['file']}\';\n";
 
 107             $plugins_string .= "?>/*/%%SmartyNocache:{$compiler->template->properties['nocache_hash']}%%*/';?>\n";
 
 109                 // remove last line break from function definition
 
 110                 $last = count($compiler->parser->current_buffer->subtrees) - 1;
 
 111                 if ($compiler->parser->current_buffer->subtrees[$last] instanceof _smarty_linebreak) {
 
 112                         unset($compiler->parser->current_buffer->subtrees[$last]);
 
 114         // if caching save template function for possible nocache call
 
 115         if ($compiler->template->caching) {
 
 116             $compiler->template->properties['function'][$_name]['compiled'] .= $plugins_string
 
 117              . $compiler->parser->current_buffer->to_smarty_php();
 
 118             $compiler->template->properties['function'][$_name]['nocache_hash'] = $compiler->template->properties['nocache_hash'];
 
 119             $compiler->template->properties['function'][$_name]['has_nocache_code'] = $compiler->template->has_nocache_code;
 
 120             $compiler->smarty->template_functions[$_name] = $compiler->template->properties['function'][$_name];
 
 121             $compiler->has_code = false;
 
 124             $output = $plugins_string . $compiler->parser->current_buffer->to_smarty_php() . "<?php \$_smarty_tpl->tpl_vars = \$saved_tpl_vars;}}?>\n";
 
 126         // restore old compiler status
 
 127         $compiler->parser->current_buffer = $saved_data[1];
 
 128         $compiler->template->has_nocache_code = $compiler->template->has_nocache_code | $saved_data[2];
 
 129         $compiler->template->required_plugins = $saved_data[3];