Works in IE, Firefox and Webkit browsers.Most templating languages suck

Ok, if your like me then you hate all templating langauges- they suck, they really suck. I hate them because I hate the rules they enforce upon me, they are slow, and they slow me down. Because of this, usually I end up making code that looks like this:


  1. function display(data) { 
  2. var output = "<div>" + data.text + "</div>"; 
  3. element.innerHTML=output
  5. }  

How horrible is that? That's probably worse than using a bad templating langauge.

Well, after disappointments with other jQuery templating plugins, I decided to make my own:

The Solution: PureJSTemplate

With PureJSTemplate you use old fashioned javascript in your template. You simply surround the javascript in special tags. Here's an example of what you can do:

  1. <#for(var i=0; i<10; i++) { #> 
  2. <#=i#> 
  3. <#}#> 

That will output the numbers 0 through 9. Easy, isn't it?

Using it:

You simply surround your template code with a textarea tag:

and call it from javascript like so:

  1. $("#output").pureJSTemplate( 
  2. id:"tpl", 
  3. data:{} 
  4. }); 

That's it.

转载请注明:代码家园 » PureJSTemplate