perl - Dancer 插件加载模板

标签 perl plugins dancer

如何在不更改 View 默认目录的情况下从不在“app/views”目录中的 Dancer::Plugin 加载模板?

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

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

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

register_plugin;

1;

最佳答案

你可以调用 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);

关于perl - Dancer 插件加载模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7344425/

相关文章:

json - Perl JSON 将所有数字视为字符串

perl - Perl 测试脚本能否检测它是否正在从一个 harness 中并行运行?

node.js - Webpack:找不到模块:错误:无法解析模块 browser.js

python - 部署 Tensorboard 插件

perl - 无法使用 Dancer::Plugin::Database 连接到多个数据库

正则表达式提取查询字符串

perl - 如何在 Perl 中查找用户的主目录?

php - WORDPRESS UPDATE DB 方法未被调用

perl - 如何使用 Test::WWW::Mechanize::PSGI 测试 Dancer 应用程序?

Perl:带有 DATE_FORMAT 的 DBI SQL 错误