ejs include function can not find the template with html extension

angry kiwi picture angry kiwi · Apr 28, 2013 · Viewed 14.8k times · Source

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?

Answer

robertklep picture robertklep · Apr 28, 2013

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 %>