Compiling Jade
Now that you have Jade installed, you can use the jade
command to compile Jade files. For example, if we put some Jade in a file:
$ echo "h1 Some Jade" > file.jade
Then we can use the jade
command to render that file.
$ jade file.jade rendered file.html
This will create file.html
, as shown:
$ cat file.html <h1>Some Jade</h1>
By default, jade
compiles and renders the file, but if we only want it to compile into JS, we can use the --client
argument, as shown:
$ jade --client file.jade rendered file.js $ cat file.js function anonymous(locals) { jade.debug = [{ lineno: 1, filename: "file.jade" }]; try { var buf = []; jade.debug.unshift({ lineno: 1, filename: jade.debug[0].filename }); jade.debug.unshift({ lineno: 1, filename: jade.debug[0].filename }); buf.push("<h1>"); jade.debug.unshift({ lineno: undefined, filename: jade.debug[0].filename }); jade.debug.unshift({ lineno: 1, filename: jade.debug[0].filename }); buf.push("Some Jade"); jade.debug.shift(); jade.debug.shift(); buf.push("</h1>"); jade.debug.shift(); jade.debug.shift();;return buf.join(""); } catch (err) { jade.rethrow(err, jade.debug[0].filename, jade.debug[0].lineno,"h1 Some Jade\n"); } }
This results in some very ugly JS, mostly due to the debugging information. We can remove that debugging information with the --no-debug
argument.
$ jade --client --no-debug file.jade rendered file.js $ cat file.js function anonymous(locals) { var buf = []; buf.push("<h1>Some Jade</h1>");;return buf.join(""); }
The JS resulting from that could still be optimized a little bit more (and likely will be in future versions of the compiler), but because it's just machine-generated JS, it's not a huge issue. The important part is that this JS can be executed on the client side to render templates dynamically. This will be covered more in Chapter 4, Logic in Templates.
Tip
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.