未找到 Mojolicious 控制器中的模板

发布于 2025-01-14 09:02:28 字数 1094 浏览 3 评论 0原文

我正在尝试提供控制器类的 __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 技术交流群。

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

发布评论

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

评论(1

轻许诺言 2025-01-21 09:02:28

首先,Data.pm 中存在一些问题:

  • __DATA 后有三个下划线,而本应有两个下划线和一个换行符
  • 模块不返回真实值

这是我最终得到的结果:

package Zairyo::Controller::Data;

use Mojo::Base 'Mojolicious::Controller', -signatures;

sub serve_iframe ($c) {
    $c->render(template => 'foo' );
}

1;

__DATA__

@@ foo.html.ep
what is this

在主脚本中,我在调用 start 之前加载该类。请注意,文档说

请注意,要检测模板,需要在调用预热之前加载并添加这些类

,并且 warmup 会立即由 start 调用,并且它是 关心 preload_namespaces 的预热。您需要更快地到达那里,因此 preload_namespaces 对这个特定问题没有任何作用。如果您尚未加载该模块,则不会检测到其 __DATA__ 模板。

#!/usr/bin/env perl
use Mojolicious::Lite -signatures;
use FindBin qw($Bin);
use lib "$Bin/lib";

push @{app->renderer->classes}, map { Mojo::Loader::load_class($_); $_ } 'Zairyo::Controller::Data';

get '/:uid'  => [uid => qr/[a-z0-9]{32,32}/i ] => {
    namespace  => 'Zairyo::Controller',
    controller => 'Data',
    action     => 'serve_iframe'
    };

app->start;

我并不是建议这个特定的代码,但现在我知道为什么你没有得到你想要的东西。

First, there are a few things a bit off in your Data.pm:

  • There are three underscores after __DATA when there should be two and a newline
  • The module does not return a true value

Here's what I ended up with:

package Zairyo::Controller::Data;

use Mojo::Base 'Mojolicious::Controller', -signatures;

sub serve_iframe ($c) {
    $c->render(template => 'foo' );
}

1;

__DATA__

@@ foo.html.ep
what is this

In the main script, I load the class before I call start. Note that the docs say:

Note that for templates to be detected, these classes need to have already been loaded and added before warmup is called

And, warmup is called immediately by start, and it's warmup that cares about preload_namespaces. You need to get there even sooner, so preload_namespaces does nothing for this particular problem. If you haven't already loaded the module, its __DATA__ templates will not be detected.

#!/usr/bin/env perl
use Mojolicious::Lite -signatures;
use FindBin qw($Bin);
use lib "$Bin/lib";

push @{app->renderer->classes}, map { Mojo::Loader::load_class($_); $_ } 'Zairyo::Controller::Data';

get '/:uid'  => [uid => qr/[a-z0-9]{32,32}/i ] => {
    namespace  => 'Zairyo::Controller',
    controller => 'Data',
    action     => 'serve_iframe'
    };

app->start;

I'm not suggesting this particular code, but now I know why you weren't getting what you wanted.

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