我遇到一些麻烦,包括我的index.html中的html片段。 我试图遵循,ejs文档,但我似乎无法使它工作。
目录结构:
project
-public
--assets
---css
---images
---js
--Index
---index.html + index.css and index.js
--someOtherPageFolder
-views
--partials
---partials1.ejs
--index.ejs
--layout.ejs
-server.js
这是我的server.js( NEW ):
var express = require("express");
var partials = require("express-partials");
var http = require('http');
var path = require('path');
var app = express();
app.get("/", function(req, res) {
res.redirect("index");
});
app.configure(function(){
app.set('port', process.env.PORT || 8888);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(partials());
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(express.bodyParser());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
http.createServer(app).listen(app.get('port'), function(){
console.log('App listening on port ' + app.get('port'));
});
这是我的test.ejs:
<h1>This is test!</h1>
这就是我想要html snipp的地方:
<div id="sb-site">
<div class="">
<p>Not test</p>
<%- include test.html %>
</div>
</div>
我做错了什么? 还有一种方法可以让我这样做并使用.html代替.ejs(我使用的是eclipse而且它不支持.ejs文件)
答案 0 :(得分:3)
包括在内,您应该这样做:
<%- include ("./partials/footer") %>
答案 1 :(得分:2)
Express 3打破ejs partials,改为使用express-partials
。
// Include it like so with your other modules
var express = require("express");
var partials = require('express-partials');
var server = express();
// Finally add it into your server conviguration
server.configure(function(){
server.set('view engine', 'ejs');
// Include partials middleware into the server
server.use(partials());
});
在.ejs
次观看中,享受这样的奢侈......
<%- include ../partials/header %>
<section id="welcome">
Welcome
</section>
<%- include ../partials/footer %>
此外,不要将ejs模块设置为读取.html
,而只需按照此答案将eclipse渲染为.ejs
.html
。 how do i get eclipse to interpret .ejs files as .html?
例如,这是基本快速结构的设置方式......
project
--public
--views
----layout.ejs
----index.ejs
----partials
------partial1.ejs
--server.js
server.js看起来像......
var express = require('express'),
partials = require('express-partials'),
http = require('http'),
path = require('path');
var app = express();
// all environments
app.configure(function() {
app.set('port', process.env.PORT || 3838);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
// Middleware
app.use(partials());
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
http.createServer(app).listen(app.get('port'), function(){
console.log('App listening on port ' + app.get('port'));
});
您的路线看起来像......
server.get("/", function(req, res) {
res.render("index");
});
最后,你的layout.ejs ......
<html>
<head>
<title></title>
</head>
<body>
<%- body %>
</body>
</html>
和index.ejs ...
<div id="index">
<%- include partial1.ejs %>
</div>
如果您需要对工作示例的引用,here it is