Pictures of the Flat and Templating Engines

If you ever have the need to make a mini template engine, may I recommend Ruby as the language to make it in? I’ve just spent an hour re-writing the one I wrote a couple of weeks ago in a much nicer way. For those who are curious, here’s an outline of the steps:

  1. Make a Templatable mixin with a single method, generate_from_template
  2. Mix this into a class, populated with some methods and instance variables
  3. In this method, use a regex to parse each # xx var from your template (get the template name from self.class.name, perhaps?)
  4. Take the xx and eval(xx) and sub! back into your line. The eval will, of course, have access to all your variables and methods from your newly Templatable class, as we mixed-in. Make a note of the fact that you can put any ruby code in your eval play around.
  5. Carry on doing this until you’ve run out of # xx to parse.
  6. Write out your fully evaluated template!

Very simple and powerful.

To finish, my flat in pictures:

Inner Lounge at Melrose Place

The Inner Lounge

The Dining Area at Melrose Place

The Dining Area

The Kitchen at Melrose Place

The Kitchen

Living room at Melrose Place

And finally, The Other Corner of the Living Room

← Older
Programs will Return
→ Newer
The UK's Science Teaching Farce