EJS include dynamic

Asked

Viewed 1,580 times

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.

Página não renderizada

As can be seen, no error returns in the console.

1 answer

3


To allow the HTML to be processed, use the output tag <%-.

<%- include(page) %>

Página renderizada

  • Maaano.. was going crazy here. kkkk I could not generate an html with this tamplate. Thanks friend! Helped a lot!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.