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

Callbacks and templates.

by LukeyBoy (Friar)
on Apr 13, 2004 at 05:40 UTC ( #344626=perlquestion: print w/ replies, xml ) Need Help??
LukeyBoy has asked for the wisdom of the Perl Monks concerning the following question:

So I'm working on a script that incorporates processing of a template and substituting tags with other data. The problem is that none of the template modules I can find involve callbacks - what I want is a function where I can feed in the template plus a function reference. The custom function would simply return the substituted text after arbitrary processing.

Anyone know of something like this?

Comment on Callbacks and templates.
Re: Callbacks and templates.
by davorg (Chancellor) on Apr 13, 2004 at 05:57 UTC
    The Template Toolkit can use callbacks. I'm not sure exactly what you want to do, but this demonstrates the general idea.
    #!/usr/bin/perl + use strict; use warnings; use Template; + my $tt = Template->new; + my $data = { word => 'callbacks', emphasise => \&emphasise }; + $tt->process(\*DATA, $data) || die $tt->error(), "\n"; + sub emphasise { return "** @_ **"; } + __DATA__ This is a template. It includes [% emphasise(word) %]
    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      That looks great, thanks!
      I didn't know you could do it this way. Neat.

      Anyway, TIMTOWTDI, even for Template-Toolkit. Here's the same demo modified to use a filter, which is like a "program" (thus, a sub) you pipe the data through.

      #!/usr/bin/perl -w use strict; use warnings; use Template; my $tt = Template->new(FILTERS => { emphasise => \&emphasise }); my $data = { word => 'callbacks' }; $tt->process(\*DATA, $data) || die $tt->error(), "\n"; sub emphasise { return "** @_ **"; } __DATA__ This is a template. It includes [% word | emphasise %]
Re: Callbacks and templates.
by kappa (Chaplain) on Apr 13, 2004 at 06:36 UTC
    You can also define callbacks using HTML::Template::Expr. Look:
    my $tmpl = new HTML::Template::Expr filename => 'template.html', functions => { site_name => \&site_name, };
    And in template you can write:
    <TMPL_VAR EXPR="site_name()">
Re: Callbacks and templates.
by Corion (Pope) on Apr 13, 2004 at 06:45 UTC

    The Petal templating system also allows you to call functions on objects, and even in a relatively nice (but still ad-hoc) fashion:

    # template <a tal:attribute="href object/link" href="test_link">Test link</a> # the matching code: my $object = {}; bless 'Obj', $object; my $i = 0; sub Obj::link { return sprintf "http://www.google.com?q=%s+Number", $i; };
Re: Callbacks and templates.
by dragonchild (Archbishop) on Apr 13, 2004 at 12:02 UTC
    What benefit would this provide over simply executing the function and passing the return value in? It sounds like you want a CMS and not a templating system. Maybe look at Mason? TT is also kinda-CMS-kinda-templating, and it does it quite nicely.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://344626]
Approved by kvale
Front-paged by matija
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (12)
As of 2014-09-03 02:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (35 votes), past polls