Skip to content

Commit 8d3c890

Browse files
committed
Allow the modulo operator in evaluation context. Closes blueimp#30
1 parent ca2597e commit 8d3c890

File tree

6 files changed

+21
-10
lines changed

6 files changed

+21
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ The template contents are matched and replaced using the regular expression **tm
228228
To use different tags for the template syntax, override **tmpl.regexp** with a modified regular expression, by exchanging all occurrences of "{%" and "%}", e.g. with "[%" and "%]":
229229

230230
```js
231-
tmpl.regexp = /([\s'\\])(?![^%]*%\])|(?:\[%(=|#)([\s\S]+?)%\])|(\[%)|(%\])/g;
231+
tmpl.regexp = /([\s'\\])(?!(?:[^[]|\[(?!%))*%\])|(?:\[%(=|#)([\s\S]+?)%\])|(\[%)|(%\])/g;
232232
```
233233

234234
By default, the plugin preserves whitespace (newlines, carriage returns, tabs and spaces). To strip unnecessary whitespace, you can override the **tmpl.func** function, e.g. with the following code:

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "blueimp-tmpl",
3-
"version": "2.2.2",
3+
"version": "2.3.0",
44
"title": "JavaScript Templates",
55
"description": "< 1KB lightweight, fast & powerful JavaScript templating engine with zero dependencies. Compatible with server-side environments like node.js, module loaders like RequireJS and all web browsers.",
66
"keywords": [

js/tmpl.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* JavaScript Templates 2.2.0
2+
* JavaScript Templates 2.3.0
33
* https://github.com/blueimp/JavaScript-Templates
44
*
55
* Copyright 2011, Sebastian Tschan
@@ -12,7 +12,7 @@
1212
* http://ejohn.org/blog/javascript-micro-templating/
1313
*/
1414

15-
/*jslint evil: true, regexp: true */
15+
/*jslint evil: true, regexp: true, unparam: true */
1616
/*global document, define */
1717

1818
(function ($) {
@@ -34,15 +34,15 @@
3434
tmpl.load = function (id) {
3535
return document.getElementById(id).innerHTML;
3636
};
37-
tmpl.regexp = /([\s'\\])(?![^%]*%\})|(?:\{%(=|#)([\s\S]+?)%\})|(\{%)|(%\})/g;
37+
tmpl.regexp = /([\s'\\])(?!(?:[^{]|\{(?!%))*%\})|(?:\{%(=|#)([\s\S]+?)%\})|(\{%)|(%\})/g;
3838
tmpl.func = function (s, p1, p2, p3, p4, p5) {
39-
if (p1) { // whitespace, quote and backspace in interpolation context
39+
if (p1) { // whitespace, quote and backspace in HTML context
4040
return {
4141
"\n": "\\n",
4242
"\r": "\\r",
4343
"\t": "\\t",
4444
" " : " "
45-
}[s] || "\\" + s;
45+
}[p1] || "\\" + p1;
4646
}
4747
if (p2) { // interpolation: {%=prop%}, or unescaped: {%#prop%}
4848
if (p2 === "=") {

js/tmpl.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "blueimp-tmpl",
3-
"version": "2.2.2",
3+
"version": "2.3.0",
44
"title": "JavaScript Templates",
55
"description": "< 1KB lightweight, fast & powerful JavaScript templating engine with zero dependencies. Compatible with server-side environments like node.js, module loaders like RequireJS and all web browsers.",
66
"keywords": [

test/test.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* JavaScript Templates Test 2.2.0
2+
* JavaScript Templates Test 2.3.0
33
* https://github.com/blueimp/JavaScript-Templates
44
*
55
* Copyright 2011, Sebastian Tschan
@@ -250,6 +250,17 @@
250250
);
251251
});
252252

253+
it('Modulo operator', function () {
254+
expect(
255+
tmpl(
256+
'{% if (o.list.length % 5 === 0) { %}5 list items{% } %}',
257+
data
258+
).replace(/[\r\n]/g, '')
259+
).to.be(
260+
'5 list items'
261+
);
262+
});
263+
253264
});
254265

255266
}(

0 commit comments

Comments
 (0)