Skip to content

Commit

Permalink
swallow leading whitespace before template tags
Browse files Browse the repository at this point in the history
this makes it easier to write templates without introducing, or needing
to think about, leaving leading whitespace lying arounswallow leading
whitespace before template tags

this makes it easier to write templates without introducing, or needing
to think about, leaving leading whitespace lying aroundd
  • Loading branch information
AD7six committed Nov 14, 2014
1 parent 9594e62 commit 915fd95
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/View/BakeView.php
Expand Up @@ -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(.)/', "<?=$1?>\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);
Expand Down
21 changes: 21 additions & 0 deletions tests/TestCase/View/BakeViewTest.php
Expand Up @@ -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");
}
}
@@ -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.
4 changes: 4 additions & 0 deletions 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<%= "" %>
@@ -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.

0 comments on commit 915fd95

Please sign in to comment.