如何在Dancer模板中制作页面特定的标题? [英] How to make a page-specific title in Dancer templates?
问题描述
我有一个标准的Perl Dancer应用程序,使用Template :: Toolkit作为渲染引擎,具有两条路径:
I have a standard Perl Dancer app, using Template::Toolkit as rendering engine, with two routes:
get '/' => sub {
template 'index';
};
get '/foo' => sub {
template 'foo';
};
我的视图/模板/main.tt
包含以下行:
<title><%= title %></title>
我希望标题var的值是 /页上的我的网站,以及 Foo -我的网站上的 / foo页面。
I want the value of title var be "My Site" on page '/', and "Foo - My Site" on page '/foo'.
我知道我可以将这些值放在控制器文件中,例如:
I know I can put these values in the controller file, like this:
template 'index', { title => 'My Site' };
但我想在相应的模板文件中指定它们,视图/索引.tt
和 views / foo.tt
。
but I want to specify them in the corresponding template files, views/index.tt
and views/foo.tt
.
我该怎么做?
谢谢。
推荐答案
此文档明确说明了如何将应用程序配置为在您的布局中提供模板中定义的变量。
This documentation clearly explains how to configure your application to make available in your layout the variables defined in your templates.
对于title标签,您可以省去使用变量<$在每个模板中定义标题的工作量。 c $ c> template.name 。也许是这样的:
For the title tag, you can save yourself the effort to define the title in each template using the variable template.name
. Maybe something like that:
<title>
<% template.name.match('(\w+).tt').0.ucfirst %> - <% settings.sitename %>
</title>
这篇关于如何在Dancer模板中制作页面特定的标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!