Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
949 views
in Technique[技术] by (71.8m points)

node.js - Ejs engine with html not working

i'm using html files instead of ejs, but the express engine is ejs

views
|
|--header.html
|--footer.html
|
|--index.html

I configured like

app.set('views', __dirname + '/views');
app.engine('html', require('ejs').renderFile);  

I render my index template by this:

res.render('index.html', {title: 'test'});

But how can i include header and footer.html in index.html

Similar posts Node.js express: confuse about ejs template

Existing example which is not working https://github.com/visionmedia/express/tree/master/examples/ejs

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The original method for what you asked would be to use partials. Partials have since been removed, and replaced with the include function of EJS. This is how you would include a file:

<% include header.html %>
<% include footer.html %>

Any locals you pass to the rendered page will also be passed to an include. For example:

app.js

app.get('/', function(req, res) {
  res.render(__dirname + '/index.html', {
    string: 'random_value',
    other: 'value'
  });
});

index.html

<!DOCTYPE html>
<body>
  <%= other %>
  <% include content.html %>
</body>

content.html

<pre><%= string %></pre>

The resultant HTML you would get is:

<!DOCTYPE html>
<body>
  value
  <pre>random_value</pre>
</body>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...