From a35729f7ada7e3447c32b191e2365279f2d8b11c Mon Sep 17 00:00:00 2001 From: druidvav Date: Thu, 11 Oct 2012 16:42:18 +0400 Subject: [PATCH] false and zero values are not treated as 0 --- test/test.js | 4 ++-- tmpl.js | 9 ++++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/test/test.js b/test/test.js index f4b6b8e..f0a692c 100644 --- a/test/test.js +++ b/test/test.js @@ -140,7 +140,7 @@ data ) ).to.be( - '' + '00' ); }); @@ -154,7 +154,7 @@ data ) ).to.be( - '' + '00' ); }); diff --git a/tmpl.js b/tmpl.js index c8b4b86..c1dc727 100644 --- a/tmpl.js +++ b/tmpl.js @@ -22,7 +22,7 @@ tmpl(tmpl.load(str)) : new Function( tmpl.arg + ',tmpl', - "var _e=tmpl.encode" + tmpl.helper + ",_s='" + + "var _e=tmpl.encode" + tmpl.helper + ",_r=tmpl.raw" + tmpl.helper + ",_s='" + str.replace(tmpl.regexp, tmpl.func) + "';return _s;" ); @@ -48,7 +48,7 @@ if (p2 === "=") { return "'+_e(" + p3 + ")+'"; } - return "'+(" + p3 + "||'')+'"; + return "'+_r(" + p3 + ")+'"; } if (p4) { // evaluation start tag: {% return "';"; @@ -66,13 +66,16 @@ "'" : "'" }; tmpl.encode = function (s) { - return String(s || "").replace( + return String((s === 0) ? '0' : (s || "")).replace( tmpl.encReg, function (c) { return tmpl.encMap[c] || ""; } ); }; + tmpl.raw = function (s) { + return String((s === 0) ? '0' : (s || "")); + }; tmpl.arg = "o"; tmpl.helper = ",print=function(s,e){_s+=e&&(s||'')||_e(s);}" + ",include=function(s,d){_s+=tmpl(s,d);}";