Skip to content

Instantly share code, notes, and snippets.

@kevinchisholm
Last active January 9, 2018 14:42
Show Gist options
  • Save kevinchisholm/5965d300e9ca85e4a76b9b0e667f8345 to your computer and use it in GitHub Desktop.
Save kevinchisholm/5965d300e9ca85e4a76b9b0e667f8345 to your computer and use it in GitHub Desktop.
Monday, Tuesday, Wednesday, Thursday, Friday
var ejs = require('ejs'),
days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'],
html = ejs.render('<%= days.join(", "); %>', {days: days});
//set a reference to the http module
var http = require('http');
//call the http module&apos;s createServer method, to create a server instance
var server = http.createServer(function (req, res) {
//access the response object, and create a Content-Type header
res.writeHead(200, {'Content-Type': 'text/html'});
//end the response, sending some text (the user will see this in their browser)
res.end(html);
})
//tell the server to listen for requests
server.listen(3000);
//just lets us know that the server is running
console.log('In your browser, go to: http://localhost:3000');
var express = require('express'),
path = require('path'),
app = express();
//set the port
app.set('port', 3000);
//set the view engine
app.set('view engine', 'ejs');
//handler for "/"
app.get('/', function(req, res){
res.render('example-2', {
welcomeMessage: "This message was rendered on the server",
headerTitle:"EJS Demo Page"
});
});
// Listen for requests
var server = app.listen(app.get('port'), function () {
console.log('The server is running on http://localhost:' + app.get('port'));
});
<html>
<head>
<title><%= headerTitle %></title>
</head>
<body>
<%= welcomeMessage %>
</body>
</html>
<html>
<head>
<title>EJS Demo Page</title>
</head>
<body>
This message was rendered on the server
</body>
</html>
var express = require('express'),
path = require('path'),
app = express();
//set the port
app.set('port', 3000);
//set the view engine
app.set('view engine', 'ejs');
//handler for "/"
app.get('/', function(req, res){
res.render('example-3', {
headerTitle:"EJS Demo Page - Example # 3",
days: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
});
});
// Listen for requests
var server = app.listen(app.get('port'), function () {
console.log('The server is running on http://localhost:' + app.get('port'));
});
<html>
<head>
<title><%= headerTitle %></title>
</head>
<body>
<ul>
<% days.forEach(function(day){ %>
<li>
<%= day %>
</li>
<% })%>
</ul>
</body>
</html>
<html>
<head>
<title>EJS Demo Page - Example # 3</title>
</head>
<body>
<ul>
<li>Monday</li>
<li>Tuesday</li>
<li>Wednesday</li>
<li>Thursday</li>
<li>Friday</li>
</ul>
</body>
</html>
var express = require('express'),
path = require('path'),
app = express();
//set the port
app.set('port', 3000);
//set the view engine
app.set('view engine', 'ejs');
//handler for "/"
app.get('/', function(req, res){
res.render('example-4', {
welcomeMessage: 'Using "if" logic in an EJS template',
headerTitle:"EJS Demo Page - Example # 4",
days: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
});
});
// Listen for requests
var server = app.listen(app.get('port'), function () {
console.log('The server is running on http://localhost:' + app.get('port'));
});
<html>
<head>
<title><%= headerTitle %></title>
<style>
.selected{
list-style-type: none;
width: 20%;
padding: 10px;
margin: 10px 0;
background-color: #fdfd96;
color: #fc0000;
font-weight: bold;
font-family: Arial;
}
</style>
</head>
<body>
<h1><%= welcomeMessage %></h1>
<ul>
<% days.forEach(function(day, index){ %>
<li
<% if (index === 3) { %>
class="selected"
<% } %> >
<%= day %>
</li>
<% })%>
</ul>
</body>
</html>
<html>
<head>
<title>EJS Demo Page - Example # 4</title>
<style>
.selected{
list-style-type: none;
width: 20%;
padding: 10px;
margin: 10px 0;
background-color: #fdfd96;
color: #fc0000;
font-weight: bold;
font-family: Arial;
}
</style>
</head>
<body>
<h1>Using "if" logic in an EJS template</h1>
<ul>
<li>Monday</li>
<li>Tuesday</li>
<li>Wednesday</li>
<li class="selected">Thursday</li>
<li>Friday</li>
</ul>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment