Skip to content

Commit

Permalink
add "-raw" flag to prevent render of includes
Browse files Browse the repository at this point in the history
When we're including a file that contains content that _looks_ like
a Mojolicious template directive, we get strange errors that are
difficult to track down. We should fix this by requiring people to use
certain extensions to enable rendering (like `.ep` and the document
extensions as known by the store (`.markdown` and `.md` by default)),
but we should also allow people to override these heuristics.

So, for now, we'll add the flag that allows overriding the behavior, and
then later we can fix our heuristics for determining if we should run
the content through the renderer.

Refs #529
  • Loading branch information
preaction committed Jan 31, 2017
1 parent e3eb705 commit a07749b
Showing 3 changed files with 42 additions and 4 deletions.
13 changes: 12 additions & 1 deletion CHANGES
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
Changelog for Statocles

{{ $NEXT }}
0.083 2017-01-31 16:44:17-06:00 America/Chicago

[Added]

- The `include` helper now accepts a `-raw` flag to disable the
template renderer. This allows you to easily include Perl code and
not have it mistaken for a template. Thanks justinQuiring on IRC
for the bug report [Github #529]

This isn't the end of this: We will likely fix the heuristics to
ignore file types (extensions) that do not look like templates.
This will be a breaking change, sorry.

0.082 2017-01-18 23:17:36-06:00 America/Chicago

20 changes: 18 additions & 2 deletions lib/Statocles/Template.pm
Original file line number Diff line number Diff line change
@@ -144,6 +144,9 @@ sub render {

# Add default helpers
local *{"@{[$t->namespace]}::include"} = sub {
if ( $_[0] eq '-raw' ) {
return $self->include( @_ );
}
my ( $name, %extra_args ) = @_;
my $inner_tmpl = $self->include( $name );
return $inner_tmpl->render( %args, %extra_args ) || '';
@@ -205,16 +208,29 @@ If nothing is found, looks in the L<theme includes|Statocles::Theme/include>.

sub include {
my ( $self, @path ) = @_;
my $render = 1;
if ( $path[0] eq '-raw' ) {
# Allow raw files to not be passed through the template renderer
# This override flag will always exist, but in the future we may
# add better detection to possible file types to process
$render = 0;
shift @path;
}
my $path = Path::Tiny->new( @path );

my @stores = @{ $self->include_stores };
for my $store ( @{ $self->include_stores } ) {
if ( $store->has_file( $path ) ) {
return $self->theme->build_template( $path, $store->read_file( $path ) );
if ( $render ) {
return $self->theme->build_template( $path, $store->read_file( $path ) );
}
return $store->read_file( $path );
}
}

my $include = eval { $self->theme->include( @path ) };
my $include = eval {
$self->theme->include( !$render ? ( '-raw', @path ) : @path );
};
if ( $@ && $@ =~ /^Can not find include/ ) {
die qq{Can not find include "$path" in include directories: }
. join( ", ", map { sprintf q{"%s"}, $_->path } @stores, @{ $self->theme->include_stores }, $self->theme->store )
13 changes: 12 additions & 1 deletion lib/Statocles/Theme.pm
Original file line number Diff line number Diff line change
@@ -171,12 +171,23 @@ L<include_stores|/include_stores> before looking in the L<main store|/store>.

sub include {
my ( $self, @path ) = @_;
my $render = 1;
if ( $path[0] eq '-raw' ) {
# Allow raw files to not be passed through the template renderer
# This override flag will always exist, but in the future we may
# add better detection to possible file types to process
$render = 0;
shift @path;
}
my $path = Path::Tiny->new( @path );

my @stores = ( @{ $self->include_stores }, $self->store );
for my $store ( @stores ) {
if ( $store->has_file( $path ) ) {
return $self->_includes->{ $path } ||= $self->build_template( $path, $store->read_file( $path ) );
if ( $render ) {
return $self->_includes->{ $path } ||= $self->build_template( $path, $store->read_file( $path ) );
}
return $store->read_file( $path );
}
}

0 comments on commit a07749b

Please sign in to comment.