Permalink
Browse files

add View::Xslate

  • Loading branch information...
fujiwara committed Aug 23, 2011
1 parent 96b6e75 commit 079af7f1f5ff62bbce06cd8b7339acc192ab3d44
Showing with 154 additions and 0 deletions.
  1. +6 −0 MANIFEST
  2. +74 −0 lib/Ark/View/Xslate.pm
  3. +70 −0 t/view_xslate.t
  4. +1 −0 t/view_xslate/forward.tx
  5. +1 −0 t/view_xslate/include.tx
  6. +1 −0 t/view_xslate/include_target.tx
  7. +1 −0 t/view_xslate/render.tx
View
@@ -65,6 +65,7 @@ lib/Ark/View.pm
lib/Ark/View/JSON.pm
lib/Ark/View/MT.pm
lib/Ark/View/TT.pm
+lib/Ark/View/Xslate.pm
LICENSE
Makefile.PL
MANIFEST This list of files
@@ -142,5 +143,10 @@ t/view_tt/forward.tt
t/view_tt/include.tt
t/view_tt/include_target.tt
t/view_tt/render.tt
+t/view_xslate.t
+t/view_xslate/forward.tx
+t/view_xslate/include.tx
+t/view_xslate/include_target.tx
+t/view_xslate/render.tx
tools/force-moose.pl
tools/test_format_junit.pl
View
@@ -0,0 +1,74 @@
+package Ark::View::Xslate;
+use Ark 'View';
+
+has path => (
+ is => 'rw',
+ isa => 'ArrayRef',
+ lazy => 1,
+ default => sub {
+ my $self = shift;
+ [$self->path_to('root')];
+ },
+);
+
+has options => (
+ is => 'rw',
+ isa => 'HashRef',
+ lazy => 1,
+ default => sub { {} },
+);
+
+has extension => (
+ is => 'rw',
+ isa => 'Str',
+ default => '.tx',
+);
+
+has xslate => (
+ is => 'rw',
+ isa => 'Text::Xslate',
+ lazy => 1,
+ default => sub {
+ my $self = shift;
+ my $c = sub { $self->context };
+ my $stash = sub { $self->context->stash };
+
+ $self->ensure_class_loaded('Text::Xslate');
+ Text::Xslate->new(
+ path => $self->path,
+ %{ $self->options }
+ );
+ },
+);
+
+sub template {
+ my ($self, $template) = @_;
+ $self->context->stash->{__view_xslate_template} = $template;
+ $self;
+}
+
+sub render {
+ my $self = shift;
+ my $template = shift;
+ my $context = $self->context;
+
+ $template ||= $self->context->stash->{__view_xslate_template}
+ || $self->context->request->action->reverse
+ or return;
+
+ $self->xslate->render(
+ $template . $self->extension,
+ {
+ %{ $context->stash },
+ c => $self->context,
+ @_,
+ },
+ );
+}
+
+sub process {
+ my ($self, $c) = @_;
+ $c->response->body( $self->render );
+}
+
+__PACKAGE__->meta->make_immutable;
View
@@ -0,0 +1,70 @@
+use Test::Base;
+use FindBin;
+
+eval { require Text::Xslate; };
+plan skip_all => 'this test required Text::Xslate' if $@;
+
+{
+ package TestApp;
+ use Ark;
+
+ package TestApp::Controller::Root;
+ use Ark 'Controller';
+
+ has '+namespace' => default => '';
+
+ sub forward :Local {
+ my ($self, $c) = @_;
+ $c->forward( $c->view('Xslate') );
+ }
+
+ sub render :Local {
+ my ($self, $c) = @_;
+ my $body = $c->view('Xslate')->render('render');
+ $c->res->body($body);
+ }
+
+ sub template :Local {
+ my ($self, $c) = @_;
+ $c->view('Xslate')->template('forward');
+ $c->forward('forward');
+ }
+
+ sub include :Local {
+ my ($self, $c) = @_;
+ $c->forward( $c->view('Xslate') );
+ }
+
+ package TestApp::View::Xslate;
+ use Ark 'View::Xslate';
+
+ has '+path' => (
+ default => sub { ["$FindBin::Bin/view_xslate"] },
+ );
+}
+
+plan 'no_plan';
+
+use Ark::Test 'TestApp',
+ components => [qw/Controller::Root View::Xslate/];
+
+{
+ my $content = get('/forward');
+ is($content, 'index xslate', 'forward view ok');
+}
+
+{
+ my $content = get('/render');
+ is($content, 'render xslate', 'render view ok');
+}
+
+{
+ my $content = get('/template');
+ is($content, 'index xslate', 'set template view ok');
+}
+
+{
+ my $content = get('/include');
+ is($content, 'before included after', 'include ok');
+}
+
View
@@ -0,0 +1 @@
+index xslate
View
@@ -0,0 +1 @@
+before <: include "include_target.tx" :> after
@@ -0,0 +1 @@
+included
View
@@ -0,0 +1 @@
+render xslate

0 comments on commit 079af7f

Please sign in to comment.