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

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

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

Replies are listed 'Best First'.
Re: An Idea for a New Template Toolkit Improvement(?)
by amw1 (Friar) on Mar 30, 2004 at 20:27 UTC
    I wasn't 100% clear on what you needed to do. This is my guess at what you were asking for. If this isn't it let me know. An example would be most helpful if this wasn't what you needed.

    perl code
    sub ContextString { my $context = shift(); if($context eq "foo") { return "String 1"; } elsif($context eq "bar") { return "String 2"; } } my $t_vars = { context_sub => \&ContextString, other => "mmm donuts", }; # config opts are whatever you need them to be my $template = Template->new($config_opts); $template->process("template_file.tt",$t_vars) || die $template->error();
    now for the template code
    . . . [% context = "bar" %] [% IF context %] Message: [% context_sub(context) %] [% END %] . . .
    in this case the template would be
    Message: String 2

      Thank you very much for the example code. I'm so annoyed that I just can't figure out a full example

      Where almost there. The only thing wrong is that I want the Template file to pass in the code, not the Perl file.

      Is that possible? How can I start implement it? Maybe mr. Wardley would appreciate a little patch... (:

      Any suggestions?


      "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

        You could turn on perl evaluation inside of the template. This would let you inline small chunks of perl code within the template. I generally try to write around doing things like that (for personal style issues mostly) but there's nothing really wrong with it.

        You could also have the perl function eval the argument that is passed to it. Generate the string of perl foo and pass that to a function similar to ContextString in my above example and have it get eval'ed

        You should try the tt mailing list:
        http://www.template-toolkit.org/info.html#lists
        --
        Clayton
Re: An Idea for a New Template Toolkit Improvement(?)
by DamnDirtyApe (Curate) on Mar 30, 2004 at 20:14 UTC

    Hmm... I'm not quite getting the what and why's of your question. Would you mind posting a more complete example, with a small sample template and what kind of information you hope to get from it?


    _______________
    DamnDirtyApe
    Those who know that they are profound strive for clarity. Those who
    would like to seem profound to the crowd strive for obscurity.
                --Friedrich Nietzsche