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 $compiler->template
->tpl_vars
[trim($_attr['assign'], "'")] = new Smarty_Variable(null, true);
73 if (isset($_attr['script'])) {
74 // script which must be included
75 $_function = "smarty_insert_{$_name}";
76 $_smarty_tpl = $compiler->template
;
78 eval('$_script = ' . $_attr['script'] . ';');
79 if (!isset($compiler->smarty
->security_policy
) && file_exists($_script)) {
80 $_filepath = $_script;
82 if (isset($compiler->smarty
->security_policy
)) {
83 $_dir = $compiler->smarty
->security_policy
->trusted_dir
;
85 $_dir = $compiler->smarty
->trusted_dir
;
88 foreach ((array) $_dir as $_script_dir) {
89 $_script_dir = rtrim($_script_dir, '/\\') . DS
;
90 if (file_exists($_script_dir . $_script)) {
91 $_filepath = $_script_dir . $_script;
97 if ($_filepath == false) {
98 $compiler->trigger_template_error("{insert} missing script file '{$_script}'", $compiler->lex
->taglineno
);
100 // code for script file loading
101 $_output .= "require_once '{$_filepath}' ;";
102 require_once $_filepath;
103 if (!is_callable($_function)) {
104 $compiler->trigger_template_error(" {insert} function '{$_function}' is not callable in script file '{$_script}'", $compiler->lex
->taglineno
);
108 $_function = "insert_{$_name}";
109 // function in PHP script ?
110 if (!is_callable($_function)) {
112 if (!$_function = $compiler->getPlugin($_name, 'insert')) {
113 $compiler->trigger_template_error("{insert} no function or plugin found for '{$_name}'", $compiler->lex
->taglineno
);
117 // delete {insert} standard attributes
118 unset($_attr['name'], $_attr['assign'], $_attr['script'], $_attr['nocache']);
119 // convert attributes into parameter array string
120 $_paramsArray = array();
121 foreach ($_attr as $_key => $_value) {
122 $_paramsArray[] = "'$_key' => $_value";
124 $_params = 'array(' . implode(", ", $_paramsArray) . ')';
126 if (isset($_assign)) {
127 if ($_smarty_tpl->caching
&& !$nocacheParam) {
128 $_output .= "echo Smarty_Internal_Nocache_Insert::compile ('{$_function}',{$_params}, \$_smarty_tpl, '{$_filepath}',{$_assign});?>";
130 $_output .= "\$_smarty_tpl->assign({$_assign} , {$_function} ({$_params},\$_smarty_tpl), true);?>";
133 $compiler->has_output
= true;
134 if ($_smarty_tpl->caching
&& !$nocacheParam) {
135 $_output .= "echo Smarty_Internal_Nocache_Insert::compile ('{$_function}',{$_params}, \$_smarty_tpl, '{$_filepath}');?>";
137 $_output .= "echo {$_function}({$_params},\$_smarty_tpl);?>";