Dancer2-Template-Simple
view release on metacpan or search on metacpan
lib/Dancer2/Template/Simple.pm view on Meta::CPAN
package Dancer2::Template::Simple;
# ABSTRACT: Pure Perl 5 template engine for Dancer2
$Dancer2::Template::Simple::VERSION = '2.0.0';
use Moo;
use Dancer2::FileUtils 'read_file_content';
use Ref::Util qw<is_arrayref is_coderef is_plain_hashref>;
with 'Dancer2::Core::Role::Template';
has start_tag => (
is => 'rw',
default => sub {'<%'},
);
has stop_tag => (
is => 'rw',
default => sub {'%>'},
);
sub BUILD {
my $self = shift;
my $settings = $self->config;
$settings->{$_} and $self->$_( $settings->{$_} )
for qw/ start_tag stop_tag /;
}
sub render {
my ( $self, $template, $tokens ) = @_;
my $content;
$content = read_file_content($template);
$content = $self->parse_branches( $content, $tokens );
return $content;
}
sub parse_branches {
my ( $self, $content, $tokens ) = @_;
my ( $start, $stop ) = ( $self->start_tag, $self->stop_tag );
my @buffer;
my $should_bufferize = 1;
my $bufferize_if_token = 0;
# $content =~ s/\Q${start}\E(\S)/${start} $1/sg;
# $content =~ s/(\S)\Q${stop}\E/$1 ${stop}/sg;
# we get here a list of tokens without the start/stop tags
my @full = split( /\Q$start\E\s*(.*?)\s*\Q$stop\E/, $content );
# and here a list of tokens without variables
my @flat = split( /\Q$start\E\s*.*?\s*\Q$stop\E/, $content );
# eg: for 'foo=<% var %>'
# @full = ('foo=', 'var')
# @flat = ('foo=')
my $flat_index = 0;
my $full_index = 0;
for my $word (@full) {
# flat word, nothing to do
if ( defined $flat[$flat_index]
&& ( $flat[$flat_index] eq $full[$full_index] ) )
{
( run in 0.662 second using v1.01-cache-2.11-cpan-39bf76dae61 )