Skip to content

Commit

Permalink
Updates for chapter 8 (templates)
Browse files Browse the repository at this point in the history
Add ejs snippets and fix some ejs API changes
  • Loading branch information
alexyoung committed Dec 5, 2016
1 parent e64bc90 commit b574eca
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ch08-templates/ejs_snippets/context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const ejs = require('ejs');
const template = '<%= message %>';
const context = { message: 'Hello template!' };
console.log(ejs.render(template, context));
5 changes: 5 additions & 0 deletions ch08-templates/ejs_snippets/delimiter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const ejs = require('ejs');
ejs.delimiter = '$'
const template = '<$= message $>';
const context = { message: 'Hello template!' };
console.log(ejs.render(template, context));
4 changes: 4 additions & 0 deletions ch08-templates/ejs_snippets/escape.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const ejs = require('ejs');
const template = '<%= message %>';
const context = {message: "<script>alert('XSS attack!');</script>"};
console.log(ejs.render(template, context));
14 changes: 14 additions & 0 deletions ch08-templates/ejs_snippets/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "ejs_snippets",
"version": "1.0.0",
"description": "",
"main": "escape.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"ejs": "^2.5.3"
}
}
10 changes: 10 additions & 0 deletions ch08-templates/listing8_4/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const ejs = require('ejs');
const fs = require('fs');
const template = fs.readFileSync('./templates/blog_page.ejs', 'utf8');

function blogPage(entries) {
const values = { entries };
return ejs.render(template, values);
}

console.log(blogPage([{ title: 'Example', date: new Date(), body: 'Example body' }]));
14 changes: 14 additions & 0 deletions ch08-templates/listing8_4/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "listing8_4",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "MIT",
"dependencies": {
"ejs": "^2.5.3"
}
}
File renamed without changes.

0 comments on commit b574eca

Please sign in to comment.