library/smarty/libs/sysplugins/smarty_internal_compile_if.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 If
       
     4  * Compiles the {if} {else} {elseif} {/if} tags
       
     5  *
       
     6  * @package    Smarty
       
     7  * @subpackage Compiler
       
     8  * @author     Uwe Tews
       
     9  */
       
    10 
       
    11 /**
       
    12  * Smarty Internal Plugin Compile If Class
       
    13  *
       
    14  * @package    Smarty
       
    15  * @subpackage Compiler
       
    16  */
       
    17 class Smarty_Internal_Compile_If extends Smarty_Internal_CompileBase {
       
    18     /**
       
    19      * Compiles code for the {if} 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, 'if', array(1, $compiler->nocache));
       
    31         // must whole block be nocache ?
       
    32         $compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
       
    33 
       
    34         if (!array_key_exists("if condition", $parameter)) {
       
    35             $compiler->trigger_template_error("missing if condition", $compiler->lex->taglineno);
       
    36         }
       
    37 
       
    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 .= "if (\$_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 .= "if (\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var'] . "]->value = " . $parameter['if condition']['value'] . ") {?>";
       
    61             }
       
    62 
       
    63             return $_output;
       
    64         } else {
       
    65             return "<?php if ({$parameter['if condition']}) {?>";
       
    66         }
       
    67     }
       
    68 }
       
    69 
       
    70 /**
       
    71  * Smarty Internal Plugin Compile Else Class
       
    72  *
       
    73  * @package    Smarty
       
    74  * @subpackage Compiler
       
    75  */
       
    76 class Smarty_Internal_Compile_Else extends Smarty_Internal_CompileBase {
       
    77     /**
       
    78      * Compiles code for the {else} tag
       
    79      *
       
    80      * @param array $args array with attributes from parser
       
    81      * @param object $compiler compiler object
       
    82      * @param array $parameter array with compilation parameter
       
    83      *
       
    84      * @return string compiled code
       
    85      */
       
    86     public function compile($args, $compiler, $parameter) {
       
    87         list($nesting, $compiler->tag_nocache) = $this->closeTag($compiler, array('if', 'elseif'));
       
    88         $this->openTag($compiler, 'else', array($nesting, $compiler->tag_nocache));
       
    89 
       
    90         return "<?php } else { ?>";
       
    91     }
       
    92 }
       
    93 
       
    94 /**
       
    95  * Smarty Internal Plugin Compile ElseIf Class
       
    96  *
       
    97  * @package    Smarty
       
    98  * @subpackage Compiler
       
    99  */
       
   100 class Smarty_Internal_Compile_Elseif extends Smarty_Internal_CompileBase {
       
   101     /**
       
   102      * Compiles code for the {elseif} tag
       
   103      *
       
   104      * @param array $args array with attributes from parser
       
   105      * @param object $compiler compiler object
       
   106      * @param array $parameter array with compilation parameter
       
   107      *
       
   108      * @return string compiled code
       
   109      */
       
   110     public function compile($args, $compiler, $parameter) {
       
   111         // check and get attributes
       
   112         $_attr = $this->getAttributes($compiler, $args);
       
   113 
       
   114         list($nesting, $compiler->tag_nocache) = $this->closeTag($compiler, array('if', 'elseif'));
       
   115 
       
   116         if (!array_key_exists("if condition", $parameter)) {
       
   117             $compiler->trigger_template_error("missing elseif condition", $compiler->lex->taglineno);
       
   118         }
       
   119 
       
   120         if (is_array($parameter['if condition'])) {
       
   121             $condition_by_assign = true;
       
   122             if ($compiler->nocache) {
       
   123                 $_nocache = ',true';
       
   124                 // create nocache var to make it know for further compiling
       
   125                 if (is_array($parameter['if condition']['var'])) {
       
   126                     $var = trim($parameter['if condition']['var']['var'], "'");
       
   127                 } else {
       
   128                     $var = trim($parameter['if condition']['var'], "'");
       
   129                 }
       
   130                 if (isset($compiler->template->tpl_vars[$var])) {
       
   131                     $compiler->template->tpl_vars[$var]->nocache = true;
       
   132                 } else {
       
   133                     $compiler->template->tpl_vars[$var] = new Smarty_Variable(null, true);
       
   134                 }
       
   135             } else {
       
   136                 $_nocache = '';
       
   137             }
       
   138         } else {
       
   139             $condition_by_assign = false;
       
   140         }
       
   141 
       
   142         if (empty($compiler->prefix_code)) {
       
   143             if ($condition_by_assign) {
       
   144                 $this->openTag($compiler, 'elseif', array($nesting + 1, $compiler->tag_nocache));
       
   145                 if (is_array($parameter['if condition']['var'])) {
       
   146                     $_output = "<?php } else { 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";
       
   147                     $_output .= "if (\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] . "]->value" . $parameter['if condition']['var']['smarty_internal_index'] . " = " . $parameter['if condition']['value'] . ") {?>";
       
   148                 } else {
       
   149                     $_output = "<?php  } else { if (!isset(\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var'] . "])) \$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var'] . "] = new Smarty_Variable(null{$_nocache});";
       
   150                     $_output .= "if (\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var'] . "]->value = " . $parameter['if condition']['value'] . ") {?>";
       
   151                 }
       
   152 
       
   153                 return $_output;
       
   154             } else {
       
   155                 $this->openTag($compiler, 'elseif', array($nesting, $compiler->tag_nocache));
       
   156 
       
   157                 return "<?php } elseif ({$parameter['if condition']}) {?>";
       
   158             }
       
   159         } else {
       
   160             $tmp = '';
       
   161             foreach ($compiler->prefix_code as $code) {
       
   162                 $tmp = $compiler->appendCode($tmp, $code);
       
   163             }
       
   164             $compiler->prefix_code = array();
       
   165             $tmp = $compiler->appendCode("<?php } else {?>", $tmp);
       
   166             $this->openTag($compiler, 'elseif', array($nesting + 1, $compiler->tag_nocache));
       
   167             if ($condition_by_assign) {
       
   168                 if (is_array($parameter['if condition']['var'])) {
       
   169                     $_output = $compiler->appendCode($tmp, "<?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");
       
   170                     $_output .= "if (\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] . "]->value" . $parameter['if condition']['var']['smarty_internal_index'] . " = " . $parameter['if condition']['value'] . ") {?>";
       
   171                 } else {
       
   172                     $_output = $compiler->appendCode($tmp, "<?php if (!isset(\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var'] . "])) \$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var'] . "] = new Smarty_Variable(null{$_nocache});");
       
   173                     $_output .= "if (\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var'] . "]->value = " . $parameter['if condition']['value'] . ") {?>";
       
   174                 }
       
   175 
       
   176                 return $_output;
       
   177             } else {
       
   178                 return $compiler->appendCode($tmp, "<?php if ({$parameter['if condition']}) {?>");
       
   179             }
       
   180         }
       
   181     }
       
   182 }
       
   183 
       
   184 /**
       
   185  * Smarty Internal Plugin Compile Ifclose Class
       
   186  *
       
   187  * @package    Smarty
       
   188  * @subpackage Compiler
       
   189  */
       
   190 class Smarty_Internal_Compile_Ifclose extends Smarty_Internal_CompileBase {
       
   191     /**
       
   192      * Compiles code for the {/if} tag
       
   193      *
       
   194      * @param array $args array with attributes from parser
       
   195      * @param object $compiler compiler object
       
   196      * @param array $parameter array with compilation parameter
       
   197      *
       
   198      * @return string compiled code
       
   199      */
       
   200     public function compile($args, $compiler, $parameter) {
       
   201         // must endblock be nocache?
       
   202         if ($compiler->nocache) {
       
   203             $compiler->tag_nocache = true;
       
   204         }
       
   205         list($nesting, $compiler->nocache) = $this->closeTag($compiler, array('if', 'else', 'elseif'));
       
   206         $tmp = '';
       
   207         for ($i = 0; $i < $nesting; $i++) {
       
   208             $tmp .= '}';
       
   209         }
       
   210 
       
   211         return "<?php {$tmp}?>";
       
   212     }
       
   213 }