Created
May 22, 2014 06:16
-
-
Save memememomo/614ada661497d4f2adae to your computer and use it in GitHub Desktop.
MojoliciousでTemplate::Toolkitを使う ref: http://qiita.com/uchiko/items/b504ded3d756803f4c75
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <html> | |
| <head><title>テスト</title></head> | |
| <body> | |
| <div> | |
| [% content.raw %] | |
| </div> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ cpanm Mojolicious::Plugin::TtRenderer |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ wget http://cpan.metacpan.org/authors/id/M/MA/MALA/Template-Stash-AutoEscape-0.03.tar.gz | |
| $ tar xvzf Template-Stash-AutoEscape-0.03.tar.gz | |
| $ cd Template-Stash-AutoEscape-0.03/ | |
| $ perl -i -ple 's{Template::Toolkit}{Template}' Makefile.PL | |
| $ perl Makefile.PL | |
| $ make | |
| $ make test | |
| $ make install |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use Mojolicious::Lite; | |
| use Template::Stash::AutoEscape; | |
| plugin 'TtRenderer', {template_options => {STASH => Template::Stash::AutoEscape->new}}; | |
| my @users = ( | |
| { | |
| id => 1, | |
| name => '太郎', | |
| age => 30, | |
| note => '<b>太郎</b>', | |
| }, | |
| { | |
| id => 2, | |
| name => '花子', | |
| age => 31, | |
| note => '<b>花子</b>', | |
| }, | |
| ); | |
| get '/' => sub { | |
| my $self = shift; | |
| $self->stash->{users} = \@users; | |
| $self->render(); | |
| } => 'index'; | |
| get '/detail/:id' => sub { | |
| my $self = shift; | |
| my $id = $self->param('id'); | |
| my ($user) = grep { $_->{id} == $id } @users; | |
| if (!$user) { | |
| return $self->render_not_found(); | |
| } | |
| $self->stash->{user} = $user; | |
| $self->render(); | |
| } => 'detail'; | |
| app->start; | |
| __DATA__ | |
| @@ layouts/default.html.tt | |
| <html> | |
| <head> | |
| <meta charset="utf8"> | |
| <title>[% title %]</title> | |
| </head> | |
| <body> | |
| <h1>[% title %]</h1> | |
| <div> | |
| [% content.raw %] | |
| </div> | |
| </body> | |
| </html> | |
| @@ index.html.tt | |
| [% WRAPPER "layouts/default.html.tt", title="一覧" %] | |
| <table> | |
| <tr> | |
| <th>ID</th> | |
| <th>名前</th> | |
| <th>年齢</th> | |
| <th>備考</th> | |
| </tr> | |
| [% FOREACH u=users %] | |
| <tr> | |
| <td>[% u.id %]</td> | |
| <td><a href="[% c.url_for('detail', id => u.id) %]">[% u.name %]</a></td> | |
| <td>[% u.age %]</td> | |
| <td>[% u.note %]</td> | |
| </tr> | |
| [% END %] | |
| </table> | |
| [% END %] | |
| @@ detail.html.tt | |
| [% WRAPPER "layouts/default.html.tt", title="詳細" %] | |
| <a href="[% c.url_for('index') %]">戻る</a> | |
| <table> | |
| <tr> | |
| <th>ID</th> | |
| <td>[% user.id %]</td> | |
| </tr> | |
| <tr> | |
| <th>名前</th> | |
| <td>[% user.name %]</td> | |
| </tr> | |
| <tr> | |
| <th>年齢</th> | |
| <td>[% user.age %]</td> | |
| </tr> | |
| <tr> | |
| <th>備考</th> | |
| <td>[% user.note %]</td> | |
| </tr> | |
| </table> | |
| [% END %] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment