| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -256,6 +256,14 @@ Use dot notation to print nested properties: | |||
| 256 | 256 | <strong>{%=o.author.name%}</strong> | |
| 257 | 257 | ``` | |
| 258 | 258 | ||
| 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 | + | ||
| 259 | 267 | ### Evaluation | |
| 260 | 268 | Use **print(str)** to add escaped content to the output: | |
| 261 | 269 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,5 +15,5 @@ | |||
| 15 | 15 | "test": "node ./test/test.js" | |
| 16 | 16 | }, | |
| 17 | 17 | "main": "tmpl", | |
| 18 | - "version": "1.0.1" | ||
| 18 | + "version": "1.0.2" | ||
| 19 | 19 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,5 @@ | |||
| 1 | 1 | /* | |
| 2 | - * JavaScript Templates Test 1.0.1 | ||
| 2 | + * JavaScript Templates Test 1.0.2 | ||
| 3 | 3 | * https://github.com/blueimp/JavaScript-Templates | |
| 4 | 4 | * | |
| 5 | 5 | * Copyright 2011, Sebastian Tschan | |
@@ -23,6 +23,9 @@ | |||
| 23 | 23 | }, | |
| 24 | 24 | data = { | |
| 25 | 25 | value: 'value', | |
| 26 | + nullValue: null, | ||
| 27 | + falseValue: false, | ||
| 28 | + zeroValue: 0, | ||
| 26 | 29 | special: '<>&"\x00', | |
| 27 | 30 | list: [1, 2, 3, 4, 5], | |
| 28 | 31 | func: function () { | |
@@ -134,6 +137,32 @@ | |||
| 134 | 137 | ); | |
| 135 | 138 | }); | |
| 136 | 139 | ||
| 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 | + | ||
| 137 | 166 | $.module('Evaluation', lifecycle); | |
| 138 | 167 | ||
| 139 | 168 | $.test('Escape HTML special characters with print(data)', function () { | |
@@ -166,7 +195,10 @@ | |||
| 166 | 195 | ||
| 167 | 196 | $.test('Else condition', function () { | |
| 168 | 197 | $.strictEqual( | |
| 169 | - $.tmpl('{% if (o.empty) { %}false{% } else { %}true{% } %}', data), | ||
| 198 | + $.tmpl( | ||
| 199 | + '{% if (o.undefinedValue) { %}false{% } else { %}true{% } %}', | ||
| 200 | + data | ||
| 201 | + ), | ||
| 170 | 202 | 'true' | |
| 171 | 203 | ); | |
| 172 | 204 | }); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,5 @@ | |||
| 1 | 1 | /* | |
| 2 | - * JavaScript Templates 1.0.1 | ||
| 2 | + * JavaScript Templates 1.0.2 | ||
| 3 | 3 | * https://github.com/blueimp/JavaScript-Templates | |
| 4 | 4 | * | |
| 5 | 5 | * Copyright 2011, Sebastian Tschan | |
@@ -45,7 +45,7 @@ | |||
| 45 | 45 | if (p3 === "=") { | |
| 46 | 46 | return "'+_e(" + p4 + ")+'"; | |
| 47 | 47 | } | |
| 48 | - return "'+" + p4 + "+'"; | ||
| 48 | + return "'+(" + p4 + "||'')+'"; | ||
| 49 | 49 | } | |
| 50 | 50 | if (p5) { // evaluation start tag: {% | |
| 51 | 51 | return "';"; | |
@@ -63,7 +63,7 @@ | |||
| 63 | 63 | "\x00": "" | |
| 64 | 64 | }; | |
| 65 | 65 | tmpl.encode = function (s) { | |
| 66 | - return String(s).replace( | ||
| 66 | + return String(s || "").replace( | ||
| 67 | 67 | tmpl.encReg, | |
| 68 | 68 | function (c) { | |
| 69 | 69 | return tmpl.encMap[c]; | |
@@ -72,7 +72,7 @@ | |||
| 72 | 72 | }; | |
| 73 | 73 | tmpl.arg = "o"; | |
| 74 | 74 | 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);}" + | ||
| 76 | 76 | ",include=function(s,d){_s+=_t(s,d);}"; | |
| 77 | 77 | if (typeof define === "function" && define.amd) { | |
| 78 | 78 | // Register as an AMD module: | |
| Back | FazBrowse Home | New Git URL |
0 commit comments