Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...