User Tools

Site Tools


javascript:jade

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
javascript:jade [2014/03/18 00:02] – [Installing Jade] mithatjavascript:jade [2014/03/18 03:16] – [Creating Web pages] mithat
Line 8: Line 8:
 To use Jade, you must first have Node.js installed on your system. On Windows and MacOS, you can use an [[http://nodejs.org/download/|installer]]; on Linux, you should be able to [[https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager|use your package manager]]((See http://lovingthepenguin.blogspot.com/2013/10/node-no-such-file-or-directory.html for a potential gotcha on Debian-based systems.)). To use Jade, you must first have Node.js installed on your system. On Windows and MacOS, you can use an [[http://nodejs.org/download/|installer]]; on Linux, you should be able to [[https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager|use your package manager]]((See http://lovingthepenguin.blogspot.com/2013/10/node-no-such-file-or-directory.html for a potential gotcha on Debian-based systems.)).
  
-Once you have Node.js installed, you have the choice of installing it so it's available system-wide or only for a particular project. To make it available system-wide, open a terminal and enter: <code>npm install jade -g</code>+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, open a terminal and enter: <code>npm install jade -g</code>
  
 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: <code>npm install jade</code> 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: <code>npm install jade</code>
Line 33: Line 33:
 === jade.compile === === jade.compile ===
  
-You can also create function that when called will return the fragment with the ''jade.compile'' method.+You can use the ''jade.compile'' method to create a function that when called will return the fragment.
 <file javascript compileFragment.js> <file javascript compileFragment.js>
 var jade = require('jade'); var jade = require('jade');
Line 70: Line 70:
 output: <code><h1>Hello, there!</h1><p>Nice to meet you.</p></code> output: <code><h1>Hello, there!</h1><p>Nice to meet you.</p></code>
  
-The above produces output that is valid HTML but hard to read. To make the output human-readable, we can pass ''jade.renderFile'' an options object with its ''pretty'' property set to true. (The options argument can be provided to the ''jade.render'' and ''jade.compile'' methods as well.)+The above produces valid HTMLbut it'hard to read. To make the output human-readable, we can pass ''jade.renderFile'' an options object with its ''pretty'' property set to true. (The options argument can be provided to the ''jade.render'' and ''jade.compile'' methods as well.)
  
 <file javascript renderFragmentFile_2.js> <file javascript renderFragmentFile_2.js>
Line 88: Line 88:
 output: <code><h1>Hello, there!</h1> output: <code><h1>Hello, there!</h1>
 <p>Nice to meet you.</p></code> <p>Nice to meet you.</p></code>
 +
 +You can (and should) also take anvantage of the callback that you can pass to ''jade.renderFile'' to handle errors:
 +<file javascript renderFragmentFile_3.js>
 +var jade = require('jade');
 +
 +options = {
 +    pretty: true
 +};
 +
 +// Render an HTML fragment from a file
 +// written in Jade.
 +// Use callback to trap errors.
 +var htmlFrag = jade.renderFile('myFrag.jade', options, function (err, html) {
 +        if (err) {
 +           throw(err);
 +        }
 +        return html;
 +    });
 +    
 +console.log(htmlFrag);
 +</file>
  
 === Writing output to file === === Writing output to file ===
Line 115: Line 136:
 </code> </code>
  
-===== Creating a simple web page =====+==== 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. 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.
Line 158: Line 179:
 </html> </html>
 </code> </code>
 +
 +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://jade-lang.com/reference/|Jade Language Reference]], after which you should be able to compose entire web pages in Jade.
 +
 +<WRAP center round info 60%>
 +More later, time permitting.
 +</WRAP>
 +
  

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki