Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Think about Loose Coupling
 
PerlMonks  

Re: Templating algorithm - pass in variables vs callback?

by lachoy (Parson)
on Mar 01, 2005 at 01:29 UTC ( #435254=note: print w/ replies, xml ) Need Help??


in reply to Templating algorithm - pass in variables vs callback?

Since many templating systems allow you to pass in an object, couldn't you just bundle your desired functionality into it, pass that to the template and route all your dynamic, lazy loading calls through it? That seems a lot more straightforward than creating a separate way of passing parameters to a template.

Chris
M-x auto-bs-mode


Comment on Re: Templating algorithm - pass in variables vs callback?
Re^2: Templating algorithm - pass in variables vs callback?
by Tanktalus (Canon) on Mar 01, 2005 at 02:19 UTC

    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.

      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

      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/ )

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://435254]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (16)
As of 2014-04-16 17:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (433 votes), past polls