Permalink
Browse files

xslate => microtemplate::datasectionex

  • Loading branch information...
1 parent 3f3a83e commit c211a8d44acc12a5ca24b10fa5e92cbe44dcfe0d @kazeburo committed Jul 6, 2010
Showing with 68 additions and 97 deletions.
  1. +44 −47 lib/NoNoPaste.pm
  2. +24 −50 lib/Shirahata.pm
View
@@ -77,14 +77,13 @@ EOF
return $sth->fetchrow_hashref;
}
-
get '/' => sub {
my ( $self, $c ) = @_;
my ($entries,$next) = $self->entry_list($c->req->param('offset'));
- return $c->render('index.tx',
- entries => $entries,
- next => $next );
+ $c->render('index',
+ entries => $entries,
+ next => $next );
};
post '/add' => sub {
@@ -96,7 +95,7 @@ post '/add' => sub {
}
my ($entries,$next) = $self->entry_list;
- $c->render('index.tx',
+ $c->render('index',
entries => $entries,
next => $next);
};
@@ -106,89 +105,88 @@ get '/entry/{id:[0-9a-f]{16}}' => sub {
my $entry = $self->retrieve_entry($c->args->{id});
return $c->res->not_found() unless $entry;
- $c->render('entry.tx', entry => $entry );
+ $c->render('entry', entry => $entry );
};
1;
__DATA__
-@@ base.tx
+@@ base.mt
<html>
<head>
-<title>NoNoPaste: Yet Another NoPaste <: $stash.test :></title>
-<link rel="stylesheet" type="text/css" href="<: $req.uri_for('/static/js/prettify/prettify.css') :>" />
-<link rel="stylesheet" type="text/css" href="<: $req.uri_for('/static/css/ui-lightness/jquery-ui-1.8.2.custom.css') :>" />
-<link rel="stylesheet" type="text/css" href="<: $req.uri_for('/static/css/default.css') :>" />
+<title>NoNoPaste: Yet Another NoPaste</title>
+<link rel="stylesheet" type="text/css" href="<?= $c->req->uri_for('/static/js/prettify/prettify.css') ?>" />
+<link rel="stylesheet" type="text/css" href="<?= $c->req->uri_for('/static/css/ui-lightness/jquery-ui-1.8.2.custom.css') ?>" />
+<link rel="stylesheet" type="text/css" href="<?= $c->req->uri_for('/static/css/default.css') ?>" />
</head>
<body>
<div id="container">
<div id="header">
-<h1 class="title"><a href="<: $req.uri_for('/') :>">NoNoPaste: Yet Another NoPaste</a></h1>
+<h1 class="title"><a href="<?= $c->req->uri_for('/') ?>">NoNoPaste: Yet Another NoPaste</a></h1>
<div class="welcome">
<ul>
-<li><a href="<: $req.uri_for('/') :>">TOP</a></li>
+<li><a href="<?= $c->req->uri_for('/') ?>">TOP</a></li>
</ul>
</div>
</div>
<div id="content">
-: block content -> { }
+? block content => sub { }
</div>
</div>
-<script src="<: $req.uri_for('/static/js/jquery-1.4.2.min.js') :>" type="text/javascript"></script>
-<script src="<: $req.uri_for('/static/js/jstorage.js') :>" type="text/javascript"></script>
-<script src="<: $req.uri_for('/static/js/prettify/prettify.js') :>" type="text/javascript"></script>
-: block javascript -> {
+<script src="<?= $c->req->uri_for('/static/js/jquery-1.4.2.min.js') ?>" type="text/javascript"></script>
+<script src="<?= $c->req->uri_for('/static/js/jstorage.js') ?>" type="text/javascript"></script>
+<script src="<?= $c->req->uri_for('/static/js/prettify/prettify.js') ?>" type="text/javascript"></script>
+? block javascript => sub {
<script type="text/javascript">
$(function() {
prettyPrint();
});
</script>
-: }
+? }
</body>
</html>
-@@ index.tx
-: cascade base
+@@ index.mt
+? extends 'base'
-: override content -> {
+? block content => sub {
<h2 class="subheader">New Entry</h2>
-: block form | fillinform( $req ) -> {
+? fillinform( $c->req, sub {
<form method="post" action="/add" id="nopaste">
<textarea name="body" rows="20" cols="60"></textarea>
<label for="nick">nick</label>
<input type="text" id="nick" name="nick" value="" size="21" />
<input type="submit" id="post_nopaste" value="POST" />
</form>
-: } # block form
+? })
<h2 class="subheader">List</h2>
-: for $entries -> $entry {
+? for my $entry ( @$entries ) {
<div class="entry">
<pre class="prettyprint">
-<: $entry.body :>
+<?= $entry->{body} ?>
</pre>
-<div class="entry_meta"><a href="<: $req.uri_for('/entry/' ~ $entry.id) :>" class="date"><: $entry.ctime :></a> / <span class="nick"><: $entry.nick :></span></div>
+<div class="entry_meta"><a href="<?= $c->req->uri_for('/entry/' . $entry->{id}) ?>" class="date"><?= $entry->{ctime} ?></a> / <span class="nick"><?= $entry->{nick} ?></span></div>
</div>
-: }
-
+? }
<p class="paging">
-: my $offset = $req.param('offset') || 0
-: if $offset >= 10 {
-<a href="<: $req.uri_for('/', [ offset => $offset - 10 ] ) :>">Prev</a>
-: }
-: if $next {
-<a href="<: $req.uri_for('/', [ offset => $offset + 10 ] ) :>">Next</a>
-: }
+? my $offset = $c->req->param('offset') || 0;
+? if ( $offset >= 10 ) {
+<a href="<?= $c->req->uri_for('/', [ 'offset' => ($offset - 10) ] ) ?>">Prev</a>
+? }
+? if ( $next ) {
+<a href="<?= $c->req->uri_for('/', [ 'offset' => ($offset + 10) ] ) ?>">Next</a>
+? }
</p>
-: } # content
+? } #block content
-: override javascript -> {
+? block javascript => sub {
<script type="text/javascript">
$(function() {
prettyPrint();
@@ -201,20 +199,19 @@ $(function() {
}
});
</script>
-: } # javascript
-
+? } #block javascript
-@@ entry.tx
-: cascade base
+@@ entry.mt
+? extends 'base'
-: override content -> {
-<h2 class="subheader"><a href="<: $req.uri_for('/entry/'~$entry.id) :>"><: $req.uri_for('/entry/'~$entry.id) :></a></h2>
+? block content => sub {
+<h2 class="subheader"><a href="<?= $c->req->uri_for('/entry/' . $entry->{id}) ?>"><?= $c->req->uri_for('/entry/' . $entry->{id}) ?></a></h2>
<div class="entry">
<pre class="prettyprint">
-<: $entry.body :>
+<?= $entry->{body} ?>
</pre>
-<div class="entry_meta"><a href="<: $req.uri_for('/entry/'~$entry.id) :>" class="date"><: $entry.ctime :></a> / <span class="nick"><: $entry.nick :></span></div>
+<div class="entry_meta"><a href="<?= $c->req->uri_for('/entry/' . $entry->{id}) ?>" class="date"><?= $entry->{ctime} ?></a> / <span class="nick"><?= $entry->{nick} ?></span></div>
</div>
-: } # content
+? } # content
View
@@ -7,7 +7,6 @@ use Scalar::Util qw/blessed/;
use base qw/Class::Accessor::Fast/;
use Plack::Builder;
use Router::Simple;
-use Data::Section::Simple;
use Path::Class;
use Net::IP;
@@ -129,7 +128,7 @@ sub build_app {
}
else {
# router not match
- $psgi_res = $c->res->not_found('no route')->finalize;
+ $psgi_res = $c->res->not_found()->finalize;
}
$psgi_res;
@@ -186,10 +185,7 @@ package Shirahata::Connection;
use strict;
use warnings;
use base qw/Class::Accessor::Fast/;
-use Text::Xslate qw(mark_raw);
-use HTML::FillInForm::Lite qw(fillinform);
-use Path::Class;
-use File::Temp;
+use Text::MicroTemplate::DataSectionEx qw//;
__PACKAGE__->mk_accessors(qw/req res stash args klass/);
@@ -201,63 +197,41 @@ sub render {
my $file = shift;
my %args = ( @_ && ref $_[0] ) ? %{$_[0]} : @_;
my %vars = (
- args => $self->args,
- req => $self->req,
- res => $self->res,
+ c => $self,
stash => $self->stash,
%args,
);
- my $body = $self->tx->render($file, \%vars);
+
+ my $mt = Text::MicroTemplate::DataSectionEx->new(
+ package => $self->klass,
+ package_name => 'Shirahata::TemplateFilters',
+ template_args => \%vars
+ );
+ my $body = $mt->render($file);
$self->res->status( 200 );
$self->res->content_type('text/html; charset=UTF-8');
$self->res->body( $body );
$self->res;
}
-my $template_dir = {};
-sub build_template {
- my $self = shift;
- if ( ! $template_dir->{$self->klass} ) {
- my $reader = Data::Section::Simple->new($self->klass);
- my $all = $reader->get_data_section;
- $template_dir->{$self->klass} = File::Temp::tempdir( CLEANUP => 1 );
-warn $template_dir->{$self->klass};
- for my $section ( keys %$all ) {
- my $fh = Path::Class::file( $template_dir->{$self->klass}, $section )->openw;
- print $fh $all->{$section};
- }
- }
- $template_dir->{$self->klass};
-}
+1;
-my $tx_cache = {};
-sub tx {
- my $self = shift;
+package Shirahata::TemplateFilters;
- return $tx_cache->{$self->klass}
- if $tx_cache->{$self->klass};
-
- my $template_dir = $self->build_template;
- $tx_cache->{$self->klass} = Text::Xslate->new(
- path => [ $template_dir ],
- cache_dir => $template_dir,
- cache => 2,
- input_layer => ':raw',
- function => {
- fillinform => sub {
- my $q = shift;
- return sub {
- my $fif = HTML::FillInForm::Lite->new(layer => ':raw');
- my $output = $fif->fill(\$_[0], $q);
- mark_raw( $output )
- };
- },
- },
- );
- $tx_cache->{$self->klass};
+use strict;
+use warnings;
+use HTML::FillInForm::Lite qw//;
+
+sub fillinform {
+ my $q = shift;
+ my $code = shift;
+ my $mteref = $Shirahata::TemplateFilters::_MTEREF ||
+ $Shirahata::TemplateFilters::_MTEREF::_MTREF;
+ $code->();
+ my $fif = HTML::FillInForm::Lite->new(layer => ':raw');
+ $$mteref = $fif->fill($mteref, $q);
}
-
1;
package Shirahata::Request;

0 comments on commit c211a8d

Please sign in to comment.