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
| [reply] [Watch: Dir/Any] [d/l] |
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 %]
| [reply] [Watch: Dir/Any] [d/l] |
That looks great, thanks!
| [reply] [Watch: Dir/Any] |
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()">
| [reply] [Watch: Dir/Any] [d/l] [select] |
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;
};
| [reply] [Watch: Dir/Any] [d/l] |
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
| [reply] [Watch: Dir/Any] |