Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

bart (450)

Journal of bart (450)

Thursday May 31, 2007
04:38 PM

"Nice in theory": curried templates

[ #33389 ]

Some time ago, I had a wild idea. I have no idea how to implement it, so I'm just going to describe how it could be here, hoping that someone will get inspired by the idea and run with it, implement it, and hopefully in Perl. It sounds to me like it might actually be very nifty. So let me explain the concept.

In functional languages, there's an often tauted feature called curried functions. Most "normal" languages have user defined functions, which expect a particular set of parameters to work, and if you fail to pass them all, it'll just barf. Other languages just fill in defaults for the missing values.

Other people thought they might be able to make it do something more useful instead, and they implemented "curried functions" in some of the so-called "functional languages", or as I could call them: "partially executed functions". A function, without the need for the programmer who wrote it to have to do anything extra, can take fewer parameters than required, fill them in, and return a function instead of a plain result, that to execute, simply requires the passing in of the rest of the parameters. Pass in all required parameters, and you get the final function value.

Move over to templates. Templates are a lot like functions, where the template is the function, the values to fill in are the function parameters, and the final (string) output is the function value.

Many dynamic websites depend on templates for the layout for their pages, complemented with code for the logic. But most often, going from template to page is a single step: you need to provide all the values for the variables to produce usable output.

It's quite common that these websites, when they do get to produce the final webpage (or a part of a webpage, a "pagelet", typically a rectangular area), to store it in a cache (memory), so that next time, as long as the data doesn't change, they don't have to regenerate the pagelet again on ever page load. Instead, it fetches it from the pagelet cache.

If one value can't be filled in, then you can't cache the pagelet. You have to produce it at the end, when the final webpage is built.

Introduce "curried templates". Just like curried functions, that would be a template that may be invoked with an incomplete set of variables, and then you'd end up with a new, partially filled in, simpler, template.

A typical use could be on a news site, where the text of a news article in a boilerplate template. In the resulting template, you can then fill in the parameters for an ad, so every viewer of the web page could see the same article with a different ad.

I'm quite sure that the code to produce fully cached pagelets, and to produce dynamic pagelets from scratch, typically look nothing alike at all. And that is a shame. I can imagine that using these curried templates, not only would the code for both extremes would look very much alike, but so could the code for any intermediate form. It's just a matter how much values you already fill in at the first templating stage.

Now, like I said, I have no idea where to start implementing such a system, but it sounds like it would be too cool. And I do think Perl's very capable of implementing such a system without too much black magic.

A currying template could always produce a new template object, where stringification of the object is overloaded to produce the flattened string output. That way you never have to distinguish between the curried or the plain output. It almost sounds too easy.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • This sounds kinda like doing multi-pass templates in Template Toolkit using the TAG_STYLE option. You might have a template that looks like:

    Todays Sponsor: [* INCLUDE title_ad *]
    Enjoy this article:
    [% INCLUDE article_text %]
  • Template::Declare