Permalink
Browse files

add View::MT.

  • Loading branch information...
1 parent 6b3f280 commit 63bb65894f953075eaf216b6574fdb70e6c187df @nekokak committed Dec 8, 2009
Showing with 52 additions and 0 deletions.
  1. +52 −0 lib/Kamui/View/MT.pm
View
52 lib/Kamui/View/MT.pm
@@ -0,0 +1,52 @@
+package Kamui::View::MT;
+use Kamui;
+use base 'Kamui::View';
+use Text::MicroTemplate::Extended;
+use File::Spec;
+use Encode;
+use URI::Escape;
+use HTML::Entities;
+
+sub render {
+ my ($class, $context) = @_;
+
+ my $c = sub { $context };
+ my $stash = sub { $context->stash };
+ my $template = $context->load_template || $context->guess_filename;
+
+ # TODO: set macro
+ my $mt = Text::MicroTemplate::Extended->new(
+ package_name => $context->base_name . '::_MT',
+ include_path => [ '.', File::Spec->catfile($context->conf->{view}->{mt}->{path}) ],
+ extension => '',
+ use_cache => 1,
+ open_layer => ':utf8',
+ macro => {
+ raw_string => sub($) { Text::MicroTemplate::EncodedString->new($_[0]) },
+ uri => sub($) {
+ Encode::is_utf8( $_[0] )
+ ? URI::Escape::uri_escape_utf8($_[0])
+ : URI::Escape::uri_escape($_[0]);
+ },
+ html_unescape => sub($) {
+ HTML::Entities::decode_entities($_[0]);
+ },
+ },
+ template_args => {
+ stash => $stash,
+ s => $stash,
+ c => $c,
+ },
+ %{ $context->conf->{view}->{mt}->{options} || {} },
+ );
+ my $output = $mt->render($template);
+
+ my $res = $context->res;
+ $res->status('200');
+ $res->body($output);
+ $res->headers([ 'Content-Type' => 'text/html' ]);
+ $res;
+}
+
+1;
+

0 comments on commit 63bb658

Please sign in to comment.