How to make a page-specific title in Dancer templates?

一笑奈何 提交于 2019-12-24 00:59:30

问题


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';
};

My views/templates/main.tt contains the line:

<title><%= title %></title>

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' };

but I want to specify them in the corresponding template files, views/index.tt and views/foo.tt.

How can I do that?

Thanks.


回答1:


This documentation clearly explains how to configure your application to make available in your layout the variables defined in your templates.

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>


来源:https://stackoverflow.com/questions/11970625/how-to-make-a-page-specific-title-in-dancer-templates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!