diff --git a/src/View/BakeView.php b/src/View/BakeView.php index 8ef89338dc9..ffe9f29e86e 100644 --- a/src/View/BakeView.php +++ b/src/View/BakeView.php @@ -92,10 +92,11 @@ protected function _evaluate($viewFile, $dataForView) { ]; $viewString = str_replace(array_keys($unPhp), array_values($unPhp), $viewString); + $viewString = preg_replace('/\n[ \t]+<% /', "\n<% ", $viewString); $viewString = str_replace(array_keys($templatify), array_values($templatify), $viewString); $viewString = preg_replace('/<\?=(.*)\?>\n(.)/', "\n\n$2", $viewString); - $this->__viewFile = TMP . Inflector::slug(str_replace(ROOT, '', $viewFile)) . '.php'; + $this->__viewFile = TMP . Inflector::slug(preg_replace('@.*Template[/\\\\]@', '', $viewFile)) . '.php'; file_put_contents($this->__viewFile, $viewString); unset($randomString, $templatify, $viewFile, $viewString); diff --git a/tests/TestCase/View/BakeViewTest.php b/tests/TestCase/View/BakeViewTest.php index a6fb5052aae..66500517b19 100644 --- a/tests/TestCase/View/BakeViewTest.php +++ b/tests/TestCase/View/BakeViewTest.php @@ -99,4 +99,25 @@ public function testRenderNewlines() { 'Tags at the end of a line should not swallow new lines when rendered' ); } + + public function testSwallowLeadingWhitespace() { + $result = $this->View->render('view_tests/leading-whitespace'); + $expected = $this->_getCompareTemplate('leading-whitespace'); + + $this->assertSame( + $expected, + $result, + 'Leading whitespace in bake templates should not result in leading/loose whitespace in rendered results' + ); + } + +/** + * _getCompareTemplate + * + * @param string $template + * @return string + */ + protected function _getCompareTemplate($template) { + return file_get_contents(dirname(dirname(__DIR__)) . "/test_app/TestApp/Template/Bake/view_tests_compare/$template.ctp"); + } } diff --git a/tests/test_app/TestApp/Template/Bake/view_tests/leading-whitespace.ctp b/tests/test_app/TestApp/Template/Bake/view_tests/leading-whitespace.ctp new file mode 100644 index 00000000000..2685e481370 --- /dev/null +++ b/tests/test_app/TestApp/Template/Bake/view_tests/leading-whitespace.ctp @@ -0,0 +1,13 @@ +<% foreach([1,2,3] as $number): %> + <%= $number %> +<% endforeach; %> + +<% foreach([1,2,3] as $number): %> + number +<% endforeach; %> + +This should make no difference: + <% foreach([1,2,3] as $number): %> + <%= $number %> + <% endforeach; %> +And the previous line should not have leading whitespace. diff --git a/tests/test_app/TestApp/Template/Bake/view_tests/newlines.ctp b/tests/test_app/TestApp/Template/Bake/view_tests/newlines.ctp new file mode 100644 index 00000000000..d5314c16f5e --- /dev/null +++ b/tests/test_app/TestApp/Template/Bake/view_tests/newlines.ctp @@ -0,0 +1,4 @@ +There should be a newline about here: <%= "" %> +And this should be on the next line. + +There should be no new line after this<%= "" %> diff --git a/tests/test_app/TestApp/Template/Bake/view_tests_compare/leading-whitespace.ctp b/tests/test_app/TestApp/Template/Bake/view_tests_compare/leading-whitespace.ctp new file mode 100644 index 00000000000..38b06d92d1f --- /dev/null +++ b/tests/test_app/TestApp/Template/Bake/view_tests_compare/leading-whitespace.ctp @@ -0,0 +1,13 @@ + 1 + 2 + 3 + + number + number + number + +This should make no difference: + 1 + 2 + 3 +And the previous line should not have leading whitespace.