http://www.perlmonks.org?node_id=749813

Corion has asked for the wisdom of the Perl Monks concerning the following question:

I have a basic question, but so far haven't found the answer in the Template Toolkit documentation.

I want to find a list of all parameter names that are (possibly) used in a template. For example:

Name: [% user_name %] Password: [% user_pass %]

should return me a list of (say)

my @items = find_used_template_parameters($template); is_deeply \@items, [qw[ user_name user_pass ]];

You're asking why I would want something like that. This would make it very, very convenient while developing a CRUD application to have a customized template that still accomodates for new fields becoming available without requiring the HTML forms to keep up. I could introduce an additional section that lists all "user defined fields" that haven't been mentioned somewhere else in the template already. For example, let's assume that a new template parameter, user_email, became available. I would then want the following template:

Name: [% user_name %] Password: [% user_pass %] [% IF has_unused_fields('/^user_/') %] [% list_unused_fields('/^user_/') %] [% END %]

produce the following, unsightly yet functional output:

Name: Corion Password: Secret user_email: corion@corion.net

I want list_unmentioned_parameters to list only the parameters that have not been mentioned in the template at all, presumably because the designer has not placed them in a location aesthetically pleasing. Bonus points if the calling application can provide a list of parameters that are to be checked, so not all parameters passed to the template need to be output.

I hope that this kind of template introspection is somehow possible, or at least, it would be very nice if it were so. I'm not wed to Template Toolkit, so if any other templating system provides this kind of introspection, I'm quite willing to look into it.