4 * Smarty Internal Plugin Compile Insert
5 * Compiles the {insert} tag
13 * Smarty Internal Plugin Compile Insert Class
16 * @subpackage Compiler
18 class Smarty_Internal_Compile_Insert
extends Smarty_Internal_CompileBase
21 * Attribute definition: Overwrites base class.
24 * @see Smarty_Internal_CompileBase
26 public $required_attributes = array('name');
28 * Attribute definition: Overwrites base class.
31 * @see Smarty_Internal_CompileBase
33 public $shorttag_order = array('name');
35 * Attribute definition: Overwrites base class.
38 * @see Smarty_Internal_CompileBase
40 public $optional_attributes = array('_any');
43 * Compiles code for the {insert} tag
45 * @param array $args array with attributes from parser
46 * @param object $compiler compiler object
48 * @return string compiled code
50 public function compile($args, $compiler)
52 // check and get attributes
53 $_attr = $this->getAttributes($compiler, $args);
54 $nocacheParam = $compiler->template
->caching
&& ($compiler->tag_nocache ||
$compiler->nocache
);
56 // do not compile as nocache code
57 $compiler->suppressNocacheProcessing
= true;
59 $compiler->tag_nocache
= true;
60 $_smarty_tpl = $compiler->template
;
65 // save possible attributes
66 eval('$_name = ' . $_attr['name'] . ';');
67 if (isset($_attr['assign'])) {
68 // output will be stored in a smarty variable instead of being displayed
69 $_assign = $_attr['assign'];
70 // create variable to make sure that the compiler knows about its nocache status
71 $var = trim($_attr['assign'], "'");
72 if (isset($compiler->template
->tpl_vars
[$var])) {
73 $compiler->template
->tpl_vars
[$var]->nocache
= true;
75 $compiler->template
->tpl_vars
[$var] = new Smarty_Variable(null, true);
78 if (isset($_attr['script'])) {
79 // script which must be included
80 $_function = "smarty_insert_{$_name}";
81 $_smarty_tpl = $compiler->template
;
83 eval('$_script = ' . $_attr['script'] . ';');
84 if (!isset($compiler->smarty
->security_policy
) && file_exists($_script)) {
85 $_filepath = $_script;
87 if (isset($compiler->smarty
->security_policy
)) {
88 $_dir = $compiler->smarty
->security_policy
->trusted_dir
;
90 $_dir = $compiler->smarty
->trusted_dir
;
93 foreach ((array) $_dir as $_script_dir) {
94 $_script_dir = rtrim($_script_dir, '/\\') . DS
;
95 if (file_exists($_script_dir . $_script)) {
96 $_filepath = $_script_dir . $_script;
102 if ($_filepath == false) {
103 $compiler->trigger_template_error("{insert} missing script file '{$_script}'", $compiler->lex
->taglineno
);
105 // code for script file loading
106 $_output .= "require_once '{$_filepath}' ;";
107 require_once $_filepath;
108 if (!is_callable($_function)) {
109 $compiler->trigger_template_error(" {insert} function '{$_function}' is not callable in script file '{$_script}'", $compiler->lex
->taglineno
);
113 $_function = "insert_{$_name}";
114 // function in PHP script ?
115 if (!is_callable($_function)) {
117 if (!$_function = $compiler->getPlugin($_name, 'insert')) {
118 $compiler->trigger_template_error("{insert} no function or plugin found for '{$_name}'", $compiler->lex
->taglineno
);
122 // delete {insert} standard attributes
123 unset($_attr['name'], $_attr['assign'], $_attr['script'], $_attr['nocache']);
124 // convert attributes into parameter array string
125 $_paramsArray = array();
126 foreach ($_attr as $_key => $_value) {
127 $_paramsArray[] = "'$_key' => $_value";
129 $_params = 'array(' . implode(", ", $_paramsArray) . ')';
131 if (isset($_assign)) {
132 if ($_smarty_tpl->caching
&& !$nocacheParam) {
133 $_output .= "echo Smarty_Internal_Nocache_Insert::compile ('{$_function}',{$_params}, \$_smarty_tpl, '{$_filepath}',{$_assign});?>";
135 $_output .= "\$_smarty_tpl->assign({$_assign} , {$_function} ({$_params},\$_smarty_tpl), true);?>";
138 $compiler->has_output
= true;
139 if ($_smarty_tpl->caching
&& !$nocacheParam) {
140 $_output .= "echo Smarty_Internal_Nocache_Insert::compile ('{$_function}',{$_params}, \$_smarty_tpl, '{$_filepath}');?>";
142 $_output .= "echo {$_function}({$_params},\$_smarty_tpl);?>";