Simple Templates with jQuery

Well this post isn't rocket science, but we all have seen the time where it would be nice to have some sort of templating when creating html with javascript. Well unless you need looks and conditions there's a pretty simple solution. Within your html string denote things you need to replace with "{myvar}". Then you will need a function like the following:

(function($) {

    $.templater = function(str,replacements){
            str = str.replace(new RegExp('{'+ i +'}','g'),this);
        return str;


Lets now take a simple example. Lets suppose we have this html stored as variable str

<a href="javascript:myFunc({id})">{text}</a>

Now we just call our new function and it will return us our result!

$.templater(str,{ id:15, text: 'Edit Item' });

Should now give the following:

<a href="javascript:myFunc(15)">Edit Item</a>

Not terribly complex, but very useful and reusable!