0
非常非常新的使用上述和迄今如此好。我只是在做一個簡單的測試網站來讓我的頭腦發生變化。Node.js,帕格模板
得到了一個單一的模板和兩個頁面(索引&左右)。我無法弄清楚,我已閱讀過有關此問題的各種網站,我是如何使用單個模板爲這兩個頁面提供不同內容的。我可能沒有找到正確的東西或做錯事情,所以如果有人能指出我的方向正確或提供良好的工作實例。它會幫助我無盡的結局。
模板
doctype html
html(lang="en")
head
title= metaTitle
meta(charset='utf-8')
meta(http-equiv="X-UA-Compatible", content="IE=edge")
meta(name="viewport", content="width=device-width, initial-scale=1")
link(rel='stylesheet', href='http://fonts.googleapis.com/css?family=Open+Sans')
link(rel='stylesheet', href='_templates/bootstrap336/css/bootstrap.css')
link(rel='stylesheet', href='_templates/css/generic.css')
body
.container
header
#header
h1 Node.js
nav.navbar.navbar-default
include shared/nav.pug
section
h3 #{pageHeading}
<!-- Want my content here -->
p
img(src='/_templates/images/reg_icon.png')
footer
.row
.col-xs-12.col-sm-6
Copyright © 2016
.col-xs-12.col-sm-6.text-right
Privacy
script(src='_includes/jquery/jquery-2.2.3.min.js')
script(src='_includes/jquery/jquery-ui-1114/jquery-ui.js')
script(src='_templates/bootstrap336/js/bootstrap.min.js')
基本的web服務器
//Basic webserver
var express = require('express'),
app = express();
require('./routes')(app);
module.exports=app;
//config
app.set('view engine','pug');
app.set('views',__dirname + '/public/_templates');
//standard
app.use(express.static(__dirname + '/public'));
//Starts and listens
var port = process.env.PORT || 3000;
app.listen(port, function() {
console.log("Listening on " + port+" | In folder " + __dirname + '\\public');
})
我routes.js文件
module.exports = function(app){
var coretitle="Node.js :: Test";
app.get('/', function (req, res) {
res.render('index', {
metaTitle : coretitle,
pageHeading : 'First attempt'
});
});
app.get('/about', function (req, res) {
res.render('index', {
metaTitle : coretitle,
pageHeading : 'All About This'
});
});
}
在你的index.pug中看到這個https://pugjs.org/language/extends.html你需要使用'extends layout.pug'來擴展layout.pug,你也需要about.pug,擴展布局。然後使用'res.render('index ...'和'res.render('about ...' – Molda
)非常感謝@Molda,對它進行排序,只是與我平常做事情的方式有所不同。 – Martin