From 5a1229b35b862a6f88f45748ba265254f1f6223f Mon Sep 17 00:00:00 2001 From: Mark Smith Date: Mon, 19 Nov 2018 01:06:41 +0700 Subject: [PATCH 1/4] Add client side ejs compiled with express middleware example --- examples/express/README.md | 5 +++++ examples/express/app.js | 27 +++++++++++++++++++++++++++ examples/express/package.json | 11 +++++++++++ examples/express/views/index.ejs | 28 ++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+) create mode 100644 examples/express/README.md create mode 100644 examples/express/app.js create mode 100644 examples/express/package.json create mode 100644 examples/express/views/index.ejs diff --git a/examples/express/README.md b/examples/express/README.md new file mode 100644 index 00000000..7ffe130b --- /dev/null +++ b/examples/express/README.md @@ -0,0 +1,5 @@ +``` +npm install +npm start +open http://localhost:3000 +``` diff --git a/examples/express/app.js b/examples/express/app.js new file mode 100644 index 00000000..b79e8d37 --- /dev/null +++ b/examples/express/app.js @@ -0,0 +1,27 @@ +var express = require('express'); +var path = require('path'); +var ejs = require('ejs'); + +var app = express(); +app.set('views', path.join(__dirname, 'views')); +app.set('view engine', 'ejs'); + +function compileEjsTemplate(name, template) { + const compiledTemplate = ejs.compile(template, { + client: true, + outputFunctionName: name + }); + return function compileEjsTemplate(req, res, next) { + res.locals.compiledEjsTemplates = res.locals.compiledEjsTemplates || {}; + res.locals.compiledEjsTemplates[name] = compiledTemplate.toString(); + return next(); + } +} + +app.use(compileEjsTemplate('helloTemplate', "Hello <%= include('messageTemplate', { person: 'John' }); %>")); +app.use(compileEjsTemplate('messageTemplate', "<%= person %> now you know <%= fact %>.")); +app.use('/', function(req, res, next) { + return res.render('index', {}); +}); + +app.listen(process.env.PORT || 3000); diff --git a/examples/express/package.json b/examples/express/package.json new file mode 100644 index 00000000..c34091a3 --- /dev/null +++ b/examples/express/package.json @@ -0,0 +1,11 @@ +{ + "description": "client side ejs compiled with express middleware", + "main": "app.js", + "scripts": { + "start": "node ./app.js" + }, + "dependencies": { + "ejs": "^2.6.1", + "express": "~4.16.0" + } +} diff --git a/examples/express/views/index.ejs b/examples/express/views/index.ejs new file mode 100644 index 00000000..6afcd874 --- /dev/null +++ b/examples/express/views/index.ejs @@ -0,0 +1,28 @@ + + + + ejs client-side + + +
+ + + From b6c13e5e45a9e0dd3e5c7b744a47e25cddb7f303 Mon Sep 17 00:00:00 2001 From: Mark Smith Date: Mon, 19 Nov 2018 01:09:39 +0700 Subject: [PATCH 2/4] Add link to the examples folder --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 30e697f7..19c063ff 100644 --- a/README.md +++ b/README.md @@ -244,6 +244,8 @@ Most of EJS will work as expected; however, there are a few things to note: }); // returns rendered string ``` +See the [examples folder](https://github.com/mde/ejs/tree/master/examples) for more details. + ### IDE Integration with Syntax Highlighting VSCode:Javascript EJS by *DigitalBrainstem* From c9c58b9d5da2269b58d25ca4b402588b76385443 Mon Sep 17 00:00:00 2001 From: Mark Smith Date: Mon, 19 Nov 2018 01:15:56 +0700 Subject: [PATCH 3/4] Update the page title --- examples/express/views/index.ejs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/express/views/index.ejs b/examples/express/views/index.ejs index 6afcd874..43995170 100644 --- a/examples/express/views/index.ejs +++ b/examples/express/views/index.ejs @@ -1,7 +1,7 @@ - ejs client-side + client side ejs compiled with express middleware
From 12ca920a8bc51c0473a97b4eca57aaf6e9400f05 Mon Sep 17 00:00:00 2001 From: Mark Smith Date: Mon, 19 Nov 2018 12:40:32 +0700 Subject: [PATCH 4/4] Linting updates --- examples/express/app.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/express/app.js b/examples/express/app.js index b79e8d37..45c67804 100644 --- a/examples/express/app.js +++ b/examples/express/app.js @@ -15,12 +15,12 @@ function compileEjsTemplate(name, template) { res.locals.compiledEjsTemplates = res.locals.compiledEjsTemplates || {}; res.locals.compiledEjsTemplates[name] = compiledTemplate.toString(); return next(); - } + }; } -app.use(compileEjsTemplate('helloTemplate', "Hello <%= include('messageTemplate', { person: 'John' }); %>")); -app.use(compileEjsTemplate('messageTemplate', "<%= person %> now you know <%= fact %>.")); -app.use('/', function(req, res, next) { +app.use(compileEjsTemplate('helloTemplate', 'Hello <%= include(\'messageTemplate\', { person: \'John\' }); %>')); +app.use(compileEjsTemplate('messageTemplate', '<%= person %> now you know <%= fact %>.')); +app.use('/', function(req, res) { return res.render('index', {}); });