【发布时间】:2016-09-21 14:54:53
【问题描述】:
使用上述方法非常新,到目前为止一切都很好。我只是在做一个简单的测试站点来了解情况。
有一个模板和两个页面(索引和关于)。我无法弄清楚并且我已经阅读了有关此事的各种网站,是如何使用单个模板为两个页面提供不同的内容。我可能没有做对或做错事,所以如果有人能指出我正确的方向或提供好的工作示例。它会帮助我无穷无尽。
模板
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')
基本网络服务器
//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 中看到这个pugjs.org/language/extends.html 你需要使用
extends layout.pug来扩展 layout.pug,你还需要 about.pug 来扩展布局。然后使用res.render('index...和res.render('about... -
非常感谢@Molda,整理好了,只是想与我平时做事的方式不同。
标签: javascript node.js templates pug