Accessing template variables inside Wrapper (layout)
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 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> But it comes out as blank. Is dancer doing something special with the wrapper or am I doing something wrong? Thanks, Deepak
On 2010-12-30, at 12:14 PM, Deepak Gulati 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
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>
But it comes out as blank.
Is dancer doing something special with the wrapper or am I doing something wrong?
Hi Deepak, There is currently an open ticket for this: https://github.com/sukria/Dancer/issues#issue/83 Best, Olaf -- Olaf Alders olaf@wundersolutions.com http://www.wundersolutions.com http://twitter.com/wundercounter 866 503 2204 (Toll free - North America) 416 944 8306 (direct)
On Thu, Dec 30, 2010 at 11:50 PM, Olaf Alders <olaf@wundersolutions.com>wrote:
There is currently an open ticket for this:
I'll try to get my solution on the Cookbook and then close the ticket, assuming it satisfies the need. Thanks for the reminder. :) Sawyer.
On 2010-12-31, at 2:25 AM, sawyer x wrote:
On Thu, Dec 30, 2010 at 11:50 PM, Olaf Alders <olaf@wundersolutions.com> wrote:
There is currently an open ticket for this:
https://github.com/sukria/Dancer/issues#issue/83
I'll try to get my solution on the Cookbook and then close the ticket, assuming it satisfies the need.
It does. That's the solution I was looking for. Thanks! Olaf -- Olaf Alders olaf@wundersolutions.com http://www.wundersolutions.com http://twitter.com/wundercounter 866 503 2204 (Toll free - North America) 416 944 8306 (direct)
On Thu, Dec 30, 2010 at 7:14 PM, Deepak Gulati <deepak.gulati@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/68876b226a9ff7aca4f222bc7e7aacdf99c53... [1] https://github.com/szabgab/peg
Thanks.
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.
For some reasons TT2 documentation calls out META separately from the behavior of template variables being accessible via the wrapper. http://www.template-toolkit.org/docs/manual/Directives.html#section_WRAPPER "One side-effect of the "inside-out" processing of the WRAPPER configuration item (and also the WRAPPER directive) is that any variables set in the template being wrapped will be visible to the template doing the wrapping, but not the other way around." ... snip .... "It achieves the same effect as defining META items which are then accessed via the template variable (which you are still free to use within WRAPPER templates), but gives you more flexibility in the type and complexity of data that you can define." That said, this solution works for me in both cases: 1. Using META to declare a variable in the template and accessing it via template. inside wrapper. 2. Accessing a (non META) variable declared in the template directly inside the wrapper. So thanks again! I am nitpicking here, but probably we should warn/error out if both layout: and WRAPPER: configuration options are both active together in config.yml
On Fri, Dec 31, 2010 at 10:54 AM, Deepak Gulati <deepak.gulati@gmail.com>wrote:
http://www.template-toolkit.org/docs/manual/Directives.html#section_WRAPPER "One side-effect of the "inside-out" processing of the WRAPPER configuration item (and also the WRAPPER directive) is that any variables set in the template being wrapped will be visible to the template doing the wrapping, but not the other way around."
We do that as well, but differently. The way we do it, If you define a variable to a template, it is available to both the template and the layout. "It achieves the same effect as defining META items which are then
accessed via the template variable [...]"
The fix I presented earlier uses the template variable. So thanks again!
Happy to help! :)
I am nitpicking here, but probably we should warn/error out if both layout: and WRAPPER: configuration options are both active together in config.yml
I was contemplating this. I'm not sure we should issue warnings on what we *think* you aren't suppose to be doing. True, we do set warnings on by default (as well as strict), because we think it's important and some of these warnings are just that - things you probably didn't want to do. However, I'm still on the fence on this.
participants (3)
-
Deepak Gulati -
Olaf Alders -
sawyer x