Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Jade-like templating engine for Java

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 src
Octocat-spinner-32 test-resources
Octocat-spinner-32 test
Octocat-spinner-32 views
Octocat-spinner-32 .gitignore
Octocat-spinner-32 README.md
Octocat-spinner-32 pom.xml
README.md

Jade

Jade-like templating engine for Java. Pretty much clones all the features found in jade-lang.com. The main difference is that it uses MVEL instead of javascript for the expressions.

In addition to this it also clones most of the (very cool) features in Scalate's Jade implementation.

We ship the following filters out of the box:

  • :markdown - requires MarkdownJ on the classpath
  • :javascript - wraps the inner text inside <script> tag
  • :plain - raw text dumped out
  • :cdata - outputs the inner text inside CDATA tag
  • :css - outputs the inner text inside <style> tag

My version of Jade is about 4x faster than Jade4j, the standard implementation linked from jade-lang.com. You can run the benchmark yourself.

Other differences from Jade4j:

  • -# Jade-level comments (no emitted content)
  • literal tags work (e.g. 'foo.bar' emits <foo.bar>)
  • attribute tags can be MVEL/Java expressions if using braces: p{ 'time' : new java.util.Date() }
  • any text in the template is a candidate for xml-escaping (not just | text lines)
  • Jade is more lenient with template parsing than Jade4j

...all this works perfectly in our Jade. whereas I was not able to get any of them to work in side-by-side tests (plz feel free to correct me if I'm wrong).

Usage

new Jade().process("html\n  body\n    p hello", new HashMap<String, Object>());

This will emit:

<html>
  <body>
    <p>hello</p>
  </body>
</html>

Note that we use an empty HashMap as the page context object. This is the object that you should fill with goodies (variables) that MVEL can read out and fill into your template.

To load the template from a file instead, use:

new Jade().execute("mytemplate.jade", new HashMap<String, Object>());

By default, Jade uses the FileTemplateReader which looks for templates in the file system (you can include templates inside each other using this scheme). To customize Jade, create your own JadeOptions object:

JadeOptions options = new JadeOptions();
options.setBaseDir("views");

Jade jade = new Jade(options);
jade.execute("home.jade", new HashMap<String, Object>());

This looks for and processes a template named home.jade in the views sub-directory of the current working directory.

Thread-safety

A Jade object is explicitly thread-safe and can be shared among multiple threads with no synchnronization overhead. The template cache and filter set are also concurrent and can be updated at any time.

Keep in mind

  • Jade does not currently support the block keyword from Javascript's Jade
  • Please consult the MVEL language for details on how to construct expressions
  • Add your own filters by implementing the Filter interface and calling Jade.register()

A quick note

Thanks to the original author of Jade, and respect to the authors of Jade4j. It was not my intention to dissect or one-up Jade4j, I implemented a Java version of Jade while not realizing that Jade4j existed until well into the project(!). So, rather than abandon a day's work, I ran a quick performance comparison and felt good that my effort was not wasted after all.

Also a copyright notice--I do not own, nor am I in any way affiliated with the original author(s) of Jade (seen at jade-lang.com). If you are really bothered by my referring to this project as Jade please feel free to call it MVEL Jade or Dhanji's Jade. =)

License is MIT. Do what you like with it, but some credit would be nice.

2012 Dhanji R. Prasanna. Or find me on twitter

Something went wrong with that request. Please try again.