UNPKG

4.88 kBMarkdownView Raw
1# templatizer.js
2
3Simple solution for compiling jade templates into vanilla JS functions for blazin' fast client-side use.
4
5## What is this?
6
7Client-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.
8
9So, the question is, what's a sane way to get to that point? Enter [jade](http://jade-lang.com). 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.
10
11## Is it faster?
12From my tests it's 6 to 10 times faster than mustache.js with ICanHaz.
13
14## How do I use it?
15
161. `npm install templatizer`
171. Write all your templates as individual jade files in a folder in your project.
181. Somewhere in your build process do this:
19
20```js
21var templatizer = require('templatizer');
22
23// pass in the template directory and what you want to
24// save the output file as. That's it!
25templatizer(__dirname + '/templates', __dirname + '/demo_output.js', options);
26```
27
28So a folder like this
29
30```
31/clienttemplates
32 user.jade
33 app.jade
34 /myfolder
35 nestedTemplate.jade
36```
37
38Compiles down to a JS file that looks something like this:
39
40```js
41// here's about 1.6k worth of utils that jade uses to DRY up the template code a bit.
42// Includes some basic shims for Object.keys, etc.
43var jade=function(exports){ ... }
44
45// a function built from the `user.jade` file
46// that takes your data and returns a string.
47exports.user = function () {}
48
49// built from the `app.jade` file
50exports.app = function () {} // the function
51
52// folders become nested objects so
53// myfolder/nestedTemplate.jade becomes
54exports.myfolder.nestedTemplate = function () {} // the template function
55
56// etc. etc
57```
58
59The awesome thing is... there are no external dependencies because they're just functions at this point. Crazy fast, SO MUCH WIN!!!!
60
61### Options
62
63The third parameter passed to `templatizer` is an options object.
64
65#### `namespace` (string, default `window`)
66
67If 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:
68
69```js
70templatizer(templatesDir, 'templates.js', {
71 namespace: 'app'
72});
73```
74
75```html
76<script>var app = {};</script>
77<script src="templates.js"></script>
78<script>
79 // Templates will be available on app.templatizer
80 document.body.innerHTML = app.templatizer.body();
81</script>
82```
83
84#### `jade` (object, default `{}`)
85
86`jade` is an object which will be passed directly to `jade.compile()`. See the [Jade API documentation](http://jade-lang.com/api/) for what options are available.
87
88Here's an example where we set the Jade `compileDebug` option to `true`.
89
90```js
91templatizer(templatesDir, outputFile, {
92 // Options
93 jade: {
94 compileDebug: true
95 }
96});
97```
98
99### Mixin Support
100
101Jade 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:
102
103```jade
104// users.jade
105ul
106 each user in users
107 mixin user(user)
108
109mixin user(user)
110 // Jade mixin content
111```
112
113Templatizer will compile this as
114
115```js
116// Compiled fn from file
117exports.users = function () {}
118
119// Compiled mixin fn
120exports.users.user = function (user) {}
121```
122
123This 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.
124
125## CLI
126
127Templatizer comes with a bin script to use from makefiles/package.json scripts/etc, it works like this:
128
129```
130$ templatizer -d path/to/templates -o /path/to/output/templates.js
131```
132
133## Tests
134
135Run `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](http://localhost:3003).
136
137## Changelog
138
139- v0.2.9 [diff](https://github.com/henrikjoreteg/templatizer/compare/v0.2.8...v0.2.9) - Adding path normalize to avoid issues if passing in paths like `/thing/../otherfolder`.
140
141## License
142
143MIT
144
145## Contributors
146
147- Aaron McCall [github profile](https://github.com/aaronmccall)
148- Luke Karrys [github profile](https://github.com/lukekarrys)
149
150If you think this is cool, you should follow me on twitter: [@HenrikJoreteg](http://twitter.com/henrikjoreteg)