ใหม่มากสำหรับการใช้ข้างต้นและจนถึงตอนนี้ดีมาก ฉันแค่ทำไซต์ทดสอบง่ายๆ เพื่อทำความเข้าใจสิ่งต่างๆ
มีเทมเพลตเดียวและสองหน้า (ดัชนีและเกี่ยวกับ) สิ่งที่ฉันไม่สามารถเข้าใจได้และฉันได้อ่านเว็บไซต์ต่างๆ เกี่ยวกับเรื่องนี้แล้ว คือฉันจะมีเนื้อหาที่แตกต่างกันสำหรับสองหน้าโดยใช้เทมเพลตเดียวได้อย่างไร ฉันอาจจะไม่ได้มีอะไรถูกหรือทำอะไรผิดไปเสียหมด ดังนั้นถ้าใครสามารถชี้ทิศทางที่ถูกต้องให้ฉันหรือยกตัวอย่างการทำงานที่ดีได้ มันจะช่วยฉันได้ไม่สิ้นสุด
เทมเพลต
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='/th_templates/bootstrap336/css/bootstrap.css')
link(rel='stylesheet', href='/th_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')
เว็บเซิร์ฟเวอร์พื้นฐาน
//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');
})
ไฟล์เส้นทางของฉัน 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'
});
});
}
extends layout.pug
เพื่อขยายlayout.pug คุณต้องมี about.pug ซึ่งขยายเลย์เอาต์ด้วย จากนั้นใช้res.render('index...
และres.render('about...
- person Molda   schedule 21.09.2016