diff --git a/packages/spacebars/spacebars.js b/packages/spacebars/spacebars.js index 807973e357..0cfc0dff8a 100644 --- a/packages/spacebars/spacebars.js +++ b/packages/spacebars/spacebars.js @@ -628,6 +628,18 @@ Spacebars.compile = function (inputString) { return parts.join('+'); };*/ + var codeGenBasicStache = function (tag, funcInfo) { + funcInfo.usedSelf = true; + var code = 'self.lookup(' + toJSLiteral(tag.path[0]) + ')'; + if (tag.path.length > 1) { + code = 'Spacebars.index(' + code + ', ' + + _.map(tag.path.slice(1), toJSLiteral).join(', ') + ')'; + } + // XXX pass args to `call` + code = 'String(Spacebars.call(' + code + '))'; + return code; + }; + // Return the source code of a string or (reactive) function // (if necessary). var interpolate = function (strOrArray, funcInfo, interpolateMode) { @@ -655,15 +667,7 @@ Spacebars.compile = function (inputString) { tag.type === 'DOUBLE') throw new Error("Can only have triple-stache for dynamic attributes"); - funcInfo.usedSelf = true; - var code = 'self.lookup(' + toJSLiteral(tag.path[0]) + ')'; - if (tag.path.length > 1) { - code = 'Spacebars.index(' + code + ', ' + - _.map(tag.path.slice(1), toJSLiteral).join(', ') + ')'; - } - // XXX pass args to `call` - code = 'String(Spacebars.call(' + code + '))'; - parts.push(code); + parts.push(codeGenBasicStache(tag, funcInfo)); break; default: throw new Error("Unknown stache tag type: " + tag.type); @@ -712,6 +716,11 @@ Spacebars.compile = function (inputString) { break; case 'DOUBLE': case 'TRIPLE': + bodyLines.push( + 'buf.' + + (tag.type === 'TRIPLE' ? 'rawHtml' : 'text') + + '(' + codeGenBasicStache(tag, funcInfo) + + ');'); // XXX implement break; case 'COMMENT': diff --git a/packages/spacebars/spacebars_tests.js b/packages/spacebars/spacebars_tests.js index 9297a1ff04..a44256e89a 100644 --- a/packages/spacebars/spacebars_tests.js +++ b/packages/spacebars/spacebars_tests.js @@ -357,4 +357,22 @@ Tinytest.add("spacebars - compiler", function (test) { ' var self = this;', ' buf.openTag("a", {"foo": function () { return String(Spacebars.call(Spacebars.index(self.lookup("bar"), "baz"))); }});', '}'); + + run('foo {{bar}} baz', + + 'function (buf) {', + ' var self = this;', + ' buf.text("foo ");', + ' buf.text(String(Spacebars.call(self.lookup("bar"))));', + ' buf.text(" baz");', + '}'); + + run('foo {{{bar}}} baz', + + 'function (buf) {', + ' var self = this;', + ' buf.text("foo ");', + ' buf.rawHtml(String(Spacebars.call(self.lookup("bar"))));', + ' buf.text(" baz");', + '}'); });