Mojo Templates

12 Nov 2009 | By Alex Young | Tags templates view

Mojo is an interesting project that brings Mustache templates to JavaScript. Mojo provides a command line tool, mojo for building Mustache-based templates using JavaScript syntax and data structures.

A template looks like this:

<html>
  <head>
    <title>{ title }</title>
  </head>
  <body>
    <h1>{ title }</h1>
    {# articles }
      <div class="article">
        <h2>{ title }</h2>
        <p>{ body }</p>
        {# published }
          <p>{ title } is published</p>
        {/ published }
      </div>
    {/ articles }
  </body>
</html>

And a corresponding template object would look like this:

page = {
    title: 'Articles',
    articles: [
      { title: 'One', body: 'some more one' },
      { title: 'Two', body: 'some more two', published: true }
    ]
}

This could be used with a server-side JavaScript framework for a fast, reusable and native templating solution.

The author uses the JSpec testing framework for tests, which is also worth checking out if you haven’t seen it.


blog comments powered by Disqus