解决方法
你看过Cro :: WebApp吗?
见https://github.com/jnthn/cro-webapp
–
也可以使用“Template :: Mojo”.
这是一个Cro服务器:
use Cro::HTTP::Router; use Cro::HTTP::Server; use Template::Mojo; my $tmpl = slurp 'views/template.tt'; my $t = Template::Mojo.new($tmpl); my $application = route { get -> '' { content 'text/html',$t.render({ title => "AAA",type => "aaa",mode => "AAAaaaAAA" }); } } my Cro::Service $hello = Cro::HTTP::Server.new: :host<localhost>,:port<10000>,:$application; $hello.start; react whenever signal(SIGINT) { $hello.stop; exit; }
模板文件如下所示:
% my %h = @_; % my $title = %h<title>; % my $type = %h<type>; % my $mode = %h<mode>; % <html> <head> <title><%= $title %></title> </head> <body> <h1><%= $type %></h1> <p><%= $mode %></p> <body> </html>
服务器代码可以做一些改造(灵感来自Bailador).添加此代码:
sub template ($template,%values) { my $tmpl = slurp "views/$template"; my $t = Template::Mojo.new($tmpl); return content 'text/html',$t.render(%values); }
并改变“得到”:
get -> '' { template 'template.tt',{ title => "AAA",type => "aaa",mode => "AAAaaaAAA" }; }