Skip to content

Commit 2501044

Browse files
committed
Use {%#o.prop%} instead of {%!o.prop%} to print unescaped strings, to avoid confusion with negated variables.
Remove empty string concatenations from the generated template function body.
1 parent fef6094 commit 2501044

File tree

5 files changed

+13
-13
lines changed

5 files changed

+13
-13
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ The template contents are matched and replaced using the regular expression **tm
226226
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 "**%\\]**":
227227

228228
```js
229-
tmpl.regexp = /(\s+)|('|\\)(?![^%]*%\])|(?:\[%(=|!)(.+?)%\])|(\[%)|(%\])/g;
229+
tmpl.regexp = /(\s+)|('|\\)(?![^%]*%\])|(?:\[%(=|#)(.+?)%\])|(\[%)|(%\])/g;
230230
```
231231

232232
## Templates syntax
@@ -241,7 +241,7 @@ Print variable with HTML special characters escaped:
241241
Print variable without escaping:
242242

243243
```html
244-
<h3>{%!o.user_id%}</h3>
244+
<h3>{%#o.user_id%}</h3>
245245
```
246246

247247
Print output of function calls:

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515
"test": "node ./test/test.js"
1616
},
1717
"main": "tmpl",
18-
"version": "1.0.0"
18+
"version": "1.0.1"
1919
}

test/test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* JavaScript Templates Test 1.0
2+
* JavaScript Templates Test 1.0.1
33
* https://github.com/blueimp/JavaScript-Templates
44
*
55
* Copyright 2011, Sebastian Tschan
@@ -92,9 +92,9 @@
9292
);
9393
});
9494

95-
$.test('Allow HTML special characters with {%!o.prop%}', function () {
95+
$.test('Allow HTML special characters with {%#o.prop%}', function () {
9696
$.strictEqual(
97-
$.tmpl('{%!o.special%}', data),
97+
$.tmpl('{%#o.special%}', data),
9898
'<>&"\x00'
9999
);
100100
});

tmpl.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* JavaScript Templates 1.0
2+
* JavaScript Templates 1.0.1
33
* https://github.com/blueimp/JavaScript-Templates
44
*
55
* Copyright 2011, Sebastian Tschan
@@ -22,9 +22,9 @@
2222
tmpl(tmpl.load(str)) :
2323
new Function(
2424
tmpl.arg,
25-
"var _s=''" + tmpl.helper + ";_s+='" +
25+
("var _s=''" + tmpl.helper + ";_s+='" +
2626
str.replace(tmpl.regexp, tmpl.func) +
27-
"';return _s;"
27+
"';return _s;").split("_s+='';").join("")
2828
);
2929
f.tmpl = f.tmpl || tmpl;
3030
return data ? f(data) : f;
@@ -33,15 +33,15 @@
3333
tmpl.load = function (id) {
3434
return document.getElementById(id).innerHTML;
3535
};
36-
tmpl.regexp = /(\s+)|('|\\)(?![^%]*%\})|(?:\{%(=|!)(.+?)%\})|(\{%)|(%\})/g;
36+
tmpl.regexp = /(\s+)|('|\\)(?![^%]*%\})|(?:\{%(=|#)(.+?)%\})|(\{%)|(%\})/g;
3737
tmpl.func = function (s, p1, p2, p3, p4, p5, p6, o, str) {
3838
if (p1) { // whitespace
3939
return o && o + s.length !== str.length ? " " : "";
4040
}
4141
if (p2) { // single quote or backslash
4242
return "\\" + s;
4343
}
44-
if (p3) { // interpolation: {%=prop%}, or unescaped: {%!prop%}
44+
if (p3) { // interpolation: {%=prop%}, or unescaped: {%#prop%}
4545
if (p3 === "=") {
4646
return "'+_e(" + p4 + ")+'";
4747
}

tmpl.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)