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.


In reply to Getting all used parameters from a Template::Toolkit template by Corion

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.