Last active
July 11, 2016 00:19
-
-
Save hayajo/943f3bef6180b55cdfe7b06059fe7998 to your computer and use it in GitHub Desktop.
Mojoliciousでcp932
This file contains 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
#!/usr/bin/env perl | |
use Mojolicious::Lite; | |
app->hook(before_dispatch => sub { | |
my $c = shift; | |
# Mojo::Message::Requestのdefault_charsetを指定することで、 | |
# 自動的に指定された文字エンコーディングでパラメータがデコードされる(デフォルトはUTF-8)。 | |
$c->req->default_charset('cp932'); | |
}); | |
app->hook(before_render => sub { | |
my $c = shift; | |
$c->res->headers->content_type('text/html; charset=shift_jis'); | |
$c->app->renderer->encoding('cp932'); | |
}); | |
any '/' => sub { | |
my $c = shift; | |
my $method = $c->req->method; | |
if ($method eq 'GET' || $method eq 'POST') { | |
my $greeting; | |
if ($method eq 'POST') { | |
my $name = $c->req->param("name"); | |
$greeting = sprintf("こんにちは、%s", $name) if ($name); | |
} | |
return $c->render(template => 'index', greeting => $greeting); | |
} | |
$c->reply->not_found; | |
} => 'index'; | |
app->start; | |
__DATA__ | |
@@ index.html.ep | |
% layout 'default'; | |
% title 'ようこそ'; | |
<h1>Mojoliciousへようこそ</h1> | |
%= form_for index => (method => 'POST') => begin | |
%= text_field 'name' | |
%= submit_button | |
% end | |
% if ($greeting) { | |
<h2><%= $greeting %></h2> | |
% } | |
@@ layouts/default.html.ep | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=Shift-JIS"> | |
<title><%= title %></title> | |
</head> | |
<body><%= content %></body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment