2

尝试使用HTMLTemplateProRenderer插件, Mojolicious::Lite 以便我可以使用HTML::Template.

问题是每个示例,甚至是文档,都只显示附加到脚本的模板文件。我需要模板文件位于与 Perl 代码不同的目录中。

这是我正在尝试做的一个示例。

这可以使用__DATA__,但是如何通过使用外部模板文件来工作:

#!/usr/bin/env perl

use Mojolicious::Lite;

plugin 'HTMLTemplateProRenderer';

# Route leading to an action that renders a template
get '/test' => sub {
    my $c = shift;

    $c->stash( one => 'This is result one' );

    $c->render(
        template => 'display/index',
        two      => 'this is the second',
        handler  => 'tmpl'
    );
};

app->start;

模板文件是display/index.tmpl

 <html>
  <head><title>Test Template</title>
   <body>
     <p>Value ONE =  <TMPL_VAR NAME="one"> </p>
     <p>Value TWO =  <TMPL_VAR NAME="two"> </p>
   </body>
 </html>
4

1 回答 1

1

首先,模板路径必须是格式<name>.<format>.<handler>。所以对于display/indexdisplay/index.html.tmpl

其次,模板搜索路径HTMLTemplateProRenderertemplatestemplates/<controller>相对于应用程序主页。如果使用插件配置选项,则应用主页tmpl_opts => {use_home_template => 1}。或任何添加到app->renderer->paths.

于 2017-02-16T22:38:20.620 回答