有没有办法使用Mojolicious渲染引擎在Web请求之外呈现模板?
答案 0 :(得分:6)
是
use Mojolicious::Renderer;
my $renderer = Mojolicious::Renderer->new;
push @{renderer->paths}, '/path/to/your/templates';
my $template = $renderer->get_data_template({
template => 'foo/bar',
format => 'html',
handler => 'epl'
});
答案 1 :(得分:0)
这是一个更全面(并且更新)的解决方案,可以充分利用完整Mojolicious堆栈中提供的所有渲染插件。
use Mojolicious;
unless (@ARGV) {
die "$0: <template base name> [key value pairs]\n";
}
my $app = Mojolicious->new(secrets => ['ignored']);
my $c = $app->build_controller;
my $r = $app->renderer;
push @{$r->paths}, './templates'; # directory containing templates
$c->app->log->level('fatal');
my $template = shift; # template base name e.g. 'index' which looks up ./templates/index.html.ep
$c->stash(shift, shift) while @ARGV >= 2; # add extra parameters into cache
my $out = $c->render_to_string($template);
print $out if $out;
exit 0;