Skip to content

Commit

Permalink
Merge branch 'master' of github.com:mde/ejs
Browse files Browse the repository at this point in the history
  • Loading branch information
mde committed Jun 2, 2019
2 parents 8a6abe4 + f2c77d7 commit cea58ff
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 11 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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*
Expand Down
5 changes: 5 additions & 0 deletions examples/express/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
```
npm install
npm start
open http://localhost:3000
```
27 changes: 27 additions & 0 deletions examples/express/app.js
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);
11 changes: 11 additions & 0 deletions examples/express/package.json
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"
}
}
28 changes: 28 additions & 0 deletions examples/express/views/index.ejs
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>
15 changes: 8 additions & 7 deletions lib/ejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,12 @@ exports.renderFile = function () {
* @public
*/

/**
* EJS template class
* @public
*/
exports.Template = Template;

exports.clearCache = function () {
exports.cache.reset();
};
Expand Down Expand Up @@ -670,9 +676,9 @@ Template.prototype = {

if (opts.rmWhitespace) {
// Have to use two separate replace here as `^` and `$` operators don't
// work well with `\r`.
// work well with `\r` and empty lines don't work well with the `m` flag.
this.templateText =
this.templateText.replace(/\r/g, '').replace(/^\s+|\s+$/gm, '');
this.templateText.replace(/[\r\n]+/g, '\n').replace(/^\s+|\s+$/gm, '');
}

// Slurp spaces and tabs before <%_ and after _%>
Expand Down Expand Up @@ -776,11 +782,6 @@ Template.prototype = {
line = line.replace(/^(?:\r\n|\r|\n)/, '');
this.truncate = false;
}
else if (this.opts.rmWhitespace) {
// rmWhitespace has already removed trailing spaces, just need
// to remove linebreaks
line = line.replace(/^\n/, '');
}
if (!line) {
return line;
}
Expand Down
3 changes: 3 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ exports.cache = {
get: function (key) {
return this._data[key];
},
remove: function (key) {
delete this._data[key];
},
reset: function () {
this._data = {};
}
Expand Down
11 changes: 10 additions & 1 deletion test/fixtures/rmWhitespace.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,16 @@ adsffadsfadsfad<%= f %>
piece of text.
<% var a = 'a' %>
Text again.
<% var aa = a + 'a' %>
Raw output:
<%- aa %>
Blank

Line
<% var b = 'b' %>
<% var c = 'c'
var d = 'd' %>
Another text. <%= c %>
Another text. <%= c %>
After escaped
<% /* newline slurp */ -%>
Last line
Expand Down
18 changes: 15 additions & 3 deletions test/fixtures/rmWhitespace.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,19 @@
<tag2>
A very long piece of text very long piece of text very long piece of
text very long piece of text very long piece of
text very long piece oftext very long
adsffadsfadsfadfpiece of text.
tex
t very long piece oftext very long
adsffadsfadsfadf
piece of text.

Text again.
Another text. c

Raw output:
aa
Blank
Line


Another text. c
After escaped
Last line

0 comments on commit cea58ff

Please sign in to comment.