1 <?php
3 /**
4 * Smarty Internal Plugin Compile Insert
5 *
6 * Compiles the {insert} tag
7 *
8 * @package Smarty
9 * @subpackage Compiler
10 * @author Uwe Tews
11 */
12 /**
13 * Smarty Internal Plugin Compile Insert Class
14 */
15 class Smarty_Internal_Compile_Insert extends Smarty_Internal_CompileBase {
16 /**
17 * Compiles code for the {insert} tag
18 *
19 * @param array $args array with attributes from parser
20 * @param object $compiler compiler object
21 * @return string compiled code
22 */
23 public function compile($args, $compiler)
24 {
25 $this->compiler = $compiler;
26 $this->required_attributes = array('name');
27 $this->optional_attributes = array('_any');
28 // check and get attributes
29 $_attr = $this->_get_attributes($args);
30 // this tag must not be cached
31 $this->compiler->tag_nocache = true;
32 $_smarty_tpl = $compiler->template;
34 $_output = '<?php ';
35 // save posible attributes
36 eval('$_name = ' . $_attr['name'] . ';');
37 $_function = "insert_{$_name}";
38 if (isset($_attr['assign'])) {
39 // output will be stored in a smarty variable instead of beind displayed
40 $_assign = $_attr['assign'];
41 // create variable to make shure that the compiler knows about its nocache status
42 $this->compiler->template->tpl_vars[trim($_attr['assign'], "'")] = new Smarty_Variable(null, true);
43 }
44 if (isset($_attr['script'])) {
45 // script which must be included
46 $_smarty_tpl = $compiler->template;
47 eval('$_script = ' . $_attr['script'] . ';');
48 if (!file_exists($_script)) {
49 $this->compiler->trigger_template_error("{insert} missing script file '{$_script}'");
50 }
51 // code for script file loading
52 $_output .= "require_once {$_script} ;";
53 require_once $_script;
54 if (!is_callable($_function)) {
55 $this->compiler->trigger_template_error(" {insert} function '{$_name}' is not callable");
56 }
57 } else {
58 if (!is_callable($_function)) {
59 if (!$_function = $this->compiler->getPlugin($_name, 'insert')) {
60 $this->compiler->trigger_template_error("{insert} no function or plugin found for '{$_name}'");
61 }
62 }
63 }
64 // delete {insert} standard attributes
65 unset($_attr['name'], $_attr['assign'], $_attr['script']);
66 // convert attributes into parameter array string
67 $_paramsArray = array();
68 foreach ($_attr as $_key => $_value) {
69 $_paramsArray[] = "'$_key' => $_value";
70 }
71 $_params = 'array(' . implode(", ", $_paramsArray) . ')';
72 // call insert
73 if (isset($_assign)) {
74 $_output .= "\$_smarty_tpl->assign({$_assign} , {$_function} ({$_params},\$_smarty_tpl->smarty,\$_smarty_tpl), true);?>";
75 } else {
76 $this->compiler->has_output = true;
77 $_output .= "echo {$_function}({$_params},\$_smarty_tpl->smarty,\$_smarty_tpl);?>";
78 }
79 return $_output;
80 }
81 }
83 ?>