您可以在您选择的模板引擎中使用布局,并且每个视图都可以扩展该布局。例如,如果您使用Jade作为模板引擎。
index.js
var express = require("express");
var app = express();
var port = process.env.PORT || 7080;
app.set('view engine', 'jade');
app.get('/', function (req, res) {
res.render('home');
});
app.listen(3000);
的意见/ layout.jade
doctype html
html
head
script(src='/javascripts/home.js')
link(rel='stylesheet', href='/stylesheets/style.css')
block title
title= "My Website"
body
.container
block content
的意见/ home.jade
extends ./layout.jade
block content
h1 Hello World!
家。玉视图扩大了布局并覆盖了content
区块。参观http://localhost:3000/
返回如下:
<!DOCTYPE html>
<html>
<head>
<script src="/javascripts/home.js"></script>
<link rel="stylesheet" href="/stylesheets/style.css">
<title>My Website</title>
</head>
<body>
<div class="container"><h1>Hello World!</h1></div>
</body>
</html>
为什么你不包括它在你的index.html? – Demnogonis