1

我有一个帖子路线,它返回参数以在特定模板(actions.tt)中使用。在该模板中,我使用 DirectoryView 插件加载了一个带有目录视图 (dirmain.tt) 的 div(使用 jQuery)。我的问题是我需要在渲染主模板(action.tt)之前将参数传递给 DirectoryView 模板。参数(dev)需要包含在目录列表中。

Perl部分:

Use Dancer;
....
post "/" => sub {
template 'actions.tt', {
    'dev' => param('dev'),
};

模板:

动作.tt

....
<div id="dir">
    <script type="text/javascript">
          $('#dir').load('/files/[% dev %]');
    </script>
</div>
....

目录

....
how do I pass [% dev %] here before the action.tt is rendered by the browser?
....

使用某种钩子会实现这一点吗?非常感谢您的帮助。谢谢!

4

1 回答 1

2

params is a function that delivers a hash, so params("dev") won't get you anything. This is better:

template 'actions.tt', {
   'dev' => param->{dev},
}

but in Dancer, some variables like params, request and session are exported by default in the templates. So you can leave the {} after 'template' empty and use this in your template:

$('#dir').load('/files/[% params.dev %]');
于 2012-02-02T21:25:23.453 回答