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 |
|
7 | Simple solution for compiling jade templates into vanilla JS functions for blazin' fast client-side use.
|
8 |
|
9 | ## What is this?
|
10 |
|
11 | 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.
|
12 |
|
13 | So, 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?
|
16 | From my tests it's 6 to 10 times faster than mustache.js with ICanHaz.
|
17 |
|
18 | ## How do I use it?
|
19 |
|
20 | 1. `npm install templatizer`
|
21 | 1. Write all your templates as individual jade files in a folder in your project.
|
22 | 1. Somewhere in your build process do this:
|
23 |
|
24 | ```js
|
25 | var templatizer = require('templatizer');
|
26 |
|
27 | // pass in the template directory and what you want to
|
28 | // save the output file as. That's it!
|
29 | templatizer(__dirname + '/templates', __dirname + '/demo_output.js', options);
|
30 | ```
|
31 |
|
32 | So a folder like this
|
33 |
|
34 | ```
|
35 | /clienttemplates
|
36 | user.jade
|
37 | app.jade
|
38 | /myfolder
|
39 | nestedTemplate.jade
|
40 | ```
|
41 |
|
42 | Compiles 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.
|
47 | var jade=function(exports){ ... }
|
48 |
|
49 | // a function built from the `user.jade` file
|
50 | // that takes your data and returns a string.
|
51 | exports.user = function () {}
|
52 |
|
53 | // built from the `app.jade` file
|
54 | exports.app = function () {} // the function
|
55 |
|
56 | // folders become nested objects so
|
57 | // myfolder/nestedTemplate.jade becomes
|
58 | exports.myfolder.nestedTemplate = function () {} // the template function
|
59 |
|
60 | // etc. etc
|
61 | ```
|
62 |
|
63 | The 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 |
|
67 | The 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
|
70 | templatizer(__dirname + '/app/**/*.jade', __dirname + '/templates.js');
|
71 | ```
|
72 |
|
73 | ### Options
|
74 |
|
75 | The third parameter passed to `templatizer` is an options object.
|
76 |
|
77 | #### `namespace` (string, default `window`)
|
78 |
|
79 | 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:
|
80 |
|
81 | ```js
|
82 | templatizer(templatesDir, 'templates.js', {
|
83 | namespace: 'app'
|
84 | });
|
85 | ```
|
86 |
|
87 | ```html
|
88 | <script>var app = {};</script>
|
89 | <script src="templates.js"></script>
|
90 | <script>
|
91 | // Templates will be available on app.templatizer
|
92 | document.body.innerHTML = app.templatizer.body();
|
93 | </script>
|
94 | ```
|
95 |
|
96 | #### `dontRemoveMixins` (boolean, default false)
|
97 |
|
98 | By 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.
|
99 |
|
100 | #### `jade` (object, default `{}`)
|
101 |
|
102 | `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.
|
103 |
|
104 | Here's an example where we set the Jade `compileDebug` option to `true`.
|
105 |
|
106 | ```js
|
107 | templatizer(templatesDir, outputFile, {
|
108 | // Options
|
109 | jade: {
|
110 | compileDebug: true
|
111 | }
|
112 | });
|
113 | ```
|
114 |
|
115 | ### Mixin Support
|
116 |
|
117 | 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:
|
118 |
|
119 | ```jade
|
120 | // users.jade
|
121 | ul
|
122 | each user in users
|
123 | mixin user(user)
|
124 |
|
125 | mixin user(user)
|
126 | // Jade mixin content
|
127 | ```
|
128 |
|
129 | Templatizer will compile this as
|
130 |
|
131 | ```js
|
132 | // Compiled fn from file
|
133 | exports.users = function () {}
|
134 |
|
135 | // Compiled mixin fn
|
136 | exports.users.user = function (user) {}
|
137 | ```
|
138 |
|
139 | 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.
|
140 |
|
141 | ## CLI
|
142 |
|
143 | Templatizer comes with a bin script to use from makefiles/package.json scripts/etc, it works like this:
|
144 |
|
145 | ```
|
146 | $ templatizer -d path/to/templates -o /path/to/output/templates.js
|
147 | ```
|
148 |
|
149 | ## Tests
|
150 |
|
151 | 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](http://localhost:3003).
|
152 |
|
153 | ## Changelog
|
154 |
|
155 | - 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`.
|
156 |
|
157 | ## License
|
158 |
|
159 | MIT
|
160 |
|
161 | ## Contributors
|
162 |
|
163 | - Aaron McCall [github profile](https://github.com/aaronmccall)
|
164 | - Luke Karrys [github profile](https://github.com/lukekarrys)
|
165 |
|
166 | If you think this is cool, you should follow me on twitter: [@HenrikJoreteg](http://twitter.com/henrikjoreteg)
|