Template::Toolkit META variables and SETs in Dancer
There is one very advanced feature in Tempate::Toolkit called META variables. META variables are variables that you define in a processed template, that are later available to the WRAPPER template. That means that you can set, for example, the title of the page in the main layout from the inner content template. That's also what it's usually useful for.
However, since Dancer provides its own "layout" option, it basically separates these two processes (rendering a WRAPPER and rendering an inner template), making Template::Toolkit unable to simply define a WRAPPER. So... how does one get it to work?
Well, it's possible to kindly ask Dancer to step aside for a bit, and give you more control over the templating, which means you can do some more advanced stuff, like using SET to set variables in the WRAPPER, or using META variables. Here's how easy it is:
In your config.yml file, you need to:
- Disable your "layout" configuration (either comment or remove it)
- Make sure you're using Template::Toolkit:
- Add the following configuration to enable the main.tt as a WRAPPER:
template: "template_toolkit"
engines:
template_toolkit:
WRAPPER: layout/main.tt
There! You now have full and complete control over the template. Dancer's template() DSL will still work, but it will no longer separate the layout processing to a different process, so you have a lot of extra control over it.
Checkout for the PEG (Perl Ecosystem Website) configuration file, contact template and main wrapper (specifically the title) for a live example.
Enjoy! :)
Thank you! Just got bitten by this myself!