Permalink
Browse files

Add templating support, needs Ratel

  • Loading branch information...
1 parent 2acf8e1 commit d8574ae69e1da23f6e1b69e54d2e8219893d4ace @tadzik tadzik committed Dec 29, 2011
Showing with 22 additions and 2 deletions.
  1. +7 −2 examples/app.pl
  2. +8 −0 examples/views/tmpl.tt
  3. +7 −0 lib/Bailador.pm
View
@@ -3,11 +3,11 @@
# simple cases
get '/' => sub {
"hello world"
-};
+}
get '/about' => sub {
"about me"
-};
+}
# regexes, as usual
get /foo(.+)/ => sub ($x) {
@@ -23,4 +23,9 @@
"junctions are cool"
}
+# templates!
+get / ^ '/template/' (.+) $ / => sub ($x) {
+ template 'tmpl.tt', { name => $x }
+}
+
baile;
View
@@ -0,0 +1,8 @@
+<html>
+ <head>
+ <title>A greeting for [%! name %]</title>
+ </head>
+ <body>
+ <h1>Hello [%! name %]!</h1>
+ </body>
+</html>
View
@@ -1,6 +1,7 @@
module Bailador;
use Bailador::Request;
use Bailador::Response;
+use Ratel;
use HTTP::Easy::PSGI;
my %routes;
@@ -9,6 +10,7 @@ my %routes;
my $current-request = Bailador::Request.new;
my $current-response = Bailador::Response.new;
+my $template-engine = Ratel.new;
sub get(Pair $x) is export {
%routes<GET>.push: $x;
@@ -30,6 +32,11 @@ sub status(Int $code) is export {
$current-response.code = $code;
}
+sub template(Str $tmpl, %params) is export {
+ $template-engine.load("views/$tmpl");
+ return $template-engine.render(|%params);
+}
+
sub dispatch($env) {
my $res = '';

0 comments on commit d8574ae

Please sign in to comment.