Skip to content

Commit

Permalink
javascript: enhance 'Template' class to support 'tostrfunc' option
Browse files Browse the repository at this point in the history
  • Loading branch information
kwatch committed Aug 22, 2011
1 parent 91ec9b6 commit 0edf0df
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
6 changes: 5 additions & 1 deletion javascript/lib/tenjin.js
Expand Up @@ -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) == '.') {
Expand All @@ -357,6 +360,7 @@ Tenjin.Template = function(filename, properties) {
};

Tenjin.Template.__props__ = {
tostrfunc : "toStr",
escapefunc : "escapeXml",
preamble : "var _buf = ''; ",
postamble : "_buf\n",
Expand Down Expand Up @@ -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 + ")";
Expand Down
13 changes: 13 additions & 0 deletions javascript/test/template_test.js
Expand Up @@ -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);
});

});


Expand Down

0 comments on commit 0edf0df

Please sign in to comment.