library/smarty/libs/sysplugins/smarty_internal_compile_assign.php
changeset 46 f11c31f7fa3e
parent 45 a56e7f9a0463
child 47 03388ec805b4
equal deleted inserted replaced
45:a56e7f9a0463 46:f11c31f7fa3e
     1 <?php
       
     2 /**
       
     3  * Smarty Internal Plugin Compile Assign
       
     4  * Compiles the {assign} tag
       
     5  *
       
     6  * @package    Smarty
       
     7  * @subpackage Compiler
       
     8  * @author     Uwe Tews
       
     9  */
       
    10 
       
    11 /**
       
    12  * Smarty Internal Plugin Compile Assign Class
       
    13  *
       
    14  * @package    Smarty
       
    15  * @subpackage Compiler
       
    16  */
       
    17 class Smarty_Internal_Compile_Assign extends Smarty_Internal_CompileBase {
       
    18     /**
       
    19      * Compiles code for the {assign} tag
       
    20      *
       
    21      * @param  array $args array with attributes from parser
       
    22      * @param  object $compiler compiler object
       
    23      * @param  array $parameter array with compilation parameter
       
    24      *
       
    25      * @return string compiled code
       
    26      */
       
    27     public function compile($args, $compiler, $parameter) {
       
    28         // the following must be assigned at runtime because it will be overwritten in Smarty_Internal_Compile_Append
       
    29         $this->required_attributes = array('var', 'value');
       
    30         $this->shorttag_order = array('var', 'value');
       
    31         $this->optional_attributes = array('scope');
       
    32         $_nocache = 'null';
       
    33         $_scope = Smarty::SCOPE_LOCAL;
       
    34         // check and get attributes
       
    35         $_attr = $this->getAttributes($compiler, $args);
       
    36         // nocache ?
       
    37         if ($compiler->tag_nocache || $compiler->nocache) {
       
    38             $_nocache = 'true';
       
    39             // create nocache var to make it know for further compiling
       
    40             if (isset($compiler->template->tpl_vars[trim($_attr['var'], "'")])) {
       
    41                 $compiler->template->tpl_vars[trim($_attr['var'], "'")]->nocache = true;
       
    42             } else {
       
    43                 $compiler->template->tpl_vars[trim($_attr['var'], "'")] = new Smarty_Variable(null, true);
       
    44             }
       
    45         }
       
    46         // scope setup
       
    47         if (isset($_attr['scope'])) {
       
    48             $_attr['scope'] = trim($_attr['scope'], "'\"");
       
    49             if ($_attr['scope'] == 'parent') {
       
    50                 $_scope = Smarty::SCOPE_PARENT;
       
    51             } elseif ($_attr['scope'] == 'root') {
       
    52                 $_scope = Smarty::SCOPE_ROOT;
       
    53             } elseif ($_attr['scope'] == 'global') {
       
    54                 $_scope = Smarty::SCOPE_GLOBAL;
       
    55             } else {
       
    56                 $compiler->trigger_template_error('illegal value for "scope" attribute', $compiler->lex->taglineno);
       
    57             }
       
    58         }
       
    59         // compiled output
       
    60         if (isset($parameter['smarty_internal_index'])) {
       
    61             $output = "<?php \$_smarty_tpl->createLocalArrayVariable($_attr[var], $_nocache, $_scope);\n\$_smarty_tpl->tpl_vars[$_attr[var]]->value$parameter[smarty_internal_index] = $_attr[value];";
       
    62         } else {
       
    63             // implement Smarty2's behaviour of variables assigned by reference
       
    64             if ($compiler->template->smarty instanceof SmartyBC) {
       
    65                 $output = "<?php if (isset(\$_smarty_tpl->tpl_vars[$_attr[var]])) {\$_smarty_tpl->tpl_vars[$_attr[var]] = clone \$_smarty_tpl->tpl_vars[$_attr[var]];";
       
    66                 $output .= "\n\$_smarty_tpl->tpl_vars[$_attr[var]]->value = $_attr[value]; \$_smarty_tpl->tpl_vars[$_attr[var]]->nocache = $_nocache; \$_smarty_tpl->tpl_vars[$_attr[var]]->scope = $_scope;";
       
    67                 $output .= "\n} else \$_smarty_tpl->tpl_vars[$_attr[var]] = new Smarty_Variable($_attr[value], $_nocache, $_scope);";
       
    68             } else {
       
    69                 $output = "<?php \$_smarty_tpl->tpl_vars[$_attr[var]] = new Smarty_Variable($_attr[value], $_nocache, $_scope);";
       
    70             }
       
    71         }
       
    72         if ($_scope == Smarty::SCOPE_PARENT) {
       
    73             $output .= "\nif (\$_smarty_tpl->parent != null) \$_smarty_tpl->parent->tpl_vars[$_attr[var]] = clone \$_smarty_tpl->tpl_vars[$_attr[var]];";
       
    74         } elseif ($_scope == Smarty::SCOPE_ROOT || $_scope == Smarty::SCOPE_GLOBAL) {
       
    75             $output .= "\n\$_ptr = \$_smarty_tpl->parent; while (\$_ptr != null) {\$_ptr->tpl_vars[$_attr[var]] = clone \$_smarty_tpl->tpl_vars[$_attr[var]]; \$_ptr = \$_ptr->parent; }";
       
    76         }
       
    77         if ($_scope == Smarty::SCOPE_GLOBAL) {
       
    78             $output .= "\nSmarty::\$global_tpl_vars[$_attr[var]] = clone \$_smarty_tpl->tpl_vars[$_attr[var]];";
       
    79         }
       
    80         $output .= '?>';
       
    81 
       
    82         return $output;
       
    83     }
       
    84 }