可以将TT2与Cro一起使用吗?

p6s*_*eve 4 template-toolkit perl6 cro

我正在考虑使用perl6和Cro来构建一个包含文本内容的网站.是否有关于使用模板工具包(如TT2)和代码示例使用Cro的最佳实践/指导?

Arn*_*mer 5

你看过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; }
Run Code Online (Sandbox Code Playgroud)

模板文件如下所示:

% 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>
Run Code Online (Sandbox Code Playgroud)

服务器代码可以做一些改造(灵感来自Bailador).添加此代码:

sub template ($template, %values)
{
  my $tmpl = slurp "views/$template";
  my $t = Template::Mojo.new($tmpl);
  return content 'text/html', $t.render(%values);
}
Run Code Online (Sandbox Code Playgroud)

并改变"得到":

  get -> ''
  {
    template 'template.tt',
    {
      title => "AAA",
      type  => "aaa",
      mode  => "AAAaaaAAA"
    };
  }
Run Code Online (Sandbox Code Playgroud)