perl6 – 是否有可能将TT2与Cro一起使用?

前端之家收集整理的这篇文章主要介绍了perl6 – 是否有可能将TT2与Cro一起使用?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在考虑使用perl6和Cro来构建一个包含文本内容的网站.是否有关于使用模板工具包(如TT2)和代码示例使用Cro的最佳实践/指导?

解决方法

你看过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"
    };
  }

猜你在找的Perl相关文章