templatizer.js
Simple solution for compiling jade templates into vanilla JS functions for blazin' fast client-side use.
What is this?
Client-side templating is overly complicated, ultimately what you actually want is a function you can call from your JS that puts your data in a template. Why should I have to send a bunch of strings with Mustaches {{}}
or other silly stuff for the client to parse? Ultimately, all I want is a function that I can call with some variable to render the string I want.
So, the question is, what's a sane way to get to that point? Enter jade. Simple, intuitive templating, and happens to be what I use on the server anyway. So... Jade has some awesome stuff for compiling templates into functions. I just built templatizer to make it easy to turn a folder full of jade templates into a CommonJS module that exports all the template functions by whatever their file name.
Is it faster?
From my tests it's 6 to 10 times faster than mustache.js with ICanHaz.
How do I use it?
npm install templatizer
- Write all your templates as individual jade files in a folder in your project.
- Somewhere in your build process do this:
var templatizer = require('templatizer');
templatizer(__dirname + '/templates', __dirname + '/demo_output.js', options);
So a folder like this
/clienttemplates
user.jade
app.jade
/myfolder
nestedTemplate.jade
Compiles down to a JS file that looks something like this:
var jade=function(exports){ ... }
exports.user = function () {}
exports.app = function () {}
exports.myfolder.nestedTemplate = function () {}
The awesome thing is... there are no external dependencies because they're just functions at this point. Crazy fast, SO MUCH WIN!!!!
Options
The third parameter passed to templatizer
is an options object.
namespace
(string, default window
)
If you are using templatizer as a global in the browser (without node, requirejs, browserify, or something similar) by default it will attach itself to window
. Using namespace
you can attach it to a different global object. For example:
templatizer(templatesDir, 'templates.js', {
namespace: 'app'
});
<script>var app = {};</script>
<script src="templates.js"></script>
<script>
document.body.innerHTML = app.templatizer.body();
</script>
jade
(object, default {}
)
jade
is an object which will be passed directly to jade.compile()
. See the Jade API documentation for what options are available.
Here's an example where we set the Jade compileDebug
option to true
.
templatizer(templatesDir, outputFile, {
jade: {
compileDebug: true
}
});
Mixin Support
Jade has a feature called mixins
which when compiled get treated as function declarations within the compiled function. Templatizer pulls these out of the compiled function and places them on the namespace of the parent function. For example:
// users.jade
ul
each user in users
mixin user(user)
mixin user(user)
// Jade mixin content
Templatizer will compile this as
exports.users = function () {}
exports.users.user = function (user) {}
This is helpful as it allows you to call users()
to create your list and then users.user()
to render just a single item in the list.
CLI
Templatizer comes with a bin script to use from makefiles/package.json scripts/etc, it works like this:
$ templatizer -d path/to/templates -o /path/to/output/templates.js
Tests
Run npm test
to run the tests (you'll need phantomjs installed). You can also run the tests in your browser with npm run browser-test
and going to http://localhost:3003.
Changelog
- v0.2.9 diff - Adding path normalize to avoid issues if passing in paths like
/thing/../otherfolder
.
License
MIT
Contributors
If you think this is cool, you should follow me on twitter: @HenrikJoreteg