Dancer 插件加载模板

发布于 2024-12-03 15:39:01 字数 298 浏览 1 评论 0原文

如何从不在“app/views”目录中的 Dancer::Plugin 加载模板而不更改视图默认目录?

这不起作用/它将默认视图路径添加到文件路径/:

package Dancer::Plugin::MyPlugin;
use Dancer ':syntax';
use Dancer::Plugin;

any '/test' => sub {
    template '/path_to_template/test.tt' => {
    };
};

register_plugin;

1;

How can I load a template from a Dancer::Plugin which is not in 'app/views' directory without changing views default directory?

This isn't working /it adds the default views path to the file path/:

package Dancer::Plugin::MyPlugin;
use Dancer ':syntax';
use Dancer::Plugin;

any '/test' => sub {
    template '/path_to_template/test.tt' => {
    };
};

register_plugin;

1;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北城半夏 2024-12-10 15:39:01

您可以调用engine来获取Dancer::Template对象并调用其render方法,例如:

my $template_engine = engine 'template';
my $content = $template_engine->render('/path/to/template.tt', { 'name' => 'value' });

然后,将渲染的内容返回到默认布局,调用apply_layout

return $template_engine->apply_layout($content);

You could call engine to get the Dancer::Template object and call its render method, e.g.:

my $template_engine = engine 'template';
my $content = $template_engine->render('/path/to/template.tt', { 'name' => 'value' });

Then, to return the rendered content in the default layout, call apply_layout:

return $template_engine->apply_layout($content);
ㄖ落Θ余辉 2024-12-10 15:39:01

目前,我认为您需要在模板调用之前设置 views 设置,然后再将其更改回来,例如:

my $views_dir = setting('views');       # remember current setting
setting 'views' => '/some/other/path';  # temporarily use our desired path
my $content = template 'test', $params; # render the view
setting 'views' => $views_dir;          # restore previous setting
return $content;

但是,这很丑陋。

我认为 template 关键字接受 system_path 选项是有意义的,就像 send_file 那样,所以你可以说,例如:

template '/path/to/view.tt', $params, { system_path => 1 };

我已为此提出了一个问题,并将考虑在下一个版本中实现它:https://github.com/sukria/Dancer/issues/645

(披露:我是 Dancer 开发团队的一员)

Currently, I think you'd need to set the views setting before the template call, then change it back afterwards, for instance:

my $views_dir = setting('views');       # remember current setting
setting 'views' => '/some/other/path';  # temporarily use our desired path
my $content = template 'test', $params; # render the view
setting 'views' => $views_dir;          # restore previous setting
return $content;

That, however, is ugly.

I think it would make sense for the template keyword to accept a system_path option, much like send_file does, so you could say, e.g.:

template '/path/to/view.tt', $params, { system_path => 1 };

I've raised an issue for this, and will look in to getting it implemented for the next release: https://github.com/sukria/Dancer/issues/645

(Disclosure: I'm part of the Dancer dev team)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文