Permalink
Browse files

javascript: enhance 'Template' class to support 'tostrfunc' option

  • Loading branch information...
1 parent 91ec9b6 commit 0edf0dfcc9d687bbd9d8a190b95b91c1fda7ce57 @kwatch committed Aug 22, 2011
Showing with 18 additions and 1 deletion.
  1. +5 −1 javascript/lib/tenjin.js
  2. +13 −0 javascript/test/template_test.js
View
@@ -342,6 +342,9 @@ Tenjin.Template = function(filename, properties) {
else if (! this.preamble) this.preamble = '';
if (this.postamble == true) delete(this.postamble);
else if (! this.postamble) this.postamble = '';
+ if (properties.tostrfunc) {
+ this.tostrfunc = properties.tostrfunc;
+ }
if (properties.escapefunc) {
var funcname = properties.escapefunc;
if (funcname.charAt(0) == '.') {
@@ -357,6 +360,7 @@ Tenjin.Template = function(filename, properties) {
};
Tenjin.Template.__props__ = {
+ tostrfunc : "toStr",
escapefunc : "escapeXml",
preamble : "var _buf = ''; ",
postamble : "_buf\n",
@@ -560,7 +564,7 @@ Tenjin.Template.prototype = {
hookExpression: function(expr, flag_escape) {
if (this.emptystr) {
//return flag_escape ? this.escapeExpression(expr) : "[" + expr + "].join()";
- return flag_escape ? this.escapeExpression(expr) : "toStr(" + expr + ")";
+ return flag_escape ? this.escapeExpression(expr) : this.tostrfunc + "(" + expr + ")";
}
else {
return flag_escape ? this.escapeExpression(expr) : "(" + expr + ")";
@@ -124,6 +124,19 @@ topic('Tenjin.Template', function(t) {
ok (t2.render(context)).eq(output2);
});
+ spec("allows to change 'toStr()' by 'tostrfunc' option.", function() {
+ var input = "<p>Hello #{name}!</p>\n";
+ var script_expected = (
+ ''
+ + "var _buf = ''; _buf += '<p>Hello ' + String(name) + '!</p>\\n';\n"
+ + "_buf\n"
+ );
+ var fname = 'input.jshtml';
+ this.writeFile(fname, input);
+ var t1 = new Tenjin.Template({tostrfunc: 'String'});
+ ok (t1.convert(input)).eq(script_expected);
+ });
+
});

0 comments on commit 0edf0df

Please sign in to comment.