/framework/vendor/smarty3/lib/libs/sysplugins/smarty_internal_compile_for.php
PHP | 133 lines | 69 code | 7 blank | 57 comment | 16 complexity | 37da9e42a61db3d8cf012d0af280de71 MD5 | raw file
1<?php 2/** 3* Smarty Internal Plugin Compile For 4* 5* Compiles the {for} {forelse} {/for} tags 6* 7* @package Smarty 8* @subpackage Compiler 9* @author Uwe Tews 10*/ 11/** 12* Smarty Internal Plugin Compile For Class 13*/ 14class Smarty_Internal_Compile_For extends Smarty_Internal_CompileBase { 15 /** 16 * Compiles code for the {for} tag 17 * 18 * Smarty 3 does implement two different sytaxes: 19 * 20 * - {for $var in $array} 21 * For looping over arrays or iterators 22 * 23 * - {for $x=0; $x<$y; $x++} 24 * For general loops 25 * 26 * The parser is gereration different sets of attribute by which this compiler can 27 * determin which syntax is used. 28 * 29 * @param array $args array with attributes from parser 30 * @param object $compiler compiler object 31 * @return string compiled code 32 */ 33 public function compile($args, $compiler) 34 { 35 $this->compiler = $compiler; 36 // {for $x=0; $x<$y; $x++} syntax 37 if (isset($args['ifexp'])) { 38 $this->required_attributes = array('ifexp', 'start', 'loop', 'varloop'); 39 } else { 40 $this->required_attributes = array('start', 'to'); 41 $this->optional_attributes = array('step', 'max'); 42 } 43 // check and get attributes 44 $_attr = $this->_get_attributes($args); 45 46 $this->_open_tag('for', array('for', $this->compiler->nocache)); 47 // maybe nocache because of nocache variables 48 $this->compiler->nocache = $this->compiler->nocache | $this->compiler->tag_nocache; 49 50 $output = "<?php "; 51 if (isset($_attr['ifexp'])) { 52 foreach ($_attr['start'] as $_statement) { 53 $output .= " \$_smarty_tpl->tpl_vars[$_statement[var]] = new Smarty_Variable;"; 54 $output .= " \$_smarty_tpl->tpl_vars[$_statement[var]]->value = $_statement[value];\n"; 55 } 56 $output .= " if ($_attr[ifexp]){ for (\$_foo=true;$_attr[ifexp]; \$_smarty_tpl->tpl_vars[$_attr[varloop]]->value$_attr[loop]){\n"; 57 } else { 58 $_statement = $_attr['start']; 59 $output .= "\$_smarty_tpl->tpl_vars[$_statement[var]] = new Smarty_Variable;"; 60 if (isset($_attr['step'])) { 61 $output .= "\$_smarty_tpl->tpl_vars[$_statement[var]]->step = $_attr[step];"; 62 } else { 63 $output .= "\$_smarty_tpl->tpl_vars[$_statement[var]]->step = ($_attr[to] - ($_statement[value]) < 0) ? -1 : 1;"; 64 } 65 if (isset($_attr['max'])) { 66 $output .= "\$_smarty_tpl->tpl_vars[$_statement[var]]->total = (int)min(ceil((\$_smarty_tpl->tpl_vars[$_statement[var]]->step > 0 ? $_attr[to]+1 - $_statement[value] : $_statement[value]-($_attr[to])+1)/abs(\$_smarty_tpl->tpl_vars[$_statement[var]]->step)),$_attr[max]);\n"; 67 } else { 68 $output .= "\$_smarty_tpl->tpl_vars[$_statement[var]]->total = (int)ceil((\$_smarty_tpl->tpl_vars[$_statement[var]]->step > 0 ? $_attr[to]+1 - $_statement[value] : $_statement[value]-($_attr[to])+1)/abs(\$_smarty_tpl->tpl_vars[$_statement[var]]->step));\n"; 69 } 70 $output .= "if (\$_smarty_tpl->tpl_vars[$_statement[var]]->total > 0){\n"; 71 $output .= "for (\$_smarty_tpl->tpl_vars[$_statement[var]]->value = $_statement[value], \$_smarty_tpl->tpl_vars[$_statement[var]]->iteration = 1;\$_smarty_tpl->tpl_vars[$_statement[var]]->iteration <= \$_smarty_tpl->tpl_vars[$_statement[var]]->total;\$_smarty_tpl->tpl_vars[$_statement[var]]->value += \$_smarty_tpl->tpl_vars[$_statement[var]]->step, \$_smarty_tpl->tpl_vars[$_statement[var]]->iteration++){\n"; 72 $output .= "\$_smarty_tpl->tpl_vars[$_statement[var]]->first = \$_smarty_tpl->tpl_vars[$_statement[var]]->iteration == 1;"; 73 $output .= "\$_smarty_tpl->tpl_vars[$_statement[var]]->last = \$_smarty_tpl->tpl_vars[$_statement[var]]->iteration == \$_smarty_tpl->tpl_vars[$_statement[var]]->total;"; 74 } 75 $output .= "?>"; 76 // return compiled code 77 return $output; 78 } 79} 80 81/** 82* Smarty Internal Plugin Compile Forelse Class 83*/ 84class Smarty_Internal_Compile_Forelse extends Smarty_Internal_CompileBase { 85 /** 86 * Compiles code for the {forelse} tag 87 * 88 * @param array $args array with attributes from parser 89 * @param object $compiler compiler object 90 * @return string compiled code 91 */ 92 public function compile($args, $compiler) 93 { 94 $this->compiler = $compiler; 95 // check and get attributes 96 $_attr = $this->_get_attributes($args); 97 98 list($_open_tag, $nocache) = $this->_close_tag(array('for')); 99 $this->_open_tag('forelse', array('forelse', $nocache)); 100 return "<?php }} else { ?>"; 101 } 102} 103 104/** 105* Smarty Internal Plugin Compile Forclose Class 106*/ 107class Smarty_Internal_Compile_Forclose extends Smarty_Internal_CompileBase { 108 /** 109 * Compiles code for the {/for} tag 110 * 111 * @param array $args array with attributes from parser 112 * @param object $compiler compiler object 113 * @return string compiled code 114 */ 115 public function compile($args, $compiler) 116 { 117 $this->compiler = $compiler; 118 // check and get attributes 119 $_attr = $this->_get_attributes($args); 120 // must endblock be nocache? 121 if ($this->compiler->nocache) { 122 $this->compiler->tag_nocache = true; 123 } 124 125 list($_open_tag, $this->compiler->nocache) = $this->_close_tag(array('for', 'forelse')); 126 if ($_open_tag == 'forelse') 127 return "<?php } ?>"; 128 else 129 return "<?php }} ?>"; 130 } 131} 132 133?>