http://qs321.pair.com?node_id=435262


in reply to Re: Templating algorithm - pass in variables vs callback?
in thread Templating algorithm - pass in variables vs callback?

It's not that I'm well-versed in many template systems because it has been a while since I looked at them all, but just going with the one I use the most, HTML::Template, for a second, I remember a method like this. Unfortunately, it scares me too much. It says that you can associate an object that has a param method like its own, one behaviour of which is to list all the parameters it has. And my dynamic method may accept hundreds or thousands of parameters, and looking them up is exactly the overhead I was trying to avoid.

If HTML::Template said that it only needed the aspect of param which is to retrieve the value, that would be perfect. Even if that's all the code does (I've not checked), I would not feel comfortable about it until the docs were updated to give me some sense of security about the long-term stability of the requirement.

Are the other templates out there doing something less invasive? I understand that this was intended to allow you to put your CGI object directly into the template to fasttrack some parameters, so the desire to change it would be quite low by the developers.

  • Comment on Re^2: Templating algorithm - pass in variables vs callback?

Replies are listed 'Best First'.
Re^3: Templating algorithm - pass in variables vs callback?
by lachoy (Parson) on Mar 01, 2005 at 02:36 UTC

    No, that's not what I'm talking about. Systems like Template Toolkit allow you to pass an object to a template as the value of one of the hash keys you pass in. You can then call methods on the object from the template and they're called just like normal method. For instance, given the dummy class:

    package Foo; sub new { return bless ( { count => 1 } ) } sub count { $_[0]->{count}++ } 1;

    And the following template code:

    use Foo; use Template; my $template = Template->new(); $template->process( \*DATA, { bar => Foo->new() } ) || die "Cannot process: ", $template->error(), "\n"; __DATA__ I am calling methods on the object named 'bar': Call: [% bar.count %] Call: [% bar.count %] Call: [% bar.count %]

    You'll see:

    I am calling methods on the object named 'bar': Call: 1 Call: 2 Call: 3

    Chris
    M-x auto-bs-mode

Re^3: Templating algorithm - pass in variables vs callback?
by Thilosophy (Curate) on Mar 01, 2005 at 03:14 UTC
    Actually, HTML::Template allows you to pass in subroutines which will get called to get the parameter values:
    # with a subroutine reference that gets called to get the value # of the scalar. The sub will recieve the template object as a # parameter. $self->param(PARAM => sub { return 'value' });
    However, since these subroutines cannot be parameterized from within the template, I am not sure how much use this is.

    I'd love to be able to do the following (which is not supported, however):

    $tmpl->param( blah => sub { my ($a, $b, $c) = @_; return 'something'; } ); <tmpl_var name='blah(1,2,3)'>

    I think HTML::Template should have some simple kind of expression language that would allow you to these things, as well as drilling down into hashes and arrays, accessing objects (which should eliminate the need for callbacks), and doing simple arithmetics .

    HTML::Template::Expr is doing some of these things already.

    (Like everyone else, I have started to implement my own templating system with these ideas in mind. It is mostly a project to teach myself about Parrot, but if you are interested: http://budgie.sourceforge.net/ )