2

有没有办法使用 Mojolicious 渲染引擎在 Web 请求之外渲染模板?

4

2 回答 2

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'
});
于 2012-10-08T13:55:36.410 回答
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;
于 2018-01-15T13:48:56.280 回答