【发布时间】:2018-07-25 02:42:37
【问题描述】:
我正在尝试将 stash 中的默认“title”变量设置为操作名称的英文版本。例如:
sub customer_orders {
...
}
应该有:
title => 'Customer Orders',
可在模板中使用。有谁知道如何做到这一点?谢谢!
【问题讨论】:
标签: perl mojolicious
我正在尝试将 stash 中的默认“title”变量设置为操作名称的英文版本。例如:
sub customer_orders {
...
}
应该有:
title => 'Customer Orders',
可在模板中使用。有谁知道如何做到这一点?谢谢!
【问题讨论】:
标签: perl mojolicious
您可以通过调用者获取子程序名称:
my $sub_name = (caller(0))[3];.
从您的输出来看,您可能还想将其大写并将 _ 替换为空格
$sub_name =~ s /_/ /g;
我会像这样大写:
my $title = join(' ', map{ ucfirst lc }split(' ', $sub_name) );
【讨论】:
看起来$c->action 在 Mojolicious 模板中可用为 $action。所以你可以这样做:
<title><%= title || action_to_title($action) %>
这样其他模板可以像这样覆盖标题:
% title 'My Override Title'
如果 title 未设置,您可以添加 action_to_title 帮助程序来为标题准备操作。
【讨论】:
您可以将标题放入布局模板中
<!DOCTYPE html>
<html>
<head>
<title><%= content 'title' %></title>
<style>
label.field-with-error { color: #FF4C4D }
input.field-with-error { background-color: #FF4C4D }
.error_msg { color: #FF4C4D }
</style>
%= content 'styles';
</head>
<body>
%= include 'basic/menu'
%= include 'basic/error_messages'
%= content
%= content_for 'include_js'
</body>
</html>
然后在您的模板中填写content(另请参阅content_for、content_with)
% content_with title => "$title_schet за $title_date";
% content_for styles => '<link rel="stylesheet" type="text/css" href="/static/css/report.css">';
<form>
...
</form>
【讨论】: