public
Description: Mustache templates for JavaScript - C compiler for ultra fast logic-free templates!
Homepage:
Clone URL: git://github.com/visionmedia/mojo.git
visionmedia (author)
Tue Nov 17 11:43:04 -0800 2009
commit  b6acced0166af81930d42088440ca32bef2213b1
tree    89c63f089f1f6c016989683812c14f0735a4bd2a
parent  292d03aa4777e15634daa3c90575971ef6329452
mojo /
name age message
file History.md Fri Nov 06 11:38:10 -0800 2009 Release 0.2.0 [visionmedia]
file Makefile Fri Nov 06 11:49:18 -0800 2009 Added benchmarks [visionmedia]
file Readme.md Tue Nov 17 11:43:04 -0800 2009 Double curlies for examples [visionmedia]
directory benchmarks/ Fri Nov 06 11:49:18 -0800 2009 Added benchmarks [visionmedia]
directory bin/ Fri Nov 06 17:28:11 -0800 2009 Tabs to spaces [visionmedia]
directory examples/ Tue Nov 17 11:43:04 -0800 2009 Double curlies for examples [visionmedia]
directory lib/ Mon Nov 09 11:56:59 -0800 2009 Fixing tabs [visionmedia]
directory spec/ Tue Nov 17 11:43:04 -0800 2009 Double curlies for examples [visionmedia]
directory src/ Fri Nov 06 11:38:10 -0800 2009 Release 0.2.0 [visionmedia]
Readme.md

Mojo

Mustache template compiler for ultra-fast native language templates.

Supported languages

  • JavaScript

Dependencies

  • gcc (compiler)
  • leg (parser generator)
  • jspec (test suite only)

Benchmarks

mojo        : 5000 times  : 0.995 seconds
mustache    : 5000 times  : 7.059 seconds
mustache.js : 5000 times  : 21.669 seconds

$ make benchmarks

Installation

Include lib/mojo.js in your application and:

$ make
$ make install

Mojo Binary

$ mojo < in > out
$ mojo --help

Running Tests

$ make test

Example

View ./examples to see the source for the examples shown below.

examples/template.html

<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>

complete template using mojo:

$ mojo < examples/template.html > examples/template.html.js

yields the following javascript:

'<html>\n\
  <head>\n\
    <title>' + (Mojo.escape(Mojo.normalize(o.title))) + '</title>\n\
  </head>\n\
  <body>\n\
    <h1>' + (Mojo.escape(Mojo.normalize(o.title))) + '</h1>\n\
    ' + Mojo.enumerate(o, Mojo.normalize(o.articles), function(o){
    return '\n\
      <div class="article">\n\
        <h2>' + (Mojo.escape(Mojo.normalize(o.title))) + '</h2>\n\
        <p>' + (Mojo.escape(Mojo.normalize(o.body))) + '</p>\n\
        ' + Mojo.enumerate(o, Mojo.normalize(o.published), function(o){
    return '\n\
          <p>' + (Mojo.escape(Mojo.normalize(o.title))) + ' is published</p>\n\
        '}) + '\n\
      </div>\n\
    '}) + '\n\
  </body>\n\
</html>'

example template rendering implementation:

load('lib/mojo.js')

function render(template, o) {
    return eval(readFile('examples/' + template + '.html.js'))
}

template object:

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

print(render('template', page))

rendered markup output:

<html>
  <head>
    <title>Articles</title>
  </head>
  <body>
    <h1>Articles</h1>

    <div class="article">
      <h2>One</h2>
      <p>some more one</p>
    </div>

    <div class="article">
      <h2>Two</h2>
      <p>some more two</p>
      <p>Two is published</p>
    </div>

  </body>
</html>

The templates produced by mojo can be simply copy / pasted into your application, or loaded via XMLHttpRequest, or via IO for server side implementations.

License

(The MIT License)

Copyright (c) 2008 - 2009 TJ Holowaychuk tj@vision-media.ca

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 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.