【问题标题】:How to render a template outside of a web request in Mojolicious?如何在 Mojolicious 的 Web 请求之外呈现模板?
【发布时间】:2012-10-08 13:08:46
【问题描述】:

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

【问题讨论】:

  • 您应该接受Tudor的解决方案,因为他付出了一些努力并且对您有所帮助
  • 感谢您的提示,我很乐意。一个人如何“接受解决方案”?我没有看到任何按钮或类似的东西让我这样做。
  • 啊我才意识到我必须点击复选标记。抱歉非常不直观。

标签: perl templates mojolicious


【解决方案1】:

是的

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'
});

【讨论】:

  • 谢谢,我知道它一定很简单,但是渲染文档没有显示语法。
【解决方案2】:

这是一个更全面(也更最新)的解决方案,它允许充分利用完整 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;

【讨论】:

    猜你喜欢
    • 2020-10-15
    • 1970-01-01
    • 2020-01-18
    • 2022-01-26
    • 2010-10-25
    • 1970-01-01
    • 2013-09-07
    • 2012-11-02
    • 1970-01-01
    相关资源
    最近更新 更多