library/smarty/libs/sysplugins/smarty_internal_compile_while.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 While
       
     4  * Compiles the {while} tag
       
     5  *
       
     6  * @package    Smarty
       
     7  * @subpackage Compiler
       
     8  * @author     Uwe Tews
       
     9  */
       
    10 
       
    11 /**
       
    12  * Smarty Internal Plugin Compile While Class
       
    13  *
       
    14  * @package    Smarty
       
    15  * @subpackage Compiler
       
    16  */
       
    17 class Smarty_Internal_Compile_While extends Smarty_Internal_CompileBase {
       
    18     /**
       
    19      * Compiles code for the {while} 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         // check and get attributes
       
    29         $_attr = $this->getAttributes($compiler, $args);
       
    30         $this->openTag($compiler, 'while', $compiler->nocache);
       
    31 
       
    32         if (!array_key_exists("if condition", $parameter)) {
       
    33             $compiler->trigger_template_error("missing while condition", $compiler->lex->taglineno);
       
    34         }
       
    35 
       
    36         // maybe nocache because of nocache variables
       
    37         $compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
       
    38         if (is_array($parameter['if condition'])) {
       
    39             if ($compiler->nocache) {
       
    40                 $_nocache = ',true';
       
    41                 // create nocache var to make it know for further compiling
       
    42                 if (is_array($parameter['if condition']['var'])) {
       
    43                     $var = trim($parameter['if condition']['var']['var'], "'");
       
    44                 } else {
       
    45                     $var = trim($parameter['if condition']['var'], "'");
       
    46                 }
       
    47                 if (isset($compiler->template->tpl_vars[$var])) {
       
    48                     $compiler->template->tpl_vars[$var]->nocache = true;
       
    49                 } else {
       
    50                     $compiler->template->tpl_vars[$var] = new Smarty_Variable(null, true);
       
    51                 }
       
    52             } else {
       
    53                 $_nocache = '';
       
    54             }
       
    55             if (is_array($parameter['if condition']['var'])) {
       
    56                 $_output = "<?php if (!isset(\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] . "]) || !is_array(\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] . "]->value)) \$_smarty_tpl->createLocalArrayVariable(" . $parameter['if condition']['var']['var'] . "$_nocache);\n";
       
    57                 $_output .= "while (\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] . "]->value" . $parameter['if condition']['var']['smarty_internal_index'] . " = " . $parameter['if condition']['value'] . ") {?>";
       
    58             } else {
       
    59                 $_output = "<?php if (!isset(\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var'] . "])) \$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var'] . "] = new Smarty_Variable(null{$_nocache});";
       
    60                 $_output .= "while (\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var'] . "]->value = " . $parameter['if condition']['value'] . ") {?>";
       
    61             }
       
    62 
       
    63             return $_output;
       
    64         } else {
       
    65             return "<?php while ({$parameter['if condition']}) {?>";
       
    66         }
       
    67     }
       
    68 }
       
    69 
       
    70 /**
       
    71  * Smarty Internal Plugin Compile Whileclose Class
       
    72  *
       
    73  * @package    Smarty
       
    74  * @subpackage Compiler
       
    75  */
       
    76 class Smarty_Internal_Compile_Whileclose extends Smarty_Internal_CompileBase {
       
    77     /**
       
    78      * Compiles code for the {/while} tag
       
    79      *
       
    80      * @param  array $args array with attributes from parser
       
    81      * @param  object $compiler compiler object
       
    82      *
       
    83      * @return string compiled code
       
    84      */
       
    85     public function compile($args, $compiler) {
       
    86         // must endblock be nocache?
       
    87         if ($compiler->nocache) {
       
    88             $compiler->tag_nocache = true;
       
    89         }
       
    90         $compiler->nocache = $this->closeTag($compiler, array('while'));
       
    91 
       
    92         return "<?php }?>";
       
    93     }
       
    94 }