[dancer-users] How to get the path or name of a template in a hook before the template is rendered

Christoph Huhn huhn at webattractive.de
Mon Jan 27 10:42:46 GMT 2014


Hello fellow Dancers...

I'm using Dancer 1.3. I need to add the path (relative to views
directory) or at least file name of the template about to be rendered to
the template tokens hash passed to this template.

I don't want to replace the template set by the route handler, so a read
only solution for the template name would be OK.

I was hoping to do this with a before_template_render hook in a plugin,
but it looks like that's impossible right now, because the hook only
get's passed the tokens, not the filename.

Then I tried a solution via Template Toolkits META template.name in my
layout template, but that doesn't work either, because template.name is
the layout template, not the content template.

After checking the Dancer docs I found out that Dancer is not using TTs
WRAPPER, so that's not possible. I could add the META to my content
template, but the I'd have to change all of my templates and every
template call in my route handlers (wich are quite a lot by now).

I've searched the docs, googled quite a lot of Dancer related websites
and browsed through the code of some Plugins, but haven't found a
"official" way to get the template file path or file name.

I've seen a solution (in Dancer::Plugin::DebugToolbar) that iterates
over %INC and overrides every render method of any Dancer::Template::*
module it can find. That would work, but it feels kinda wrong to me.
There should be an easier solution to this, than digging in the guts  of
core or other plugin modules.

Any suggestions are most welcome.

Best regards, Chris



More information about the dancer-users mailing list