未找到 Mojolicious 控制器中的模板
我正在尝试提供控制器类的 __DATA__ 部分中包含的模板,但它似乎不起作用。
在我的主 app.pl
文件中
#!/usr/bin/env perl
use Mojolicious::Lite -signatures;
use FindBin qw($Bin);
use lib "$Bin/lib";
push @{app->renderer->classes}, 'Zairyo::Controller::Data';
push @{app->preload_namespaces}, 'Zairyo::Controller::Data';
get '/:uid' => [uid => qr/[a-z0-9]{32,32}/i ] => { controller => 'Data', action => 'serve_iframe' };
app->start;
,在 Zairyo::Controller::Data
: 中,
package Zairyo::Controller::Data;
use Mojo::Base 'Mojolicious::Controller', -signatures;
sub serve_iframe ($c) {
$c->render(template => 'foo');
}
__DATA___
@@ foo.html.ep
what is this
我希望它能够按照 文档,而是在浏览器上抛出错误 无法呈现响应...
模板在日志中找不到“foo.html.ep”
。
我已经通过这样做解决了这个问题
$c->render(inline => data_section(__PACKAGE__, 'foo.html.ep') );
,但这似乎有点黑客。
我在这里做错了什么?
I am trying to serve a template contained in the __DATA__
section of a controller class, but it doesn't seem to work.
In my main app.pl
file I have
#!/usr/bin/env perl
use Mojolicious::Lite -signatures;
use FindBin qw($Bin);
use lib "$Bin/lib";
push @{app->renderer->classes}, 'Zairyo::Controller::Data';
push @{app->preload_namespaces}, 'Zairyo::Controller::Data';
get '/:uid' => [uid => qr/[a-z0-9]{32,32}/i ] => { controller => 'Data', action => 'serve_iframe' };
app->start;
and in Zairyo::Controller::Data
:
package Zairyo::Controller::Data;
use Mojo::Base 'Mojolicious::Controller', -signatures;
sub serve_iframe ($c) {
$c->render(template => 'foo');
}
__DATA___
@@ foo.html.ep
what is this
which I'd expect to work as per the documentation but instead throws an error Could not render a response...
on the browser and Template "foo.html.ep" not found
on the log.
I've solved this by doing
$c->render(inline => data_section(__PACKAGE__, 'foo.html.ep') );
but it seems a bit of a hack.
What am I doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,Data.pm 中存在一些问题:
__DATA
后有三个下划线,而本应有两个下划线和一个换行符这是我最终得到的结果:
在主脚本中,我在调用
start
之前加载该类。请注意,文档说:,并且
warmup
会立即由start
调用,并且它是关心
。您需要更快地到达那里,因此preload_namespaces
的预热preload_namespaces
对这个特定问题没有任何作用。如果您尚未加载该模块,则不会检测到其__DATA__
模板。我并不是建议这个特定的代码,但现在我知道为什么你没有得到你想要的东西。
First, there are a few things a bit off in your Data.pm:
__DATA
when there should be two and a newlineHere's what I ended up with:
In the main script, I load the class before I call
start
. Note that the docs say:And,
warmup
is called immediately bystart
, and it'swarmup
that cares aboutpreload_namespaces
. You need to get there even sooner, sopreload_namespaces
does nothing for this particular problem. If you haven't already loaded the module, its__DATA__
templates will not be detected.I'm not suggesting this particular code, but now I know why you weren't getting what you wanted.