Skip to content

Commit

Permalink
improved Mojo::Template performance slightly
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed May 18, 2013
1 parent 7ac17f4 commit 331429d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,6 +1,7 @@

4.01 2013-05-17
4.01 2013-05-18
- Improved documentation.
- Improved tests.

4.0 2013-05-15
- Code name "Top Hat", this is a major release.
Expand Down
22 changes: 12 additions & 10 deletions lib/Mojo/Template.pm
Expand Up @@ -27,6 +27,7 @@ sub build {
my $self = shift;

my (@lines, $cpst, $multi);
my $escape = $self->auto_escape;
for my $line (@{$self->tree}) {
push @lines, '';
for (my $j = 0; $j < @{$line}; $j += 2) {
Expand Down Expand Up @@ -64,7 +65,6 @@ sub build {
unless ($multi) {

# Escaped
my $escape = $self->auto_escape;
if (($type eq 'escp' && !$escape) || ($type eq 'expr' && $escape)) {
$lines[-1] .= "\$_M .= _escape";
$lines[-1] .= " scalar $value" if length $value;
Expand Down Expand Up @@ -126,10 +126,11 @@ sub interpret {
}

sub parse {
my ($self, $tmpl) = @_;
my $self = shift;

# Clean start
delete $self->template($tmpl)->{tree};
my $template = @_ ? $self->template(shift)->template : $self->template;
my $tree = $self->tree([])->tree;

my $tag = $self->tag_start;
my $replace = $self->replace_mark;
Expand Down Expand Up @@ -175,7 +176,7 @@ sub parse {
# Split lines
my $state = 'text';
my ($trimming, @capture_token);
for my $line (split /\n/, $tmpl) {
for my $line (split /\n/, $template) {
$trimming = 0 if $state eq 'text';

# Turn Perl line into mixed line
Expand Down Expand Up @@ -247,27 +248,27 @@ sub parse {
@capture_token = ();
}
}
push @{$self->tree}, \@token;
push @$tree, \@token;
}

return $self;
}

sub render {
my $self = shift->parse(shift)->build;
return $self->compile || $self->interpret(@_);
my $self = shift;
return $self->parse(shift)->build->compile || $self->interpret(@_);
}

sub render_file {
my ($self, $path) = (shift, shift);

$self->name($path) unless defined $self->{name};
my $tmpl = slurp $path;
my $template = slurp $path;
my $encoding = $self->encoding;
croak qq{Template "$path" has invalid encoding.}
if $encoding && !defined($tmpl = decode $encoding, $tmpl);
if $encoding && !defined($template = decode $encoding, $template);

return $self->render($tmpl, @_);
return $self->render($template, @_);
}

sub _trim {
Expand Down Expand Up @@ -661,6 +662,7 @@ Interpret compiled template code.
=head2 parse
$mt = $mt->parse;
$mt = $mt->parse($template);
Parse template into tree.
Expand Down
10 changes: 4 additions & 6 deletions t/mojo/template.t
Expand Up @@ -24,9 +24,9 @@ use Mojo::Template;
my $capture = 'no warnings "redefine"; sub capture { shift->(@_) }';

# Consistent scalar context
my $mt = Mojo::Template->new;
my $mt = Mojo::Template->new(template => '<%= @foo %>:<%== @foo %>');
$mt->prepend('my @foo = (3, 4);');
my $output = $mt->render('<%= @foo %>:<%== @foo %>');
my $output = $mt->parse->build->compile || $mt->interpret;
is $output, "2:2\n", 'same context';

# Trim tag
Expand Down Expand Up @@ -557,10 +557,8 @@ EOF
is $output, " \n 2\n\n 3\n\n 4\n", 'block loop';

# Strict
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
% $foo = 1;
EOF
$mt = Mojo::Template->new->template('% $foo = 1;');
$output = $mt->parse->build->compile || $mt->interpret;
isa_ok $output, 'Mojo::Exception', 'right exception';
like $output->message, qr/^Global symbol "\$foo" requires/, 'right message';

Expand Down

0 comments on commit 331429d

Please sign in to comment.