Better JavaScript Templates

      Digg it  Save This Page

What is it?

JavaScript Templates (Jst) is a pure Javascript templating engine that runs in your browser using JSP-like syntax. If that doesn't sound familiar, check out the live working example below.


Why did I write this?

  1. TrimPath failed with JSTL errors when running in Jetty with default settings.
  2. TrimPath isn't pure Javascript*. I think Velocity's syntax is terrible. I wanted JSP-like syntax.
  3. Embeddedjs failed to retrieve a template from within an Ajax call. That is, I was already using Ajax within a drag n' drop scriptaculous function call, so embeddedjs couldn't use Ajax to retrieve the template from the server.
  4. I wanted the templates stored in the page anyway, like TrimPath, not on the server.
  5. Neither Embeddedjs nor TrimPath linted well, so they can't be minified. The download for embeddedjs is 2.5X that of my Jst implementation and TrimPath is 3.5X as big!
  6. I already wrote the parser years ago in Java for server-side Javascript. It was easy to port to JS.

  7. * Right from TrimPath's homepage: ${p.name|capitalize}
      What is that? Better to put the capitalize method on the String prototype and do this:
          <%= p.name.capitlize() %>
      You get the benefit of having a better String object and you don't have to learn a one-off syntax.

Benefits

  1. Only 5k minified!
  2. Pure Javascript with JSP syntax.
  3. Works within Ajax calls.
  4. Doesn't conflict with JSTL on the server.
  5. One line renders the template (see the one liner below).
  6. Only ~250 lines of executable code, and I haven't looked to refactor it to make it smaller. It works well. If it ain't broke...

Example usage

// src == your template element ID (template must be stored in a textarea)
// dest == the element where the rendered HTML goes
// data = any data your template will need when rendering
// Jst.evaluate(src, dest, data);

Jst.evaluate("template", "rendered", {"name":"flintstone"});


Example template



Test Drive It

Feel free to edit the template above, then hit the render button.

Enter your name:
The HTML will be rendered in this DIV

License

Copyright 2008, mark turansky (www.markturansky.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

The Software shall be used for Good, not Evil.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

(This is the license from www.json.org and I think it's awesome.  ~mgt)

Download