Skip to content

Latest commit

 

History

History
35 lines (25 loc) · 1.22 KB

using-view-engine.md

File metadata and controls

35 lines (25 loc) · 1.22 KB

Express - using view engine

Today I learnt how to use view engine with Express framework.

To use a view engine, you need to declare it in the runner (routes) file i.e. app.js. It's done with a function app.set('view engine, 'ENGINE NAME'). Then, we need to tell Express where these view files will be stored as such app.set('views', './src/views'). Finally, to return a view as a response to a route request, res.render is used.

For example, to use EJS view engine, the app.js file would looks as follows:

app.set('views', './src/views');

app.set('view engine', 'ejs');

app.get('/', function (req, res) {
    res.render('index', {
        title: 'Hello from render',
        list: ['a', 'b']
    });
});

Notice how we pass a JSON object as a second parameter to res.render. The variables declared in the object are available in the rendered view file and can be used as such:

<ul class="nav navbar-nav">
    <% for(var i = 0; i < list.length; i++) { %>
        <li><a><%= list[i] %></a></li>
    <% } %>
</ul>

Resources