Template engine Jade

Templates allow you to split your presentation information out from your program code,making it easier to arrange your project files and render out web pages with complicated structure. I am using jade template for this.
To install the Jade templating engine, use the NPM command :
npm install jade

   var express = require('express');
   var app = express();

     app.set('view engine', 'jade');
     app.set('view options', { layout: true });
     app.set('views', __dirname + '/views');

   app.get('/books/:name?', function(req, res, next) {
    var name = req.params.name;

    switch ( name ? name.toLowerCase() : '' ) {
     case 'node':
     case 'mongo':
     case 'javascript':
     res.render('books', {book: name});
      break;
    default:
    next();
   }
 });

    app.get('/books/*?', function(req, res){
     res.render('books', {book: null});
   });

    app.get('/?', function(req, res){
     res.render('index');
   });

var port = 3000;
app.listen(port);
console.log('Listening on port ' + port);

— views, the directory where the template files are located. Eg: app.set(‘views’, ‘./views’)
— view engine, the template engine to use. Eg: app.set(‘view engine’, ‘jade’)

The render command in Express takes care of loading any of the required template modules.

More Info : Templating Engine Jade.
Advanced

About

My Name is Dileep Singh, Noder, Javascript Lover & NoSql Developer, Fitness Freak, Love to travel new places, learning photography & Music Manic ♯ ♩ ♬

Posted in Express, Node.Js Tagged with: ,