2015年7月4日 星期六

node js / relation routes views render jade


http://expressjs.com/guide/using-template-engines.html

to add new page into the web, as long as adding file on routes/ , views/ and app.js

routes/peter.js
var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('peter', { title: 'Peter' ,content:'hihi'});
});

module.exports = router;

view/peter.jade
extends layout

block content
  h1= title
  h2= content

  p Welcome to #{title}


app.js

var peter = require('./routes/peter');

app.use('/peter', peter);


Once the view engine is set, you don’t have to explicitly specify the engine or load the template engine module in your app, Express loads it internally as shown below, for the example above.
app.set('view engine', 'jade');
Create a Jade template file named “index.jade” in the views directory, with the following content.
html
  head
    title!= title
  body
    h1!= message
Then create a route to render the “index.jade” file. If the view engine property is not set, you will have to specify the extension of the view file, else you can omit it.
app.get('/', function (req, res) {
  res.render('index', { title: 'Hey', message: 'Hello there!'});
});







沒有留言:

張貼留言