Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-25 06:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found