Template::Tiny 0.01 - Request for features

Alias on 2009-12-06T20:54:37

For a few years now, I've been thinking about doing a ::Tiny job on the Template Toolkit. But it was never quite a big enough problem to be worthwhile, because Template.pm doesn't have any particularly horrendous dependencies and always seems like a reasonable way to spend 2.2meg of RAM.

But for Padre 2.2meg is a hell of a lot to spend, because of the multiplier effect of threading. Template.pm is geared much more towards being a load focal point in a website or a content generation system, it is just that much too complex when used as a utility class for simple code generation (which is what I want to use it for in Padre).

So Template::Tiny is happening, and I've just uploaded the first release which support pretty much nothing other than basic [% foo %] insertions from a single variable HASH.

Just adding support for tags, plus a tiny bit of boilerplate, has memory consumption at around 24k of RAM consumed out of my anticipated budget of around 100k.

Before I go any further, I'd like to ask you guys what you consider to be the essential TT features that you would want to retain even in a light weight ::Tiny context.

For me, this includes the three basic array, hash and object foo.bar usages, and IF blocks (and potentially simple | filters).

What do you want?


Loops

Ovid on 2009-12-06T21:20:33

For it to be useful, I really think it needs loops.

<table>
[% FOR item IN items %]
  <tr><td>[% item.name %]</td><td>[% item.value %]</td>
[% END %]
</table>

I work hard (usually) to ensure there is no logic in my templates, but I can't do without loops. And it needs to be easily upgradeable to Template::Toolkit.

That being said, many people will be upset about the whitespace issues of the above. There are obvious ways of dealing with this for the general case (what if an entire line is nothing but template directives?), but I think that's all you're looking for here.

Re:Loops

Ovid on 2009-12-06T21:21:43

Er, pretend I didn't miss out on a closing "tr" tag :)

Re:Loops

bart on 2009-12-08T10:54:04

And filters. I may not imagine the bad things that would happen if item.name and/or item.value contain text with substrings that are meaningful as HTML markup.

Besides, for a template system I want to be able to insert text between every listed item, but not after the final item; just like join does in Perl, but then with a loop-ish instead of a functional syntax. (Just being able to test for the last iteration in the loop, would do the trick.)

I don't remember the syntax TT2 uses for such a feature, but I'm pretty sure it can do it.

Re:Loops

Ovid on 2009-12-08T12:40:56

Template Toolkit's syntax:

[% item.name | html %]

What I would wants...

sigzero on 2009-12-06T21:50:09

loops
variable substitution (of course)
simple config (where templates are found and the like)
maybe the "include" function as well

loop variable

leedo on 2009-12-06T23:07:51

The one thing I am missing from TT while working in Text::MicroTemplate is the loop variable. It lets you check what the loop index is and has a few convenience methods, such as loop.first.

http://template-toolkit.org/docs/manual/Variables.html#section_loop

Plugins

spacebat on 2009-12-07T00:26:32

What I'd like is the ability to have simple plugins to support custom directives. That way we'd be able expand TT::Tiny just a little to meet our needs.

Caveat: I haven't used TT

educated_foo on 2009-12-07T03:10:55

If T::Tiny doesn't have some looping construct, I don't see the advantage over a simple eval or string interpolation.

If statement

BillR on 2009-12-07T03:34:48

I don't like logic in templates, but I will set a flag in the stash and allow the template to control the presentation. For example, assume it is hard to figure out if something is "pending". After lots of hard work the program figures it out and sets $stash{pending}=1. Then the template simply has [% IF pending %]Item is pending![% END %] or whatever.

repeating what's been said

mpeters on 2009-12-07T15:03:27

These have all been mentioned, but I just wanted to add one more voice:

  • Loops - Not just loops, but also with the ability to tell first, index, last and possibly even vs odd.
  • Includes - Doesn't need to be as complicated or as varied as TT, but being able to break templates up avoids a lot of duplication.
  • Filters - You can't do web or XML stuff safely without having HTML escaping, and being able to define your own filters makes it easy to extend.