Template Toolkit

chrimble on 2003-11-05T11:10:33

I've been using Template Toolkit for the past two or three years now, and it is truly a marvel. But yesterday, I was asked by a coworker if it was easy to include a datastructure dump in the output containing all variables passed to a template object in the process call without having to do anything reciprocally to the back-end code (for the purposes of debugging). I wasn't sure, but it didn't sound too unreasonable, so I said I'd take a look.

After trundling through the documentation, getting unhelpful advice on IRC (shock horror!) and finally delving into the stash and poking around a bit, it became apparent that lots of things become intermingled in the generation process that I hadn't previously appreciated (leading to potentially unexpected results such as perl -we'use Template; Template->new->process(\"c:[% component %] f:[% foo %]", { component => "oops", foo => "bar" })'). So at least the short answer to the question would appear to be "no".

Of course, a solution involving embedding perl stash traversal code directly into the template or even writing a template plugin would be doable at a push, it's almost certainly not worth doing if you have access to the back end code.


Template::Plugin::Dumper

perrin on 2003-11-05T17:41:09

Template::Plugin::Dumper didn't do what you need?

Re:Template::Plugin::Dumper

chrimble on 2003-11-06T09:50:46

Template::Plugin::Dumper works great providing you know the name of the datastructure you want to dump - there's no way of dumping out the original datastructure you passed down in the process directive unless you do something like $template->process($tplfile, { mydata => $real_ds_ref });, sticking [% Dumper.dump(mydata) %] in your template, and updating all your other template variables accordingly.

My colleague didn't want to have to do this (or force $real_ds_ref to contain a reference pointing back to a copy of itself), and just see everything that was being passed down without any additional fuss.

Hence the stash-poking described above. ;-)

Having said that, it's probably possible to patch TT2 to force it's internal [% template %] (for instance) to contain a reference back to the original datastructure, but to be honest I'm not sure that that's desirable or even useful, because internally everything then gets munged into the stash and the template contructed from that. It certainly wouldn't help debugging the [% component %] issue mentioned above...

Re:Template::Plugin::Dumper

chrimble on 2003-11-06T09:57:35

Whilst I remember, the answer to the question "why not just dump the stash?" goes something like, "you could do that, but you'll end up dumping out all the other stash stuff which you probably wouldn't want. A better solution would be to write a template plugin to essentially do that, but filter out the stash stuff TT2 adds".

It was this point I thought, "blow that". ;-)