Ejs engine with html not working

I use html files instead of ejs, but the express engine is ejs

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

I am configured as

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

I draw my index template as follows:

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

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

Related Entries Node.js express: confuse ejs template

Existing example that does not work https://github.com/visionmedia/express/tree/master/examples/ejs

+4
source share
1 answer

The original method for what you asked for would use partial ones. Particles have been removed and replaced with the include EJS function. Here is how you would include the file:

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

Any locals that you go to the rendering page are also passed to include. For instance:

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 result is HTML:

 <!DOCTYPE html> <body> value <pre>random_value</pre> </body> 
+5
source

Source: https://habr.com/ru/post/1501755/


All Articles