Shave : Ultra simple Javascript templates

I’m working on a new theme for PadPressed which is quickly becoming more of a JavaScript app built on a thin slice of PHP rather than a regular theme.  It also uses a lot of Ajax, so I found myself creating DOM elements on the fly  a lot. Concatenated strings is something I grew up tired of very, very quickly.

So I thought of using some kind of templating engine. I liked mustache.js the most, which is supposedly  a minimal templating engine. I like the concept a lot, but It turns out that it’s not so minimal, at least it was too much for what I needed.

My necessities are very simple: I want to pass a template as a string with variable placeholders , an object, and receive a parsed template. No logic, no edge cases, just that. So I came up with this simple function:

I use it like this:

Granted, developing for one single platform is a rare luxury among web designers, so I’ve not tested it outside Safari/iPad but I’m leaving it here, just in case somebody finds it useful.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>