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 (Score:2, Informative)
Re:Template::Plugin::Dumper (Score:1)
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_refto contain a reference pointing baRe:Template::Plugin::Dumper (Score:1)
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". ;-)