3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

MojoliciousでTemplate::Toolkitを使う

Last updated at Posted at 2014-05-22

概要

事情があってMojoliciousでTTを使う場合の設定方法。

Mojolicious::Plugin::TtRenderer

MojoliciousでTTを使うためのプラグイン。
cpanmで普通にインストール。

$ cpanm Mojolicious::Plugin::TtRenderer

Template::Stash::AutoEscape

自動エスケープ機能を追加するTTのプラグイン。
Makefile.PLに依存モジュールとして"Template::Toolkit"と記載されているため、
インストールに失敗する。
以下の手順で、Makefile.PLを編集してインストールする。

$ 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

プラグイン有効化

以下のように記述して、プラグインを有効にする。

myapp.pl
use Mojolicious::Lite;

plugin 'TtRenderer', {template_options => {STASH => Template::Stash::AutoEscape->new}};

Template::Toolkitの利用

テンプレートファイルの拡張子は「html.tt」となる。

url_forなどの関数を読み出すときは「c.url_for」のように記述する。
ヘルパーは「h」経由で呼び出す。

layout機能を使う場合、contentをそのまま記述すると、
contentで読み込まれるHTMLがエスケープされてしまうので、
rawオプションを使用する必要がある。

templates/layouts/default.html.tt
<html>
<head><title>テスト</title></head>
<body>
<div>
[% content.raw %]
</div>
</body>
</html>

以下、Template::Toolkitを使った時のサンプルアプリ。

myapp.pl
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 %]
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?