Skip to content

Commit

Permalink
allow configuring template directive characters
Browse files Browse the repository at this point in the history
When we're writing blog posts about Perl, we run afoul of the template
parser, a lot. The `%` that indicates a line of template is the same as
the hash sigil, which often appears on the start of a line.

Now, users can disable line-based processing entirely if they want, and
change the template tags to look however they want (maybe to match
Template Toolkit, so `[% ... %]`).

Refs preaction#541
  • Loading branch information
preaction committed Jun 21, 2018
1 parent 6bc64ee commit 6ccc43f
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/Statocles/Template.pm
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,21 @@ has _template => (
lazy => 1,
default => sub {
my ( $self ) = @_;
my %config;
if ( $self->theme ) {
%config = map { $_ => $self->theme->$_ }
grep { $self->theme->$_ }
qw(
tag_start tag_end
line_start trim_mark
replace_mark expression_mark
escape_mark comment_mark
capture_start capture_end
);
}
my $t = Mojo::Template->new(
name => $self->path,
%config,
);
$t->parse( $self->content );
return $t;
Expand Down
53 changes: 53 additions & 0 deletions lib/Statocles/Theme.pm
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,59 @@ has include_stores => (
},
);

=attr tag_start
String that indicates the start of a template tag. Defaults to
C<< <% >>.
=attr tag_end
String that indicates the end of a template tag. Defaults to C<< %> >>.
=attr line_start
String that indicates the start of a line of template code.
Defaults to C<%>.
=attr expression_mark
String that indicates an expression to be evaluated and inserted into
the template. Defaults to C<=>.
=attr escape_mark
String that escapes the template directives. Defaults to C<%>.
=attr comment_mark
String that indicates a comment. Defaults to C<#>.
=attr capture_start
Keyword that starts capturing string. Defaults to C<begin>.
=attr capture_end
Keyword that ends capturing string. Defaults to C<end>.
=attr trim_mark
String that indicates that whitespace should be trimmed. Defaults to
C<=>.
=cut

has [qw(
tag_start tag_end
line_start trim_mark
replace_mark expression_mark
escape_mark comment_mark
capture_start capture_end
)] => (
is => 'ro',
isa => Maybe[Str],
);

=attr _templates
The cached template objects for this theme.
Expand Down
20 changes: 20 additions & 0 deletions t/theme/template.t
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,26 @@ ENDHTML
};
};

subtest 'template directive config' => sub {
my $theme = Statocles::Theme->new(
store => '::default',
tag_start => '[%',
tag_end => '%]',
line_start => "\036", # Something nobody will use
);
my $content = <<ENDTMPL;
[%= "hello" %]
% not you
ENDTMPL
my $expect = <<'ENDHTML';
hello
% not you
ENDHTML
my $tmpl = $theme->build_template( 'derp', $content );
my $got = $tmpl->render;
eq_or_diff $got, $expect;
};

subtest 'error messages' => sub {

subtest 'template not found' => sub {
Expand Down

0 comments on commit 6ccc43f

Please sign in to comment.