我要渲染 .html.ep
在发送电子邮件并从 cron 运行的独立脚本中使用 Mojolicious 渲染引擎的模板:
#!/usr/bin/perl
use feature ':5.10';
use Mojo::Base -strict;
use Mojolicious::Renderer;
use Data::Dumper;
my $renderer = Mojolicious::Renderer->new();
push @{$renderer->paths}, '/app/templates';
my $template = $renderer->get_data_template({
template => 'template_name',
format => 'html',
handler => 'ep'
});
print Dumper($template) . "\n";
但是,$template
总是未定义的。模板文件为
/app/templates/template_name.html.ep
.我究竟做错了什么?
最佳答案
您正在使用 get_data_template
来自 Mojo::Renderer,用于从 __DATA__
加载模板当前源代码文件的部分。
事实上,Mojo::Renderer 是用错了 .你想要Mojo::Template ,作为一个模块的独立模板引擎。
use Mojo::Template;
my $mt = Mojo::Template->new( vars => 1 );
my $email_body = $mt->render_file( 'test.html.ep', { one => 1, two => 2 } );
say $email_body;
使用 test.html.ep:
The magic numbers are <%= $one %> and <%= $two %>.
输出:
The magic numbers are 1 and 2.
option
vars
很重要,因此它接受命名变量而不是参数列表。
关于perl - 如何在独立的 Perl 脚本中使用 Mojolicious 渲染?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42120196/