Monday 5 June 2017

NodeJs - How to render EJS in a rendered EJS?

that's my idea: I want to load a rendered ejs file as the body of my main template ejs file. Is it possible?

My app.js:

router.get('/', function(req, res) {
  res.render('layout', {title: "Example title", body: _____________});
});

I tried the following but it did not work:

body: res.render('page1_body_content')

That would be my main-template-file (layout.ejs)

<!doctype html>
<html>
        <head>
                <title>
                        <% (title) ? title : '' %>
                </title>
                <link rel="stylesheet" href="https://v4-alpha.getbootstrap.com/dist/css/bootstrap.min.css">
                <link rel="stylesheet" href="css/font-awesome-4.7.0/css/font-awesome.min.css">
                <link rel="stylesheet" href="css/style.css" />
        </head>
        <body>
                <%= body %>
        </body>
</html>

My 2nd file (page1_body_content.ejs) looks like this:

<b>hallo</b>

The result should look like this:

<!doctype html>
<html>
        <head>
                <title>
                        Expample title
                </title>
                <link rel="stylesheet" href="https://v4-alpha.getbootstrap.com/dist/css/bootstrap.min.css">
                <link rel="stylesheet" href="css/font-awesome-4.7.0/css/font-awesome.min.css">
                <link rel="stylesheet" href="css/style.css" />
        </head>
        <body>
                <b>Hallo</b>
        </body>
</html>


via Alpha

No comments:

Post a Comment