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:
- Make a Templatable mixin with a single method, generate_from_template
- Mix this into a class, populated with some methods and instance variables
- In this method, use a regex to parse each # xx var from your template (get the template name from self.class.name, perhaps?)
- 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.
- Carry on doing this until you’ve run out of # xx to parse.
- Write out your fully evaluated template!
Very simple and powerful.
To finish, my flat in pictures:
The Inner Lounge
The Dining Area
And finally, The Other Corner of the Living Room