UNPKG

7.3 kBMarkdownView Raw
1# templatizer.js
2
3[![Build Status](https://travis-ci.org/HenrikJoreteg/templatizer.png?branch=master)](https://travis-ci.org/HenrikJoreteg/templatizer)
4
5[![NPM](https://nodei.co/npm/templatizer.png?downloads=true)](https://nodei.co/npm/templatizer/)
6
7Simple solution for compiling jade templates into vanilla JS functions for blazin' fast client-side use.
8
9## What is this?
10
11Client-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.
12
13So, 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.
14
15## Is it faster?
16From my tests it's 6 to 10 times faster than mustache.js with ICanHaz.
17
18## How do I use it?
19
201. `npm install templatizer`
211. Write all your templates as individual jade files in a folder in your project.
221. Somewhere in your build process do this:
23
24```js
25var templatizer = require('templatizer');
26
27// pass in the template directory and what you want to
28// save the output file as. That's it!
29templatizer(__dirname + '/templates', __dirname + '/demo_output.js', options);
30```
31
32So a folder like this
33
34```
35/clienttemplates
36 user.jade
37 app.jade
38 /myfolder
39 nestedTemplate.jade
40```
41
42Compiles down to a JS file that looks something like this:
43
44```js
45// here's about 1.6k worth of utils that jade uses to DRY up the template code a bit.
46// Includes some basic shims for Object.keys, etc.
47var jade=function(exports){ ... }
48
49// a function built from the `user.jade` file
50// that takes your data and returns a string.
51exports.user = function () {}
52
53// built from the `app.jade` file
54exports.app = function () {} // the function
55
56// folders become nested objects so
57// myfolder/nestedTemplate.jade becomes
58exports.myfolder.nestedTemplate = function () {} // the template function
59
60// etc. etc
61```
62
63The awesome thing is... there are no external dependencies because they're just functions at this point. Crazy fast, SO MUCH WIN!!!!
64
65### Glob Paths
66
67The directory path can also be a [glob](https://github.com/isaacs/node-glob) instead that can be used to match `*.jade` files across multiple directories. For example:
68
69```js
70templatizer(__dirname + '/app/**/*.jade', __dirname + '/templates.js');
71```
72
73### Options
74
75The third parameter passed to `templatizer` is an options object.
76
77#### `namespace` (object, optional)
78
79If you are using templatizer as a global in the browser (without requirejs, browserify, or something similar) by default your templates will be available at `window.templatizer`. Using `namespace` you can attach it to a different global object or rename the property it attaches to.
80
81#### `namespace.parent` (string, default `window`)
82
83This is the name of the object where you want to attach your templates.
84
85#### `namespace.defineParent` (boolean, default `false`)
86
87If this option is `true` and `namespace.parent` does not exist, it will be created. By default if `namespace.parent` does not exist, templatizer will throw an error like this: `templatizer: window.app does not exist or is not an object`.
88
89#### `namespace.name` (string, default `templatizer`)
90
91This is the name of the property on `namespace.parent` where your templates will be attached.
92
93#### Shorthand
94
95If all you want is to attach the `templatizer` object to an already created global variable, then you can just make `namespace` the name of the object where it will attach:
96
97
98```js
99templatizer(templatesDir, 'templates.js', {
100 namespace: 'app'
101});
102```
103
104```html
105<script>var app = {};</script>
106<script src="templates.js"></script>
107<script>
108 // Templates will be available on app.templatizer
109 document.body.innerHTML = app.templatizer.body();
110</script>
111```
112
113#### `dontRemoveMixins` (boolean, default false)
114
115By default `jade` will not compile any mixins which aren't being called from the file they were created in. This is usually a very good thing, since keeps file sizes down. But in some cases (especially when using the [mixin support](#mixin-support) functionality), you may want to create mixins and call them from other places in your code or other files. Setting this option to `true` will keep all mixins in the compiled source.
116
117#### `inlineJadeRuntime` (boolean, default true)
118
119By default the jade runtime will be included into the generated template javascript file. In order minimize the file size you can set this parameter to false. Instead a `jade` module is expected as amdDependency parameter. Otherwise an error will be thrown.
120
121#### `amdDependencies` (array, default [])
122
123An array of AMD module dependencies you want to pass in to the generated templates javascript file.
124
125#### `jade` (object, default `{}`)
126
127`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.
128
129Here's an example where we set the Jade `compileDebug` option to `true`.
130
131```js
132templatizer(templatesDir, outputFile, {
133 // Options
134 jade: {
135 compileDebug: true
136 }
137});
138```
139
140#### `globOptions` (object, default `{}`)
141
142`globOptions` will be passed directly to `node-glob`. See the [API docs](https://github.com/isaacs/node-glob#options) for available options.
143
144### Mixin Support
145
146Jade 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:
147
148```jade
149// users.jade
150ul
151 each user in users
152 mixin user(user)
153
154mixin user(user)
155 // Jade mixin content
156```
157
158Templatizer will compile this as
159
160```js
161// Compiled fn from file
162exports.users = function () {}
163
164// Compiled mixin fn
165exports.users.user = function (user) {}
166```
167
168This 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.
169
170## CLI
171
172Templatizer comes with a bin script to use from makefiles/package.json scripts/etc, it works like this:
173
174```
175$ templatizer -d path/to/templates -o /path/to/output/templates.js
176```
177
178## Tests
179
180Run `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).
181
182## Changelog
183
184- 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`.
185
186## License
187
188MIT
189
190## Contributors
191
192- Aaron McCall [github profile](https://github.com/aaronmccall)
193- Luke Karrys [github profile](https://github.com/lukekarrys)
194
195If you think this is cool, you should follow me on twitter: [@HenrikJoreteg](http://twitter.com/henrikjoreteg)