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 revisionBoth sides next revision
javascript:jade [2014/03/18 03:16] – [Creating Web pages] mithatjavascript:jade [2014/03/18 03:30] – [Creating an HTML fragment] 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 97: Line 94:
 }; };
  
-// Render an HTML fragment from a file +// Render an HTML fragment from a file written in Jade.
-// written in Jade.+
 // Use callback to trap errors. // Use callback to trap errors.
 var htmlFrag = jade.renderFile('myFrag.jade', options, function (err, html) { var htmlFrag = jade.renderFile('myFrag.jade', options, function (err, html) {
Line 121: 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>
  

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki