javascript:jade
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
javascript:jade [2014/03/17 22:48] – mithat | javascript:jade [2014/03/18 03:40] – [Creating web pages] mithat | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Jade ====== | ====== Jade ====== | ||
- | [[http:// | + | [[http:// |
===== Getting started ===== | ===== Getting started ===== | ||
==== Installing Jade ==== | ==== Installing Jade ==== | ||
- | To use Jade, you must first have Node.js installed on your system. On Windows and MacOS, you can use an [[http:// | + | To use Jade, you must first have Node.js installed on your system. On Windows and MacOS, you can use an [[http:// |
- | Once you have Node.js installed, you have the choice of installing | + | Once you have Node.js installed, you have the choice of installing |
- | Until I get a good grasp of using Jade, I want to install | + | When I am learning new Node.js modules, I prefer |
- | When this is done you should see a '' | + | When this is done you should see a '' |
==== Creating an HTML fragment ==== | ==== Creating an HTML fragment ==== | ||
- | The first thing we are going to do is use Jade to produce an HTML fragment. This is done with the '' | + | === Rendering strings in Jade to HTML === |
- | <code javascript> | + | |
+ | The first thing we are going to do is use Jade to produce an HTML fragment | ||
+ | <file javascript | ||
var jade = require(' | var jade = require(' | ||
- | // Render an HTML fragment from a string literal | + | // Render an HTML fragment from a string literal written in Jade. |
- | // written in Jade. | + | |
var htmlFrag = jade.render(' | var htmlFrag = jade.render(' | ||
console.log(htmlFrag); | console.log(htmlFrag); | ||
- | </code> | + | </file> |
- | You can also create function that when called will return the fragment with the '' | + | output: < |
- | <code javascript> | + | |
+ | === Compiling strings in Jade into functions === | ||
+ | |||
+ | You can use the '' | ||
+ | <file javascript | ||
var jade = require(' | var jade = require(' | ||
- | // Compile a function that produces an HTML fragment | + | // Compile a function that produces an HTML fragment from a string |
- | // from a string literal written in Jade. | + | // literal written in Jade. |
var frag = jade.compile(' | var frag = jade.compile(' | ||
Line 39: | Line 44: | ||
console.log(htmlFrag); | console.log(htmlFrag); | ||
+ | </ | ||
+ | |||
+ | output: < | ||
+ | |||
+ | === jade.renderFile === | ||
+ | |||
+ | In a lot of cases, you will want to render code that is stored in a file rather than in a string literal or variable. You can do that with the '' | ||
+ | |||
+ | <file jade myFrag.jade> | ||
+ | h1 Hello, there! | ||
+ | p Nice to meet you. | ||
+ | </ | ||
+ | |||
+ | <file javascript renderFragmentFile_1.js> | ||
+ | var jade = require(' | ||
+ | |||
+ | // Render an HTML fragment from a file written in Jade. | ||
+ | var htmlFrag = jade.renderFile(' | ||
+ | |||
+ | console.log(htmlFrag); | ||
+ | </ | ||
+ | |||
+ | output: < | ||
+ | |||
+ | The above produces valid HTML, but it's hard to read. To make the output human-readable, | ||
+ | |||
+ | <file javascript renderFragmentFile_2.js> | ||
+ | var jade = require(' | ||
+ | |||
+ | options = { | ||
+ | pretty: true | ||
+ | }; | ||
+ | |||
+ | // Render an HTML fragment from a file written in Jade. | ||
+ | var htmlFrag = jade.renderFile(' | ||
+ | |||
+ | console.log(htmlFrag); | ||
+ | </ | ||
+ | |||
+ | output: < | ||
+ | < | ||
+ | |||
+ | You can (and should) also take anvantage of the callback that you can pass to '' | ||
+ | <file javascript renderFragmentFile_3.js> | ||
+ | var jade = require(' | ||
+ | |||
+ | options = { | ||
+ | pretty: true | ||
+ | }; | ||
+ | |||
+ | // Render an HTML fragment from a file written in Jade. | ||
+ | // Use callback to trap errors. | ||
+ | var htmlFrag = jade.renderFile(' | ||
+ | if (err) { | ||
+ | | ||
+ | } | ||
+ | return html; | ||
+ | }); | ||
+ | | ||
+ | console.log(htmlFrag); | ||
+ | </ | ||
+ | |||
+ | === Writing output to file === | ||
+ | To write the output created to a file, you'll need to use Node.js' | ||
+ | |||
+ | <file javascript convertFragment.js> | ||
+ | var jade = require(' | ||
+ | var fs = require(' | ||
+ | |||
+ | options = { | ||
+ | pretty: true | ||
+ | }; | ||
+ | |||
+ | // Render an HTML fragment from a file written in Jade. | ||
+ | var htmlFrag = jade.renderFile(' | ||
+ | if (err) { | ||
+ | | ||
+ | } | ||
+ | return html; | ||
+ | }); | ||
+ | |||
+ | // Write HTML fragment to a file. | ||
+ | fs.writeFile(' | ||
+ | if (err) { | ||
+ | throw(err); | ||
+ | } | ||
+ | }); | ||
+ | </ | ||
+ | |||
+ | output:< | ||
+ | $ cat myFrag.html | ||
+ | |||
+ | < | ||
+ | < | ||
</ | </ | ||
+ | |||