Skip to content

Commit

Permalink
Load templates relatively to the script location
Browse files Browse the repository at this point in the history
  • Loading branch information
Tadeusz Sośnierz committed Jun 22, 2012
1 parent 675b0bb commit 00cf63b
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 3 deletions.
3 changes: 2 additions & 1 deletion examples/pastebin.pl
@@ -1,13 +1,14 @@
use lib 'lib';

use Bailador;
Bailador::import;

unless 'data'.IO ~~ :d {
mkdir 'data'
}

get '/' => sub {
template 'examples/views/index.tt'
template 'index.tt'
}

post '/new_paste' => sub {
Expand Down
5 changes: 5 additions & 0 deletions lib/Bailador.pm
Expand Up @@ -9,6 +9,11 @@ module Bailador;

my $app = Bailador::App.current;

our sub import {
my $file = callframe(1).file;
$app.location = $file.substr(0, $file.rindex('/'));
}

sub route_to_regex($route) {
$route.split('/').map({
my $r = $_;
Expand Down
3 changes: 2 additions & 1 deletion lib/Bailador/App.pm
Expand Up @@ -4,13 +4,14 @@ use Template::Mojo;

class Bailador::App {
has %.routes = { GET => [], 'POST' => [] };
has $.location is rw;
has Bailador::Context $.context = Bailador::Context.new;
has Template::Mojo $!template; # type object

method request { $.context.request }
method response { $.context.response }
method template(Str $tmpl, @params) {
$!template.new(slurp $tmpl).render(|@params);
$!template.new(slurp "$!location/views/$tmpl").render(|@params);
}

my $current = Bailador::App.new;
Expand Down
3 changes: 2 additions & 1 deletion t/04-templates.t
@@ -1,9 +1,10 @@
use Test;
use Bailador;
Bailador::import;
use Bailador::Test;

plan 1;

get '/' => sub { template 't/templates/simple.tt', 'bar' }
get '/' => sub { template 'simple.tt', 'bar' }

response-content-is 'GET', '/', "a happy bar\n";
File renamed without changes.

0 comments on commit 00cf63b

Please sign in to comment.