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
Last revisionBoth sides next revision
javascript:jade [2014/03/18 00:18] – [Creating an HTML fragment] mithatjavascript:jade [2014/03/18 03:40] – [Creating web pages] mithat
Line 16: Line 16:
 ==== Creating an HTML fragment ==== ==== Creating an HTML fragment ====
  
-=== jade.render ===+=== 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 ''jade.render'' method. 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 ''jade.render'' method.
Line 22: Line 22:
 var jade = require('jade'); var jade = require('jade');
  
-// 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('p Hello, world'); var htmlFrag = jade.render('p Hello, world');
  
Line 31: Line 30:
 output: <code><p>Hello, world</p></code> output: <code><p>Hello, world</p></code>
  
-=== jade.compile ===+=== Compiling strings in Jade into functions ===
  
-You can use the ''jade.compile'' method to create a function that when called will return the fragment.+You can use the ''jade.compile'' method to create a function that when called will return the converted Jade.
 <file javascript compileFragment.js> <file javascript compileFragment.js>
 var jade = require('jade'); var jade = require('jade');
  
-// 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('p Hello, world'); var frag = jade.compile('p Hello, world');
  
Line 51: Line 50:
 === jade.renderFile === === jade.renderFile ===
  
-In a lot of cases, you will want to render code that is stored in a file rather than in a string variable or literal. You can do that with the ''jade.renderFile'' method.+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 ''jade.renderFile'' method.
  
 <file jade myFrag.jade> <file jade myFrag.jade>
Line 61: Line 60:
 var jade = require('jade'); var jade = require('jade');
  
-// Render an HTML fragment from a file +// Render an HTML fragment from a file written in Jade.
-// written in Jade.+
 var htmlFrag = jade.renderFile('myFrag.jade'); var htmlFrag = jade.renderFile('myFrag.jade');
  
Line 79: Line 77:
 }; };
  
-// Render an HTML fragment from a file +// Render an HTML fragment from a file written in Jade.
-// written in Jade.+
 var htmlFrag = jade.renderFile('myFrag.jade', options); var htmlFrag = jade.renderFile('myFrag.jade', options);
  
Line 88: Line 85:
 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 100: Line 117:
 }; };
  
-// Render an HTML fragment from a file +// Render an HTML fragment from a file written in Jade. 
-// written in Jade. +var htmlFrag = jade.renderFile('myFrag.jade', options, function(err, html) { 
-var htmlFrag = jade.renderFile('myFrag.jade', options);+        if (err) { 
 +           throw(err); 
 +        } 
 +        return html; 
 +    });
  
-// Write fragment to a file. +// Write HTML fragment to a file. 
-fs.writeFile('myFrag.html', htmlFrag);+fs.writeFile('myFrag.html', htmlFrag, function(err) { 
 +    if (err) { 
 +        throw(err); 
 +    } 
 +});
 </file> </file>
  
Line 115: Line 140:
 </code> </code>
  
-===== Creating a simple web page ===== 
- 
-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> 
- 
-<file javascript buildMyPage.js> 
-var jade = require('jade'); 
-var fs = require('fs'); 
- 
-options = { 
-    pretty: true 
-}; 
- 
-// Render an HTML page from a file 
-// written in Jade. 
-var html = jade.renderFile('myPage.jade', options); 
- 
-// Write HTML to a file. 
-fs.writeFile('myPage.html', html); 
-</file> 
- 
-output:<code> 
-$ cat myPage.html  
-<!DOCTYPE html> 
-<html> 
-  <head> 
-    <title>A Simple Site</title> 
-  </head> 
-  <body> 
-    <h1>Hello there!</h1> 
-    <p>Nice to meet you.</p> 
-  </body> 
-</html> 
-</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]]. 
  

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki