Code
I want to make my webpage modular so that I don´t need to write my stuff again and again. I thought my solution would be the ejs lib. So I´ve using express and ejs that is configured like this:
const app = express();
app.engine('.html', require('ejs').renderFile);
app.set('view engine', 'html');
app.set('views', __dirname + '/wwwroot/views');
My views folder struct looks like that:
wwwroot
static
css
js
views
login
index.html
profile
dashboard.html
templates
inc_header.html
inc_footer.html
My Dashboard has the following content
<% include templates/inc_header.html %>
This is my dashboard
Problem
The Header file will not be included. I´ve tried wwwroot/views/templates/header.html and header.html. Nothing works.
My Whole Serverconfiguration
app.engine('.html', require('ejs').renderFile);
app.set('view engine', 'html');
app.set('views', __dirname + '/wwwroot/views');
app.use(session({
secret: program.secret || "secret",
resave: true,
saveUninitialized: true
}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }))
app.use('/', express.static(path.join(__dirname, '/wwwroot/static/')));
Output on the Webserver
<% include templates/inc_header.html %> This is my dashboard
Looks like the file will not be rendered?
Code
I want to make my webpage modular so that I don´t need to write my stuff again and again. I thought my solution would be the ejs lib. So I´ve using express and ejs that is configured like this:
const app = express();
app.engine('.html', require('ejs').renderFile);
app.set('view engine', 'html');
app.set('views', __dirname + '/wwwroot/views');
My views folder struct looks like that:
wwwroot
static
css
js
views
login
index.html
profile
dashboard.html
templates
inc_header.html
inc_footer.html
My Dashboard has the following content
<% include templates/inc_header.html %>
This is my dashboard
Problem
The Header file will not be included. I´ve tried wwwroot/views/templates/header.html and header.html. Nothing works.
My Whole Serverconfiguration
app.engine('.html', require('ejs').renderFile);
app.set('view engine', 'html');
app.set('views', __dirname + '/wwwroot/views');
app.use(session({
secret: program.secret || "secret",
resave: true,
saveUninitialized: true
}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }))
app.use('/', express.static(path.join(__dirname, '/wwwroot/static/')));
Output on the Webserver
<% include templates/inc_header.html %> This is my dashboard
Looks like the file will not be rendered?
Share Improve this question edited Jan 24, 2018 at 11:52 Lukas Gund asked Jan 24, 2018 at 11:15 Lukas GundLukas Gund 7112 gold badges10 silver badges30 bronze badges 2-
You don't have a file called
header.html
at all! – Quentin Commented Jan 24, 2018 at 11:17 - I used inc_header.html. I edited it.... – Lukas Gund Commented Jan 24, 2018 at 11:18
2 Answers
Reset to default 1This is from ejs docs:
Includes are relative to the template with the include call.
Since the file from where you include headers is dashboard.html
then the path should be:
<% include ../templates/inc_header %>
<%- include templates/inc_header.html %>
include template tags of ejs has a hyphen attached to the opening tag
add the hyphen to the include tag and it will work.
you can also send data to the file being included
<%- include("../partials/head.ejs", {variant:e.name}); %>
but NB: if a variable is to be used in it, it has to be passed to it from anywhere it is being included.
to solve this issue you can set a default value to it if not sent Add this to the file being included
<em>Variant: <%= typeof variant != 'undefined' ? variant : 'default' %></em>
for more check out this link
for using html files in express using ejs, use this in the index.js(entry point for tour application)
const app = express();
app.engine('.html', require('ejs').renderFile);
app.set('view engine', 'html');
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744974176a4604043.html
评论列表(0条)