diff options
author | franck cuny <franck@lumberjaph.net> | 2010-07-30 13:40:06 +0200 |
---|---|---|
committer | franck cuny <franck@lumberjaph.net> | 2010-07-30 13:40:06 +0200 |
commit | 1745c19001c4a4595624fbe97adcfc65820ae5fe (patch) | |
tree | 6cbd69d644eaf0499516f501073f416473c84aa4 | |
download | dancer-template-xslate-1745c19001c4a4595624fbe97adcfc65820ae5fe.tar.gz |
initial commit
-rw-r--r-- | lib/Dancer/Template/Xslate.pm | 66 | ||||
-rw-r--r-- | t/00-load.t | 8 | ||||
-rw-r--r-- | t/01-main.t | 27 | ||||
-rw-r--r-- | t/index.xslate | 5 |
4 files changed, 106 insertions, 0 deletions
diff --git a/lib/Dancer/Template/Xslate.pm b/lib/Dancer/Template/Xslate.pm new file mode 100644 index 0000000..9f1e2c7 --- /dev/null +++ b/lib/Dancer/Template/Xslate.pm @@ -0,0 +1,66 @@ +package Dancer::Template::Xslate; + +# ABSTRACT: Text::Xslate wrapper for Dancer + +use strict; +use warnings; + +use Text::Xslate; + +use base 'Dancer::Template::Abstract'; + +my $_engine; + +sub init { + my $self = shift; + + my %args = ( + %{$self->config}, + ); + + $_engine = Text::Xslate->new(%args); +} + +sub render { + my ($self, $template, $tokens) = @_; + + my $content = eval { + $_engine->render($template, $tokens) + }; + + if (my $err = $@) { + my $error = qq/Couldn't render template "$err"/; + die $error; + } + + return $content; +} + +1; + +=head1 DESCRIPTION + +This class is an interface between Dancer's template engine abstraction layer +and the L<Text::Xslate> module. + +In order to use this engine, use the template setting: + + template: xslate + +This can be done in your config.yml file or directly in your app code with the +B<set> keyword. + +You can configure L<Text::Xslate> : + + template: xslate + engines: + xslate: + cache_dir => "/www/.../xslate_cache", + cache => 1, + module => + - Text::Xslate::Bridge::TT2 # to keep partial compatibility + + +=head1 SEE ALSO + +L<Dancer>, L<Text::Xslate>, L<http://xslate.org/> diff --git a/t/00-load.t b/t/00-load.t new file mode 100644 index 0000000..da6530d --- /dev/null +++ b/t/00-load.t @@ -0,0 +1,8 @@ +use strict; +use warnings; +use Test::More tests => 1; + +BEGIN { + use_ok( 'Dancer::Template::Xslate' ) || print "Bail out! +"; +} diff --git a/t/01-main.t b/t/01-main.t new file mode 100644 index 0000000..97682ed --- /dev/null +++ b/t/01-main.t @@ -0,0 +1,27 @@ +use strict; +use warnings; +use Test::More tests => 2; +use Dancer::FileUtils 'path'; + +use Dancer::Template::Xslate; + +my $engine; +eval { $engine = Dancer::Template::Xslate->new }; +is $@, '', + "Dancer::Template::Xslate engine created"; + +my $template = path('t', 'index.xslate'); + +my $result = $engine->render( + $template, + { var1 => 1, + var2 => 2, + foo => 'one', + bar => 'two', + baz => 'three' + } +); + +my $expected = + 'this is var1="1" and var2=2' . "\n\nanother line\n\none two three\n"; +is $result, $expected, "processed a template given as a file name"; diff --git a/t/index.xslate b/t/index.xslate new file mode 100644 index 0000000..7ec6233 --- /dev/null +++ b/t/index.xslate @@ -0,0 +1,5 @@ +this is var1="<: $var1 :>" and var2=<: $var2 :> + +another line + +<: $foo :> <: $bar :> <: $baz :> |