Skip to content

Commit

Permalink
added MacroNode::$replaced to distinguish between the variables and c…
Browse files Browse the repository at this point in the history
…ontrol logic
  • Loading branch information
dg committed Aug 22, 2014
1 parent f4d8efb commit b2b1ce4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/Latte/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ public function openMacro($name, $args = NULL, $modifiers = NULL, $isRightmost =
{
$node = $this->expandMacro($name, $args, $modifiers, $nPrefix);
if ($node->isEmpty) {
$this->writeCode($node->openingCode, $this->output, $isRightmost);
$this->writeCode($node->openingCode, $this->output, $node->replaced, $isRightmost);
} else {
$this->macroNode = $node;
$node->saved = array(& $this->output, $isRightmost);
Expand Down Expand Up @@ -419,19 +419,24 @@ public function closeMacro($name, $args = NULL, $modifiers = NULL, $isRightmost
$node->macro->nodeClosed($node);

$this->output = & $node->saved[0];
$this->writeCode($node->openingCode, $this->output, $node->saved[1]);
$this->writeCode($node->closingCode, $node->content, $isRightmost, $isLeftmost);
$this->writeCode($node->openingCode, $this->output, $node->replaced, $node->saved[1]);
$this->writeCode($node->closingCode, $node->content, $node->replaced, $isRightmost, $isLeftmost);
$this->output .= $node->content;
return $node;
}


private function writeCode($code, & $output, $isRightmost, $isLeftmost = NULL)
private function writeCode($code, & $output, $replaced, $isRightmost, $isLeftmost = NULL)
{
if ($isRightmost) {
$leftOfs = strrpos("\n$output", "\n");
$isLeftmost = $isLeftmost === NULL ? trim(substr($output, $leftOfs)) === '' : $isLeftmost;
if ($isLeftmost && !preg_match('#<\?php.*\secho\s#As', $code)) {
if ($isLeftmost === NULL) {
$isLeftmost = trim(substr($output, $leftOfs)) === '';
}
if ($replaced === NULL) {
$replaced = preg_match('#<\?php.*\secho\s#As', $code);
}
if ($isLeftmost && !$replaced) {
$output = substr($output, 0, $leftOfs); // alone macro without output -> remove indentation
} elseif (substr($code, -2) === '?>') {
$code .= "\n"; // double newline to avoid newline eating by PHP
Expand Down
3 changes: 3 additions & 0 deletions src/Latte/MacroNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ class MacroNode extends Object
/** @var bool */
public $closing = FALSE;

/** @var bool has output? */
public $replaced;

/** @var MacroTokens */
public $tokenizer;

Expand Down

0 comments on commit b2b1ce4

Please sign in to comment.