4  * Smarty Internal Plugin Compile Insert
 
   6  * Compiles the {insert} tag
 
  14  * Smarty Internal Plugin Compile Insert Class
 
  16 class Smarty_Internal_Compile_Insert extends Smarty_Internal_CompileBase {
 
  17         // attribute definitions
 
  18     public $required_attributes = array('name');
 
  19         public $shorttag_order = array('name');
 
  20     public $optional_attributes = array('_any'); 
 
  23      * Compiles code for the {insert} tag
 
  25      * @param array $args array with attributes from parser
 
  26      * @param object $compiler compiler object
 
  27      * @return string compiled code
 
  29     public function compile($args, $compiler)
 
  31         $this->compiler = $compiler;
 
  32         // check and get attributes
 
  33         $_attr = $this->_get_attributes($args); 
 
  34         // never compile as nocache code
 
  35         $this->compiler->suppressNocacheProcessing = true;
 
  36         $this->compiler->tag_nocache = true;
 
  37         $_smarty_tpl = $compiler->template;
 
  42         // save posible attributes
 
  43         eval('$_name = ' . $_attr['name'] . ';');
 
  44         if (isset($_attr['assign'])) {
 
  45             // output will be stored in a smarty variable instead of being displayed
 
  46             $_assign = $_attr['assign']; 
 
  47             // create variable to make shure that the compiler knows about its nocache status
 
  48             $this->compiler->template->tpl_vars[trim($_attr['assign'], "'")] = new Smarty_Variable(null, true);
 
  50         if (isset($_attr['script'])) {
 
  51             // script which must be included
 
  52             $_function = "smarty_insert_{$_name}";
 
  53             $_smarty_tpl = $compiler->template;
 
  55             eval('$_script = ' . $_attr['script'] . ';');
 
  56             if (!isset($this->compiler->smarty->security_policy) && file_exists($_script)) {
 
  57                 $_filepath = $_script;
 
  59                 if (isset($this->compiler->smarty->security_policy)) {
 
  60                     $_dir = $this->compiler->smarty->security_policy->trusted_dir;
 
  62                     $_dir = $this->compiler->smarty->trusted_dir;
 
  65                     foreach((array)$_dir as $_script_dir) {
 
  66                         if (strpos('/\\', substr($_script_dir, -1)) === false) {
 
  69                         if (file_exists($_script_dir . $_script)) {
 
  70                             $_filepath = $_script_dir . $_script;
 
  76             if ($_filepath == false) {
 
  77                 $this->compiler->trigger_template_error("{insert} missing script file '{$_script}'", $this->compiler->lex->taglineno);
 
  79             // code for script file loading
 
  80             $_output .= "require_once '{$_filepath}' ;";
 
  81             require_once $_filepath;
 
  82             if (!is_callable($_function)) {
 
  83                 $this->compiler->trigger_template_error(" {insert} function '{$_function}' is not callable in script file '{$_script}'", $this->compiler->lex->taglineno);
 
  87             $_function = "insert_{$_name}"; 
 
  88             // function in PHP script ?
 
  89             if (!is_callable($_function)) {
 
  91                 if (!$_function = $this->compiler->getPlugin($_name, 'insert')) {
 
  92                     $this->compiler->trigger_template_error("{insert} no function or plugin found for '{$_name}'", $this->compiler->lex->taglineno);
 
  96         // delete {insert} standard attributes
 
  97         unset($_attr['name'], $_attr['assign'], $_attr['script'], $_attr['nocache']); 
 
  98         // convert attributes into parameter array string
 
  99         $_paramsArray = array();
 
 100         foreach ($_attr as $_key => $_value) {
 
 101             $_paramsArray[] = "'$_key' => $_value";
 
 103         $_params = 'array(' . implode(", ", $_paramsArray) . ')'; 
 
 105         if (isset($_assign)) {
 
 106             if ($_smarty_tpl->caching) {
 
 107                 $_output .= "echo Smarty_Internal_Nocache_Insert::compile ('{$_function}',{$_params}, \$_smarty_tpl, '{$_filepath}',{$_assign});?>";
 
 109                 $_output .= "\$_smarty_tpl->assign({$_assign} , {$_function} ({$_params},\$_smarty_tpl), true);?>";
 
 112             $this->compiler->has_output = true;
 
 113             if ($_smarty_tpl->caching) {
 
 114                 $_output .= "echo Smarty_Internal_Nocache_Insert::compile ('{$_function}',{$_params}, \$_smarty_tpl, '{$_filepath}');?>";
 
 116                 $_output .= "echo {$_function}({$_params},\$_smarty_tpl);?>";