Skip to content

Commit

Permalink
removed one sided trimming of whitespace characters from Mojo::Templa…
Browse files Browse the repository at this point in the history
…te again
  • Loading branch information
kraih committed Jan 24, 2014
1 parent 093ad62 commit 7a971ca
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 30 deletions.
2 changes: 0 additions & 2 deletions Changes
@@ -1,8 +1,6 @@

4.69 2014-01-24
- Improved router to allow format detection for bridges.
- Improved Mojo::Template to allow trimming of whitespace characters around
tags to be limited to one side.

4.68 2014-01-22
- Added Mojo::DOM::Node.
Expand Down
19 changes: 8 additions & 11 deletions lib/Mojo/Template.pm
Expand Up @@ -144,17 +144,17 @@ sub parse {

my $token_re = qr/
(
\Q$tag\E(?:\Q$replace\E|\Q$cmnt\E) # Replace
\Q$tag\E(?:\Q$replace\E|\Q$cmnt\E) # Replace
|
\Q$tag$expr\E(?:\Q$escp\E)?(?:\s*\Q$cpen\E(?!\w))? # Expression
\Q$tag$expr\E(?:\Q$escp\E)?(?:\s*\Q$cpen\E(?!\w))? # Expression
|
\Q$tag\E(?:\s*\Q$cpen\E(?!\w))? # Code
\Q$tag\E(?:\s*\Q$cpen\E(?!\w))? # Code
|
(?:(?<!\w)\Q$cpst\E\s*)?(?:\Q$trim\E{1,3})?\Q$end\E # End
(?:(?<!\w)\Q$cpst\E\s*)?(?:\Q$trim\E)?\Q$end\E # End
)
/x;
my $cpen_re = qr/^(\Q$tag\E)(?:\Q$expr\E)?(?:\Q$escp\E)?\s*\Q$cpen\E/;
my $end_re = qr/^(?:(\Q$cpst\E)\s*)?(\Q$trim\E{1,3})?\Q$end\E$/;
my $end_re = qr/^(?:(\Q$cpst\E)\s*)?(\Q$trim\E)?\Q$end\E$/;

# Split lines
my $state = 'text';
Expand Down Expand Up @@ -193,9 +193,8 @@ sub parse {

# Trim left side
if ($2) {
$trimming = length($2) / length($trim);
$self->_trim(\@token) if $trimming eq 1 || $trimming eq 2;
$trimming = 0 if $trimming eq 2;
$trimming = 1;
$self->_trim(\@token);
}

# Hint at end
Expand Down Expand Up @@ -390,9 +389,7 @@ backslash.
Whitespace characters around tags can be trimmed with a special tag ending.
<%= Trim whitespace characters on both sides of this expression =%>
<%= Trim whitespace characters on the left side of this expression ==%>
<%= Trim whitespace characters on the right side of this expression ===%>
<%= All whitespace characters around this expression will be trimmed =%>
You can capture whole template blocks for reuse later with the C<begin> and
C<end> keywords.
Expand Down
10 changes: 4 additions & 6 deletions lib/Mojolicious/Guides/Rendering.pod
Expand Up @@ -128,14 +128,12 @@ backslash.
in multiple\\
lines

You can also add additional equal signs to the end of a tag to have it
automatically remove surrounding whitespace, this allows you to freely indent
your code without ruining the result.
You can also add an additional equal sign to the end of a tag to have it
automatically remove all surrounding whitespace, this allows you to freely
indent your code without ruining the result.

<% for (1 .. 3) { %>
<%= 'trim both sides' =%>
<%= 'trim left side' ==%>
<%= 'trim right side' ===%>
<%= $foo =%>
<% } %>

Stash values that don't have invalid characters in their name get
Expand Down
12 changes: 1 addition & 11 deletions t/mojo/template.t
Expand Up @@ -29,21 +29,11 @@ $mt->prepend('my @foo = (3, 4);')->parse('<%= @foo %>:<%== @foo %>');
my $output = $mt->build->compile || $mt->interpret;
is $output, "2:2\n", 'same context';

# Trim tag (both sides)
# Trim tag
$mt = Mojo::Template->new;
$output = $mt->render(" ♥ <%= 'test♥' =%> \n");
is $output, ' ♥test♥', 'tag trimmed';

# Trim tag (left side)
$mt = Mojo::Template->new;
$output = $mt->render(" ♥ <%= 'test♥' ==%> \n");
is $output, " ♥test♥ \n", 'tag trimmed';

# Trim tag (right side)
$mt = Mojo::Template->new;
$output = $mt->render(" ♥ <%= 'test♥' ===%> \n");
is $output, ' ♥ test♥', 'tag trimmed';

# Trim expression
$mt = Mojo::Template->new;
$output = $mt->render("<%= '123' %><%= 'test' =%>\n");
Expand Down

0 comments on commit 7a971ca

Please sign in to comment.