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');
29 * Attribute definition: Overwrites base class.
32 * @see Smarty_Internal_CompileBase
34 public $shorttag_order = array('name');
37 * Attribute definition: Overwrites base class.
40 * @see Smarty_Internal_CompileBase
42 public $optional_attributes = array('_any');
45 * Compiles code for the {insert} tag
47 * @param array $args array with attributes from parser
48 * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
50 * @return string compiled code
51 * @throws \SmartyCompilerException
53 public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
55 // check and get attributes
56 $_attr = $this->getAttributes($compiler, $args);
57 //Does tag create output
58 $compiler->has_output = isset($_attr[ 'assign' ]) ? false : true;
60 $nocacheParam = $compiler->template->caching && ($compiler->tag_nocache || $compiler->nocache);
62 // do not compile as nocache code
63 $compiler->suppressNocacheProcessing = true;
65 $compiler->tag_nocache = true;
66 $_smarty_tpl = $compiler->template;
71 // save possible attributes
72 eval('$_name = @' . $_attr[ 'name' ] . ';');
73 if (isset($_attr[ 'assign' ])) {
74 // output will be stored in a smarty variable instead of being displayed
75 $_assign = $_attr[ 'assign' ];
76 // create variable to make sure that the compiler knows about its nocache status
77 $var = trim($_attr[ 'assign' ], "'");
78 if (isset($compiler->template->tpl_vars[ $var ])) {
79 $compiler->template->tpl_vars[ $var ]->nocache = true;
81 $compiler->template->tpl_vars[ $var ] = new Smarty_Variable(null, true);
84 if (isset($_attr[ 'script' ])) {
85 // script which must be included
86 $_function = "smarty_insert_{$_name}";
87 $_smarty_tpl = $compiler->template;
89 eval('$_script = @' . $_attr[ 'script' ] . ';');
90 if (!isset($compiler->smarty->security_policy) && file_exists($_script)) {
91 $_filepath = $_script;
93 if (isset($compiler->smarty->security_policy)) {
94 $_dir = $compiler->smarty->security_policy->trusted_dir;
96 $_dir = $compiler->smarty instanceof SmartyBC ? $compiler->smarty->trusted_dir : null;
99 foreach ((array) $_dir as $_script_dir) {
100 $_script_dir = rtrim($_script_dir, '/\\') . $compiler->smarty->ds;
101 if (file_exists($_script_dir . $_script)) {
102 $_filepath = $_script_dir . $_script;
108 if ($_filepath == false) {
109 $compiler->trigger_template_error("{insert} missing script file '{$_script}'", null, true);
111 // code for script file loading
112 $_output .= "require_once '{$_filepath}' ;";
113 require_once $_filepath;
114 if (!is_callable($_function)) {
115 $compiler->trigger_template_error(" {insert} function '{$_function}' is not callable in script file '{$_script}'",
120 $_function = "insert_{$_name}";
121 // function in PHP script ?
122 if (!is_callable($_function)) {
124 if (!$_function = $compiler->getPlugin($_name, 'insert')) {
125 $compiler->trigger_template_error("{insert} no function or plugin found for '{$_name}'", null,
130 // delete {insert} standard attributes
131 unset($_attr[ 'name' ], $_attr[ 'assign' ], $_attr[ 'script' ], $_attr[ 'nocache' ]);
132 // convert attributes into parameter array string
133 $_paramsArray = array();
134 foreach ($_attr as $_key => $_value) {
135 $_paramsArray[] = "'$_key' => $_value";
137 $_params = 'array(' . implode(", ", $_paramsArray) . ')';
139 if (isset($_assign)) {
140 if ($_smarty_tpl->caching && !$nocacheParam) {
141 $_output .= "echo Smarty_Internal_Nocache_Insert::compile ('{$_function}',{$_params}, \$_smarty_tpl, '{$_filepath}',{$_assign});?>";
143 $_output .= "\$_smarty_tpl->assign({$_assign} , {$_function} ({$_params},\$_smarty_tpl), true);?>";
146 if ($_smarty_tpl->caching && !$nocacheParam) {
147 $_output .= "echo Smarty_Internal_Nocache_Insert::compile ('{$_function}',{$_params}, \$_smarty_tpl, '{$_filepath}');?>";
149 $_output .= "echo {$_function}({$_params},\$_smarty_tpl);?>";