-
Notifications
You must be signed in to change notification settings - Fork 844
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:mde/ejs
- Loading branch information
Showing
9 changed files
with
109 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
``` | ||
npm install | ||
npm start | ||
open http://localhost:3000 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) { | ||
return res.render('index', {}); | ||
}); | ||
|
||
app.listen(process.env.PORT || 3000); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>client side ejs compiled with express middleware</title> | ||
</head> | ||
<body> | ||
<div id="greeting"></div> | ||
<script> | ||
const helloTemplateFn = <%- compiledEjsTemplates.helloTemplate %>; | ||
const messageTemplateFn = <%- compiledEjsTemplates.messageTemplate %>; | ||
(function() { | ||
const includeFn = function(path, data) { | ||
if (path === 'messageTemplate') { | ||
return messageTemplateFn({ | ||
fact: 'how to render a page with compiled ejs templates', | ||
person: data.person | ||
}); | ||
} | ||
}; | ||
const html = helloTemplateFn({}, null, includeFn, null); | ||
const $greeting = document.getElementById('greeting'); | ||
$greeting.innerHTML = html; | ||
})(); | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters