Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ejs include function can not find the template with html extension

My ejs engine set up is app.js is like below:

// this parse html file as ejs file
    app.engine('.html', require('ejs').__express);
    app.set('view engine', 'html');
    app.set('views', __dirname + '/view');

My directory is like this:

view (folder)
  home.html
  head.html
app.js

Home.html is like this:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>home</title>
<% include head %>
</head>

<body>

</body>
</html>

and head.html is like this:

<link rel="stylesheet" type="text/css" href="css/main.css">
<script type="text/javascript" src="js/jquery-1.5.js"></script>

the problem is the file head.html will not be parsed if the extension was html. Error says it expect ejs file. So there is a problem with include function?

like image 907
angry kiwi Avatar asked Apr 28 '13 10:04

angry kiwi


People also ask

Can I use EJS in HTML?

EJS (Embedded JavaScript Templating) is one of the most popular template engines for JavaScript. As the name suggests, it lets us embed JavaScript code in a template language that is then used to generate HTML.


1 Answers

As Elie Gnrd is suggesting, you use .ejs files directly by changing the view engine configuration of Express.

If that isn't an option, and you want/need to keep using .html as an extension for your templates, you have to be explicit in the include:

<% include head.html %>
like image 118
robertklep Avatar answered Oct 04 '22 12:10

robertklep