User Tools

Site Tools


bwnjsejs:using_locals_and_conditional_templates

Locals : informations que l'on veut retrouver dans toutes les pages.

index.js

var express = require('express');
var app = express();

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

app.locals.pagetitle = "Awesome Website";

app.get('/', function(req, res) {
  console.log('/ requested');
  res.render('default', { title: 'Hello', content: 'Heyyy salut-donc !', users: ['Mireille', 'José', 'Marcel', 'Odette', 'Lucien', 'Félicienne'] });
})

// si on a autre chose
app.get('*', function(req, res) {
  console.log('Page inconnue demandée');
  res.render('default', { title: '😭', content: '😭😭😭'});
})

var server = app.listen(3000, function() {
  console.log('Listening on port 3000.');
})

default.ejs

<!DOCTYPE html>
<html>
<head><% include partials/head.ejs %></head>
<body>
<h1><%= pagetitle %> - <%= title %></h1>
<p><%= content %></p>

<% if (typeof(users) !== 'undefined') { %>
  <ul>
  <% for (var i=0; i <users.length; i++) {%>
    <li><%= users[i] %></li>
  <% } %>
  </ul>
<% } %>

</body>
</html>
bwnjsejs/using_locals_and_conditional_templates.txt · Last modified: 2016/05/08 17:59 by leo