1
Using Express 4 and template engine EJS, I came across the following:
Let’s say I have the following structure:
meu_projeto/
+-- server.js
+-- views/
¦ +-- layout.js
¦ +-- admin/
¦ ¦ +-- index.js
¦ ¦ +-- blog/
¦ ¦ ¦ +-- index.js
¦ ¦ ¦ +-- ...
¦ ¦ +-- configs/
¦ ¦ ¦ +-- index.js
¦ ¦ ¦ +-- ...
Right now, my route is like this:
router.get('/', (req, res) => {
res.render('layout', { page: 'admin/index.ejs' });
});
In the archive layout ejs.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Exemplo</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css">
</head>
<body>
<% include(page) %>
</body>
</html>
The archive index ejs. in the directory admin is so just to test:
<h2>ADMIN</h2>
But when I access the route, the page to be included is not rendered.
As can be seen, no error returns in the console.
Maaano.. was going crazy here. kkkk I could not generate an html with this tamplate. Thanks friend! Helped a lot!
– Andrei Coelho