[Dancer-users] Accessing template variables inside Wrapper (layout)

sawyer x xsawyerx at gmail.com
Fri Dec 31 08:23:57 CET 2010


On Thu, Dec 30, 2010 at 7:14 PM, Deepak Gulati <deepak.gulati at gmail.com>wrote:

> Hi Folks,
>
> Template toolkit allows me to set variables inside a template and then
> access them from the wrapper:
> http://www.template-toolkit.org/docs/manual/Config.html#section_WRAPPER
>

Yes, these variables are called META variables. If you're on the IRC
channel, you've probably heard me mention them quite a bit recently.


> This doesn't seem to when using Template Toolkit with Dancer. e.g. in
> the index.tt that dancer generates, I added:
> <% foo = "bar" %> on the first line
>
> and tried to access it from main.tt
> <title><% foo %></title>
>

Two reasons:
1. Dancer does not use TT's WRAPPER functionality, since it doesn't exist
everywhere (and definitely is not called the same, nor written the same,
everywhere). Dancer does its own wrapper (called layout) for you. The
downside is that you don't get TT's WRAPPER, but Dancer's layout. The upside
is that every template engine (even Template::Tiny) gets a layout, which is
awesome.
2. Since Dancer does not use TT's WRAPPER, you do not have META variables.
META variables are created by TT's Finite State Machine (FSM) processing of
the templates. It does not (as far as I know) allow you to separate these
two states and provide the META variables separately. It *has* to go through
TT's WRAPPER functionality and be done using TT's FSM engine.

Is dancer doing something special with the wrapper or am I doing
> something wrong?
>

Dancer is not using the WRAPPER, but provides its own called "layout".
That's the summary.

Thanks,
>

But I have more! :)

Recently I've been contributing to the Dancer code of the PEG (Perl
Ecosystem Group) website[1] and the one thing that *had* to be fixed was
these META variables. There was a lot of content that should have been in
templates but instead lay hardcoded inside the routes file. So I set about
to fix it.

I've been able to get META variables working relatively easily. I wanted to
write a post about it, but haven't gotten around to it yet. Here is a short
explanation of how I did it:

1. In the configuration I removed the layout entry.
2. I added a configuration for the template engine and set up the layout as
a WRAPPER:
engine:
  template_toolkit:
    WRAPPER: 'layouts/main.tt'

3. Now you can use META variables.

Here you can see the commit diff that accomplishes it:
https://github.com/xsawyerx/peg/commit/68876b226a9ff7aca4f222bc7e7aacdf99c539c8

[1] https://github.com/szabgab/peg
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.backup-manager.org/pipermail/dancer-users/attachments/20101231/c8922a01/attachment.htm>


More information about the Dancer-users mailing list