UNPKG

4.25 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 2k 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
65Currently the only available option is `jade`, which 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.
66
67Here's an example where we set the Jade `compileDebug` option to `true`.
68
69```js
70templatizer(templatesDir, outputFile, {
71 // Options
72 jade: {
73 compileDebug: true
74 }
75});
76```
77
78### Mixin Support
79
80Jade 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:
81
82```jade
83// users.jade
84ul
85 each user in users
86 mixin user(user)
87
88mixin user(user)
89 // Jade mixin content
90```
91
92Templatizer will compile this as
93
94```js
95// Compiled fn from file
96exports.users = function () {}
97
98// Compiled mixin fn
99exports.users.user = function (user) {}
100```
101
102This 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.
103
104## CLI
105
106Templatizer comes with a bin script to use from makefiles/package.json scripts/etc, it works like this:
107
108```
109$ templatizer -d path/to/templates -o /path/to/output/templates.js
110```
111
112## Sample?
113
114Check out the `tests/demo_output.js` file for... err... demo output built from the `templates` directory in this project.
115
116## Changelog
117
118- 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`.
119
120## License
121
122MIT
123
124## Contributors
125
126- Aaron McCall [github profile](https://github.com/aaronmccall)
127- Luke Karrys [github profile](https://github.com/lukekarrys)
128
129If you think this is cool, you should follow me on twitter: [@HenrikJoreteg](http://twitter.com/henrikjoreteg)