From 50b359445849c52fd9e02a884c04b765042bd0ea Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Wed, 14 Mar 2012 17:41:38 -0700 Subject: [PATCH] Fix failing `partials` example. Only prepend $indent to the first output on a new line. --- src/Mustache/Compiler.php | 35 +++++++++++++++++++++++------ test/Mustache/Test/CompilerTest.php | 4 ++-- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/Mustache/Compiler.php b/src/Mustache/Compiler.php index bfe53f7..daebe2e 100644 --- a/src/Mustache/Compiler.php +++ b/src/Mustache/Compiler.php @@ -20,6 +20,7 @@ class Compiler { private $sections; private $source; + private $indentNextLine; /** * Compile a Mustache token parse tree into PHP source code. @@ -31,8 +32,9 @@ class Compiler { * @return string Generated PHP source code */ public function compile($source, array $tree, $name) { - $this->sections = array(); - $this->source = $source; + $this->sections = array(); + $this->source = $source; + $this->indentNextLine = true; return $this->writeCode($tree, $name); } @@ -243,7 +245,7 @@ class Compiler { ->loadLambda((string) call_user_func($value)) ->renderInternal($context, $indent); } - $buffer .= $indent . %s; + $buffer .= %s%s; '; const VARIABLE_ESCAPED = 'htmlspecialchars($value, ENT_COMPAT, $this->mustache->getCharset())'; @@ -259,13 +261,13 @@ class Compiler { private function variable($id, $escape, $level) { $method = $this->getFindMethod($id); $id = ($method !== 'last') ? var_export($id, true) : ''; - $escape = $escape ? self::VARIABLE_ESCAPED : '$value'; + $value = $escape ? self::VARIABLE_ESCAPED : '$value'; - return sprintf($this->prepare(self::VARIABLE, $level), $method, $id, $escape); + return sprintf($this->prepare(self::VARIABLE, $level), $method, $id, $this->flushIndent(), $value); } const LINE = '$buffer .= "\n";'; - const TEXT = '$buffer .= $indent . %s;'; + const TEXT = '$buffer .= %s%s;'; /** * Generate Mustache Template output Buffer call PHP source. @@ -277,9 +279,11 @@ class Compiler { */ private function text($text, $level) { if ($text === "\n") { + $this->indentNextLine = true; + return $this->prepare(self::LINE, $level); } else { - return sprintf($this->prepare(self::TEXT, $level), var_export($text, true)); + return sprintf($this->prepare(self::TEXT, $level), $this->flushIndent(), var_export($text, true)); } } @@ -323,4 +327,21 @@ class Compiler { return 'findDot'; } } + + const LINE_INDENT = '$indent . '; + + /** + * Get the current $indent prefix to write to the buffer. + * + * @return string "$indent . " or "" + */ + private function flushIndent() { + if ($this->indentNextLine) { + $this->indentNextLine = false; + + return self::LINE_INDENT; + } else { + return ''; + } + } } diff --git a/test/Mustache/Test/CompilerTest.php b/test/Mustache/Test/CompilerTest.php index e08fa45..b53879b 100644 --- a/test/Mustache/Test/CompilerTest.php +++ b/test/Mustache/Test/CompilerTest.php @@ -65,9 +65,9 @@ class CompilerTest extends \PHPUnit_Framework_TestCase { '$buffer .= $indent . \'foo\'', '$buffer .= "\n"', '$value = $context->find(\'name\');', - '$buffer .= $indent . htmlspecialchars($value, ENT_COMPAT, $this->mustache->getCharset());', + '$buffer .= htmlspecialchars($value, ENT_COMPAT, $this->mustache->getCharset());', '$value = $context->last();', - '$buffer .= $indent . \'\\\'bar\\\'\';', + '$buffer .= \'\\\'bar\\\'\';', 'return $buffer;', ) ),