First, I need to make it clear that I'm not sure that this is really usefull, or the Best Way To Do It(tm). So, before you pass trough, please take a minute and think about the problem. Maybe you know a way of doing this that I don't.

I'm building need to build context-driven custom messages for some situations in my system. I'm using Andy Wardley's Template::Toolkit for building the Templates and need to figure out a way to read meta-information from the template, so I can for example do something like this:

Update: Added a little more consistent example code.

[%# Template File %] [% META # META is a kind of a new tag, and stores perl # code for each key needed by the Template. title => sub { use Class::DBI::AutoLoader( %params ); return DATA::Table->retrieve(code=>'A'); } name => sub { return +shift @NAMES || 'Default Name' } %] [% title %] And here goes the template text, and the template [% name %].

# Perl File # %parameters are your template parameters, don't care. my $template = new Template( %parameters ); # data will hold template filling data my %data; map { $data{$_} = eval $template->code_for( $_ ) } $template->needed_k +eys( 'Template.tt' );

In other words, I need an idea for a mechanism that can associate a piece of perl code (in string format) that can be eval()'ed to get the actual value for each variable needed by a template.

Do the Perl Monks have any suggestions of ways of doing this "magic"?

As I need a quick'n'dirty trick, I'll accept any kind of suggestion, provided that there is a minimum change that it works. I just workarrounded it, so now if this is useful in any way, I can try to write a module...

Thank you all in advance for your precious ideas and time!


"In few words, translating PerlMonks documentation and best articles to other languages is like building a bridge to join other Perl communities into PerlMonks family. This makes the family bigger, the knowledge greater, the parties better and the life easier." -- monsieur_champs


In reply to An Idea for a New Template Toolkit Improvement(?) by monsieur_champs

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.