Exemplo n.º 1
0
 protected function walkForeach(ForeachNode $foreach)
 {
     $exprSym = $this->gensym();
     $ret = "";
     $ret .= "{$exprSym} = " . $this->walk($foreach->getExpression()) . ";";
     if ($foreach->hasElseBody()) {
         $ret .= "{$exprSym}else = true;";
     }
     if ($foreach->hasName()) {
         $name = $foreach->getName();
     } else {
         $name = $foreach->getValueVariableName();
     }
     $ret .= "\$smarty->foreach->{$name} = (object) array('index' => -1, 'iteration' => 0, 'first' => null, 'last' => null, 'total' => count({$exprSym}));";
     $ret .= "foreach ({$exprSym} as ";
     if ($foreach->getKeyVariableName()) {
         $ret .= "\${$foreach->getKeyVariableName()} => ";
     }
     $ret .= "\${$foreach->getValueVariableName()}) {";
     $ret .= "{$exprSym}else = false;";
     $ret .= "++\$smarty->foreach->{$name}->index;";
     $ret .= "++\$smarty->foreach->{$name}->iteration;";
     $ret .= "\$smarty->foreach->{$name}->first = \$smarty->foreach->{$name}->index == 0;";
     $ret .= "\$smarty->foreach->{$name}->last = \$smarty->foreach->{$name}->iteration == \$smarty->foreach->{$name}->total;";
     $ret .= implode("", $this->walkEach((array) $foreach->getBody()));
     $ret .= "}";
     if ($foreach->hasElseBody()) {
         $ret .= "if({$exprSym}else){" . implode("", $this->walkEach($foreach->getElseBody())) . "}";
     }
     return $ret;
 }
Exemplo n.º 2
0
 protected function walkForeach(ForeachNode $foreach)
 {
     return new ForeachNode($foreach->getExpression(), $foreach->getName(), $foreach->getKeyVariableName(), $foreach->getValueVariableName(), $this->walkEach($foreach->getBody()), $foreach->hasElseBody() ? $this->walkEach($foreach->getElseBody()) : null);
 }