include in template ejs

0

I'm doing a render recursive and quite simple in this way in express

// index.js
const VIEWS = __dirname + '/app/views/';

app.get('/', function (req, res) {
    res.render(VIEWS + 'includes/partials/body', {layout: VIEWS + 'dashboard/home'});
});

Now, my file app/views/includes/partials/body.ejs is the following

<!-- Start body and layout -->
<% include header %> <!-- Esto carga correctamente -->
<div class="container">
    <% include(layout) %>
    <!-- layout es una variable con valor app/views/dashboard/home -->
    <!-- como se puede ver arriba en mi res.render() -->
</div>
<% include footer %> <!-- Esto carga correctamente -->
<!-- End body and layout -->

This is my file app/views/dashboard/home.ejs

<h1>Home</h1>

No matter how much I change the content of app/views/dashboard/home or give another value to the variable layout for another route with another file and another html different ALWAYS results in this:

<!-- Start header -->
<!DOCTYPE HTML>
<html>
<head>
    <title>yT</title>
    <link rel="stylesheet" href="./public/bootstrap/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="./public/css/style.css">
</head>

<body>
    <br />
    <div class="container">
        <nav class="navbar navbar-inverse">
            <div class="container-fluid">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
              <span class="sr-only">Toggle navigation</span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
            </button>
                    <a class="navbar-brand" href="#">yT</a>
                </div>
                <div id="navbar" class="navbar-collapse collapse">
                    <ul id="supertabs" class="nav navbar-nav" data-tabs="supertabs">
                        <li><a href="#y" data-toggle="tab">y</a></li>
                        <li><a href="#T" data-toggle="tab">T</a></li>
                        <li><a href="#c" data-toggle="tab">c</a></li>
                    </ul>
                </div>
            </div>
        </nav>
    </div>
<!-- End header -->
<!-- Start body and layout -->
<div class="container">
    <!-- Aquí debería renderizarse un <h1>Home</h1> -->
    <!-- Pero nunca lo hace :C -->
</div>
<!-- End body and layout -->
<!-- Start footer -->
    <script src="./public/jquery/dist/jquery.min.js"></script>
    <script src="./public/bootstrap/dist/js/bootstrap.min.js"></script>
    <script src="./public/socket.io/socket.io.js"></script>
    <script src="./private/js/app.js"></script>
</body>
</html>
<!-- End footer -->

That is to say that include(layout) NEVER render to me what is inside said file in said route, How can I solve this?

    
asked by Jorius 15.03.2017 в 02:32
source

1 answer

1

I solved it by reading here the documentation in github in the Tags section.

Adding a script ( - ) where I perform include(layout) emits the value of the layout variable without escaping and thus correctly loads the .ejs file:

<!-- Start body and layout -->
<%- include('header') %> 
<!-- 
Esto lo cambie para un mejor uso del motor
y le agregué un guión ( - ) después de la <% para 
"emitir el valor sin escape en la plantilla"
-->
<div class="container">
    <%- include(layout) %> <!-- Al igual que aquí le agrego el guión ( - ) -->
</div>
<%- include('footer') %> <!-- Al igual que cambié esto para lo primeramente mencionado --> 
<!-- End body and layout -->
    
answered by 15.03.2017 / 15:41
source