Skip to content

Commit 5614516

Browse files
committed
Print falsy values as empty strings.
1 parent 2501044 commit 5614516

File tree

5 files changed

+48
-8
lines changed

5 files changed

+48
-8
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,14 @@ Use dot notation to print nested properties:
256256
<strong>{%=o.author.name%}</strong>
257257
```
258258

259+
Note that the JavaScript Templates engine prints **falsy** values as empty strings.
260+
That is, **undefined**, **null**, **false**, **0** and **NaN** will all be converted to **''**.
261+
To be able to print e.g. the number 0, convert it to a String before using it as an output variable:
262+
263+
```html
264+
<h3>{%=0+''%}</h3>
265+
```
266+
259267
### Evaluation
260268
Use **print(str)** to add escaped content to the output:
261269

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.1"
18+
"version": "1.0.2"
1919
}

test/test.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* JavaScript Templates Test 1.0.1
2+
* JavaScript Templates Test 1.0.2
33
* https://github.com/blueimp/JavaScript-Templates
44
*
55
* Copyright 2011, Sebastian Tschan
@@ -23,6 +23,9 @@
2323
},
2424
data = {
2525
value: 'value',
26+
nullValue: null,
27+
falseValue: false,
28+
zeroValue: 0,
2629
special: '<>&"\x00',
2730
list: [1, 2, 3, 4, 5],
2831
func: function () {
@@ -134,6 +137,32 @@
134137
);
135138
});
136139

140+
$.test('Print empty string for escaped falsy values', function () {
141+
$.strictEqual(
142+
$.tmpl(
143+
'{%=o.undefinedValue%}{% print(o.undefinedValue); %}' +
144+
'{%=o.nullValue%}{% print(o.nullValue); %}' +
145+
'{%=o.falseValue%}{% print(o.falseValue); %}' +
146+
'{%=o.zeroValue%}{% print(o.zeroValue); %}',
147+
data
148+
),
149+
''
150+
);
151+
});
152+
153+
$.test('Print empty string for unescaped falsy values', function () {
154+
$.strictEqual(
155+
$.tmpl(
156+
'{%#o.undefinedValue%}{% print(o.undefinedValue, true); %}' +
157+
'{%#o.nullValue%}{% print(o.nullValue, true); %}' +
158+
'{%#o.falseValue%}{% print(o.falseValue, true); %}' +
159+
'{%#o.zeroValue%}{% print(o.zeroValue, true); %}',
160+
data
161+
),
162+
''
163+
);
164+
});
165+
137166
$.module('Evaluation', lifecycle);
138167

139168
$.test('Escape HTML special characters with print(data)', function () {
@@ -166,7 +195,10 @@
166195

167196
$.test('Else condition', function () {
168197
$.strictEqual(
169-
$.tmpl('{% if (o.empty) { %}false{% } else { %}true{% } %}', data),
198+
$.tmpl(
199+
'{% if (o.undefinedValue) { %}false{% } else { %}true{% } %}',
200+
data
201+
),
170202
'true'
171203
);
172204
});

tmpl.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* JavaScript Templates 1.0.1
2+
* JavaScript Templates 1.0.2
33
* https://github.com/blueimp/JavaScript-Templates
44
*
55
* Copyright 2011, Sebastian Tschan
@@ -45,7 +45,7 @@
4545
if (p3 === "=") {
4646
return "'+_e(" + p4 + ")+'";
4747
}
48-
return "'+" + p4 + "+'";
48+
return "'+(" + p4 + "||'')+'";
4949
}
5050
if (p5) { // evaluation start tag: {%
5151
return "';";
@@ -63,7 +63,7 @@
6363
"\x00": ""
6464
};
6565
tmpl.encode = function (s) {
66-
return String(s).replace(
66+
return String(s || "").replace(
6767
tmpl.encReg,
6868
function (c) {
6969
return tmpl.encMap[c];
@@ -72,7 +72,7 @@
7272
};
7373
tmpl.arg = "o";
7474
tmpl.helper = ",_t=arguments.callee.tmpl,_e=_t.encode" +
75-
",print=function(s,e){_s+=e&&s||_e(s);}" +
75+
",print=function(s,e){_s+=e&&(s||'')||_e(s);}" +
7676
",include=function(s,d){_s+=_t(s,d);}";
7777
if (typeof define === "function" && define.amd) {
7878
// Register as an AMD module:

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.

0 commit comments

Comments
 (0)