Skip to content

Commit

Permalink
more helper support
Browse files Browse the repository at this point in the history
  • Loading branch information
dgreensp committed Jun 17, 2013
1 parent cd8bf66 commit 7eefb31
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
27 changes: 18 additions & 9 deletions packages/spacebars/spacebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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':
Expand Down
18 changes: 18 additions & 0 deletions packages/spacebars/spacebars_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");',
'}');
});

0 comments on commit 7eefb31

Please sign in to comment.