javascript:jade
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
javascript:jade [2014/03/18 03:30] – [Creating an HTML fragment] mithat | javascript:jade [2014/03/18 03:41] (current) – removed mithat | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Jade ====== | ||
- | |||
- | [[http:// | ||
- | |||
- | ===== Getting started ===== | ||
- | |||
- | ==== Installing Jade ==== | ||
- | 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 the Jade module so it's available system-wide or only for a particular project. To make it available system-wide, | ||
- | |||
- | When I am learning new Node.js modules, I prefer to install them on a per-project basis. To do this, open a terminal in your project directory (or navigate to the directory) and enter: < | ||
- | |||
- | When this is done you should see a '' | ||
- | |||
- | ==== Creating an HTML fragment ==== | ||
- | |||
- | === Rendering strings in Jade to HTML === | ||
- | |||
- | The first thing we are going to do is use Jade to produce an HTML fragment from a string literal. This is done with the '' | ||
- | <file javascript renderFragment.js > | ||
- | var jade = require(' | ||
- | |||
- | // Render an HTML fragment from a string literal written in Jade. | ||
- | var htmlFrag = jade.render(' | ||
- | |||
- | console.log(htmlFrag); | ||
- | </ | ||
- | |||
- | output: < | ||
- | |||
- | === Compiling strings in Jade into functions === | ||
- | |||
- | You can use the '' | ||
- | <file javascript compileFragment.js> | ||
- | var jade = require(' | ||
- | |||
- | // Compile a function that produces an HTML fragment from a string | ||
- | // literal written in Jade. | ||
- | var frag = jade.compile(' | ||
- | |||
- | // Render stuff. | ||
- | var htmlFrag = frag(); | ||
- | |||
- | 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 | ||
- | |||
- | < | ||
- | < | ||
- | </ | ||
- | |||
- | ==== Creating web pages ==== | ||
- | |||
- | In this section, we will use Jade to build a simple but complete web page. It will serve as an introduction to some fundamental Jade language concepts. | ||
- | |||
- | <file jade myPage.jade> | ||
- | doctype html | ||
- | html | ||
- | head | ||
- | title A Simple Site | ||
- | body | ||
- | h1 Hello there! | ||
- | p Nice to meet you. | ||
- | </ | ||
- | |||
- | <file javascript buildMyPage.js> | ||
- | var jade = require(' | ||
- | var fs = require(' | ||
- | |||
- | options = { | ||
- | pretty: true | ||
- | }; | ||
- | |||
- | // Render an HTML page from a file | ||
- | // written in Jade. | ||
- | var html = jade.renderFile(' | ||
- | |||
- | // Write HTML to a file. | ||
- | fs.writeFile(' | ||
- | </ | ||
- | |||
- | output:< | ||
- | $ cat myPage.html | ||
- | < | ||
- | < | ||
- | < | ||
- | < | ||
- | </ | ||
- | < | ||
- | < | ||
- | < | ||
- | </ | ||
- | </ | ||
- | </ | ||
- | |||
- | The basic idea is that Jade uses a shorthand HTML syntax, and indentation is used to nest elements. | ||
- | |||
- | This should be enough to make you comfortable reading the [[http:// | ||
- | |||
- | <WRAP center round info 60%> | ||
- | More later, time permitting. | ||
- | </ | ||
- | |||
javascript/jade.1395113422.txt.gz · Last modified: 2014/03/18 03:30 by mithat