Skip to content

Instantly share code, notes, and snippets.

@sshaw
Created March 29, 2012 07:25
Show Gist options
  • Save sshaw/2234505 to your computer and use it in GitHub Desktop.
Save sshaw/2234505 to your computer and use it in GitHub Desktop.
Mojolicious Static Includes
sub register
{
my ($self, $app) = @_;
$self->helper(include_static => sub {
my ($c, $path) = @_;
return unless $path;
if($path !~ m|^/|) {
my $root = first { -f "$_/$path" } @{$c->app->renderer->paths};
$path = "$root/$path" if $root;
}
open my $in, '<', $path or die "can't include file '$path': $!";
my $content = do { local $/; <$in> };
return $content;
});
}
sub register
{
my ($self, $app) = @_;
$self->helper(include_static => sub {
my ($c, $path) = @_;
return unless $path;
if($path !~ m|^/|) {
my $root = first { -f "$_/$path" } @{$c->app->renderer->paths};
$path = "$root/$path" if $root;
}
open my $in, '<', $path or die "can't include file '$path': $!";
my $content = do { local $/; <$in> };
return $content;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment