EJS include dynamic

0

Using Express 4 and EJS template engine, 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
¦   ¦   ¦   +-- ...

At this point, my route is like this:

router.get('/', (req, res) => {
    res.render('layout', { page: 'admin/index.ejs' });
});

In the 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 index.ejs file in the admin directory is just for testing:

<h2>ADMIN</h2>

But when I access the route, the page to be included is not rendered.

As can be seen, it does not return any errors in the console.

    
asked by anonymous 10.01.2018 / 16:08

1 answer

0

To allow HTML to process, you must use the output tag <%- .

<%- include(page) %>

    
10.01.2018 / 16:19