Mojolicious render_to_string和stash值和输出

时间:2018-04-20 22:18:16

标签: perl mojolicious

我在这里错过了一篇文章,希望有人能指出我做错了什么。

Mojolicious应用程序有一个路径/导出,用于创建href并将该数据发送到export.html.ep模板以呈现为字符串(将生成电子邮件)

模板已被剥离为裸骨以进行测试:

% my $data = stash 'data';
% dumper $data;

<div></div>

出口路线功能:

use base 'Mojolicious::Controller';
...
sub export {
    my $self = shift;
    my $log  = $self->log;
    my $href = {
        foo => "bar",
        boom => [ "three", "two", "one" ],
    };

    $self->stash(data => $href);
    my $html = $self->render_to_string();
    $log->debug("html is ", { filter => \&Dumper, value => $html });
}

我的测试人员是export.t:

...
$t->get_ok("/export")->status_is(200);
print Dumper($t->tx->res->content->asset->slurp);
...

在我的日志中,我看到了:

html is $VAR1 = bless( do{\(my $o = 'HASH(0xad01ef0)')}, 'Mojo::ByteStream' );

关于STDOUT我看到了:

ok 1 - GET /export
ok 2 - 200 OK
$VAR1 = '

<div></div>
';

所以看起来$ data并没有进入模板。另外,我希望render_to_sting提供一个字符串而不是Mojo :: ByteStream。

如何将$ href放入模板中,如何从模板渲染中获取text / html。

(最新版本的Mojo,perl 5.22,ubuntu 16.04系统)

谢谢,

1 个答案:

答案 0 :(得分:3)

footer: TabBar { id: tabBar currentIndex: swipeView.currentIndex Material.theme: Material.Dark FontAwesome { id: awesome resource: "qrc:///controls/fa-solid-900.ttf" } TabButton { font.pointSize: 10 font.family: awesome.family text: (awesome.loaded ? awesome.icons.fa_star : "x") + " " + "Général" } TabButton { font.pointSize: 10 font.family: awesome.family text: (awesome.loaded ? awesome.icons.fa_cogs : "x") + " " + "Test" } TabButton { font.pointSize: 10 font.family: awesome.family text: (awesome.loaded ? awesome.icons.fa_trophy : "x") + " " + "Match" } } 是Mojolicious中的reserved stash value。您可以使用不同的存储值传递数据,模板将获取它。

data
# app.pl
use Mojolicious::Lite;
get '/export' => sub {
    my $self = shift;
    $self->stash(data => { foo => "bar" });
    $self->stash(datx => { foo => "baz" });
    $self->render_to_string();
};
app->start;
__DATA__

@@ export.html.ep
% my $data = stash 'data';
% my $datx = stash 'datx';
<div>
bar: <%= $data->{foo} %><p/>
baz: <%= $datx->{foo} %><p/>
</div>