Friday, April 4, 2008

Sexy Templates

Yes, my templates are going to be sexy! Full of sass!

If things go as planned (pfft, do they ever?) my renewed Template engine is going to be streamlined as hell. Filters' structure will most probably be untouched (what do you expect, they get a value in and they're expected to return a value). I still have the TOM (Template Object Model) class left, but it's a whole different story now. The old TOM is kind of integrated into Template and perhaps partly in the new TOM, but tags are also a part of TOM now.

Tags are now also classes, instead of functions. I'm not sure what kind of performance implications this will have, but the Template object is skinned to the bone now, so perhaps they counter each other. But yes. Tags are expanded TOMs, that have a special method evaluate() that actually does the stuff. But, instead of having a "here's the tree, have fun!"-approach, it's much friendlier this time around.

  1. The template is parsed in an intuitive top-down-nested order - just as it should be!
  2. The aforementioned evaluate() that acts as your main method, and you write all the actions inside this.
  3. Auxiliary method step() advances one 'step' in the tree, and returns false if the tree is already at the end, or otherwise a string with the contents from the tree and advances one step.
  4. Auxiliary method evaluateVariable($var) is your holy grail of variable evaluation. If the tag accepts a variable, just put it through this method, and out comes the evaluated value - they are handled identically to the {{ var }} -variable tags, filters and all!
  5. Tags can expect either nodes (a block tag) or a tag (simple tag) explicitly, by just calling either the auxiliary methods expectsNodes() or expectsTag().


Dead easy, dead sexy!

What's un-sexy, however, is the fact that I have to rewrite all tags, including if-then-else :(

No comments: