smarty_internal_compile_for.php (6462B)
1 <?php 2 /** 3 * Smarty Internal Plugin Compile For 4 * Compiles the {for} {forelse} {/for} tags 5 * 6 * @package Smarty 7 * @subpackage Compiler 8 * @author Uwe Tews 9 */ 10 11 /** 12 * Smarty Internal Plugin Compile For Class 13 * 14 * @package Smarty 15 * @subpackage Compiler 16 */ 17 class Smarty_Internal_Compile_For extends Smarty_Internal_CompileBase 18 { 19 /** 20 * Compiles code for the {for} tag 21 * Smarty 3 does implement two different syntax's: 22 * - {for $var in $array} 23 * For looping over arrays or iterators 24 * - {for $x=0; $x<$y; $x++} 25 * For general loops 26 * The parser is generating different sets of attribute by which this compiler can 27 * determine which syntax is used. 28 * 29 * @param array $args array with attributes from parser 30 * @param object $compiler compiler object 31 * @param array $parameter array with compilation parameter 32 * 33 * @return string compiled code 34 */ 35 public function compile($args, $compiler, $parameter) 36 { 37 if ($parameter == 0) { 38 $this->required_attributes = array('start', 'to'); 39 $this->optional_attributes = array('max', 'step'); 40 } else { 41 $this->required_attributes = array('start', 'ifexp', 'var', 'step'); 42 $this->optional_attributes = array(); 43 } 44 // check and get attributes 45 $_attr = $this->getAttributes($compiler, $args); 46 47 $output = "<?php "; 48 if ($parameter == 1) { 49 foreach ($_attr['start'] as $_statement) { 50 if (is_array($_statement['var'])) { 51 $var = $_statement['var']['var']; 52 $index = $_statement['var']['smarty_internal_index']; 53 } else { 54 $var = $_statement['var']; 55 $index = ''; 56 } 57 $output .= " \$_smarty_tpl->tpl_vars[$var] = new Smarty_Variable;"; 58 $output .= " \$_smarty_tpl->tpl_vars[$var]->value{$index} = $_statement[value];\n"; 59 } 60 if (is_array($_attr['var'])) { 61 $var = $_attr['var']['var']; 62 $index = $_attr['var']['smarty_internal_index']; 63 } else { 64 $var = $_attr['var']; 65 $index = ''; 66 } 67 $output .= " if ($_attr[ifexp]) { for (\$_foo=true;$_attr[ifexp]; \$_smarty_tpl->tpl_vars[$var]->value{$index}$_attr[step]) {\n"; 68 } else { 69 $_statement = $_attr['start']; 70 if (is_array($_statement['var'])) { 71 $var = $_statement['var']['var']; 72 $index = $_statement['var']['smarty_internal_index']; 73 } else { 74 $var = $_statement['var']; 75 $index = ''; 76 } 77 $output .= "\$_smarty_tpl->tpl_vars[$var] = new Smarty_Variable;"; 78 if (isset($_attr['step'])) { 79 $output .= "\$_smarty_tpl->tpl_vars[$var]->step = $_attr[step];"; 80 } else { 81 $output .= "\$_smarty_tpl->tpl_vars[$var]->step = 1;"; 82 } 83 if (isset($_attr['max'])) { 84 $output .= "\$_smarty_tpl->tpl_vars[$var]->total = (int) min(ceil((\$_smarty_tpl->tpl_vars[$var]->step > 0 ? $_attr[to]+1 - ($_statement[value]) : $_statement[value]-($_attr[to])+1)/abs(\$_smarty_tpl->tpl_vars[$var]->step)),$_attr[max]);\n"; 85 } else { 86 $output .= "\$_smarty_tpl->tpl_vars[$var]->total = (int) ceil((\$_smarty_tpl->tpl_vars[$var]->step > 0 ? $_attr[to]+1 - ($_statement[value]) : $_statement[value]-($_attr[to])+1)/abs(\$_smarty_tpl->tpl_vars[$var]->step));\n"; 87 } 88 $output .= "if (\$_smarty_tpl->tpl_vars[$var]->total > 0) {\n"; 89 $output .= "for (\$_smarty_tpl->tpl_vars[$var]->value{$index} = $_statement[value], \$_smarty_tpl->tpl_vars[$var]->iteration = 1;\$_smarty_tpl->tpl_vars[$var]->iteration <= \$_smarty_tpl->tpl_vars[$var]->total;\$_smarty_tpl->tpl_vars[$var]->value{$index} += \$_smarty_tpl->tpl_vars[$var]->step, \$_smarty_tpl->tpl_vars[$var]->iteration++) {\n"; 90 $output .= "\$_smarty_tpl->tpl_vars[$var]->first = \$_smarty_tpl->tpl_vars[$var]->iteration == 1;"; 91 $output .= "\$_smarty_tpl->tpl_vars[$var]->last = \$_smarty_tpl->tpl_vars[$var]->iteration == \$_smarty_tpl->tpl_vars[$var]->total;"; 92 } 93 $output .= "?>"; 94 95 $this->openTag($compiler, 'for', array('for', $compiler->nocache)); 96 // maybe nocache because of nocache variables 97 $compiler->nocache = $compiler->nocache | $compiler->tag_nocache; 98 // return compiled code 99 return $output; 100 } 101 } 102 103 /** 104 * Smarty Internal Plugin Compile Forelse Class 105 * 106 * @package Smarty 107 * @subpackage Compiler 108 */ 109 class Smarty_Internal_Compile_Forelse extends Smarty_Internal_CompileBase 110 { 111 /** 112 * Compiles code for the {forelse} tag 113 * 114 * @param array $args array with attributes from parser 115 * @param object $compiler compiler object 116 * @param array $parameter array with compilation parameter 117 * 118 * @return string compiled code 119 */ 120 public function compile($args, $compiler, $parameter) 121 { 122 // check and get attributes 123 $_attr = $this->getAttributes($compiler, $args); 124 125 list($openTag, $nocache) = $this->closeTag($compiler, array('for')); 126 $this->openTag($compiler, 'forelse', array('forelse', $nocache)); 127 128 return "<?php }} else { ?>"; 129 } 130 } 131 132 /** 133 * Smarty Internal Plugin Compile Forclose Class 134 * 135 * @package Smarty 136 * @subpackage Compiler 137 */ 138 class Smarty_Internal_Compile_Forclose extends Smarty_Internal_CompileBase 139 { 140 /** 141 * Compiles code for the {/for} tag 142 * 143 * @param array $args array with attributes from parser 144 * @param object $compiler compiler object 145 * @param array $parameter array with compilation parameter 146 * 147 * @return string compiled code 148 */ 149 public function compile($args, $compiler, $parameter) 150 { 151 // check and get attributes 152 $_attr = $this->getAttributes($compiler, $args); 153 // must endblock be nocache? 154 if ($compiler->nocache) { 155 $compiler->tag_nocache = true; 156 } 157 158 list($openTag, $compiler->nocache) = $this->closeTag($compiler, array('for', 'forelse')); 159 160 if ($openTag == 'forelse') { 161 return "<?php } ?>"; 162 } else { 163 return "<?php }} ?>"; 164 } 165 } 166 }