[Dancer-users] Accessing template vars in layout

Olaf Alders olaf at wundersolutions.com
Sun May 16 14:14:04 CEST 2010


On 2010-05-14, at 7:51 PM, Naveed Massjouni wrote:

>> 'm not sure about accessing variables inside your layout that you
> have created inside of a template in your views folder.  But you can
> access variables that you pass from your route:
> 
> If you had the following route:
>  get '/foo' => sub {  template foo => { user => 'bob' } };
> 
> Then in your layout, you would be able to access the variable user
> just like you can inside of a template.  So in views/layout/main.tt
> you can have:
>  hello [% user %]


Hi Naveed,

Thanks very much for this, but it doesn't quite solve my problem.  I've got the following in my layout/main.tt

<html>
<head>
<title>[% title %]</title>
...

Now, since I'm using auto_page, the web designer can add pages as he sees fit and the routes will be created.  What I'd like him to be able to do is to set the title tags from within individual pages.  Eg in contact.tt:

[% title = 'Contact Us' %]

I had thought that the layouts would be using TT's wrapper functionality, which would enable this, but that's not the case.  So, my workaround is something like this in main.tt:

[% title = 'default title' %]

[% IF request.path == '/' %]
    [% title = 'home page title' %]
[% ELSIF request.path == '/contact' %]
    [% title = 'contact page title' %]
[% END %]

That gets the job done, but it's not nearly as clean as I'd like and I need to trust the web designer to get the syntax correct etc.  Is there a way to get Dancer::Template::TemplateToolkit to use a wrapper by default?  


Thanks,

Olaf
--
Olaf Alders
olaf at wundersolutions.com

http://www.wundersolutions.com
http://twitter.com/wundercounter

866 503 2204 (Toll free - North America)
416 944 8306 (direct)



More information about the Dancer-users mailing list