/** * Compiles a while-loop into PHTML. * * Notice that if it has no children, we assume it's a do/while loop * and don't print brackets * * @param Node $node the while-node to compile * * @return string The compiled PHTML */ protected function compileWhile(Node $node) { $subject = $node->subject; if ($this->isVariable($subject)) { $subject = "isset({$subject}) ? {$subject} : null"; } $hasChildren = count($node->children) > 0; $isDoWhile = $node->prev() && $node->prev()->type === 'do'; if (!$hasChildren && !$isDoWhile) { $this->throwException('A while-loop without children you loop through is not valid if' . ' there\'s no do-statement before it.', $node); } else { if ($isDoWhile && $hasChildren) { $this->throwException('In a do-while statement the while-part shouldn\'t have any children', $node); } } $phtml = $this->createCode("while ({$subject})" . ($hasChildren ? ' {' : ''), $isDoWhile ? ' ' : '<?php ') . $this->newLine(); if ($hasChildren) { $phtml .= $this->compileChildren($node->children) . $this->newLine(); $phtml .= $this->indent() . $this->createCode('}') . $this->newLine(); } return $phtml; }
/** * Compiles a conditional, either if, elseif, else if or else into PHTML. * * @param Node $node the conditional node to compile * * @return string The compiled PHTML */ protected function compileConditional(Node $node) { $type = $node->conditionType; $subject = $node->subject; if ($subject === 'block') { $subject = '$__block'; } if ($this->isVariable($subject)) { $subject = "isset({$subject}) ? {$subject} : false"; } if ($type === 'unless') { $type = 'if'; $subject = "!({$subject})"; } $isPrevConditional = $node->prev() && $node->prev()->type === 'conditional'; $isNextConditional = $node->next() && $node->next()->type === 'conditional' && $node->next()->conditionType !== 'if'; $prefix = $isPrevConditional ? '' : '<?php '; $suffix = $isNextConditional ? '' : '?>'; $phtml = $type === 'else' ? $this->createCode(' else {', $prefix) : $this->createCode("{$type} ({$subject}) {", $prefix); $phtml .= $this->compileChildren($node->children); $phtml .= $this->newLine() . $this->indent() . $this->createCode("}", '<?php ', $suffix); return $phtml; }