Skip to content

Commit

Permalink
Support switch statements inside templates.
Browse files Browse the repository at this point in the history
  • Loading branch information
rgrove committed Oct 2, 2012
1 parent 7d9b214 commit 6b97684
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/template/js/template-micro.js
Expand Up @@ -125,15 +125,18 @@ Micro.compile = function (text, options) {
})

.replace(options.code, function (match, code) {
return tokenOpen + (blocks.push("';\n" + code + "\n;$t+='") - 1) + tokenClose;
return tokenOpen + (blocks.push("';\n" + code + "\n$t+='") - 1) + tokenClose;
})

.replace(options.stringEscape, "\\$&")

// Replace the token placeholders with code.
.replace(/\ufffe(\d+)\uffff/g, function (match, index) {
return blocks[parseInt(index, 10)];
}) +
})

// Remove noop string concatenations that have been left behind.
.replace(/\n\$t\+='';\n/g, '\n') +

"';\nreturn $t;";

Expand Down
24 changes: 24 additions & 0 deletions src/template/tests/unit/assets/template-test.js
Expand Up @@ -84,6 +84,30 @@ microSuite.add(new Y.Test.Case({
Assert.areSame('', compiled({display: false}));
},

'<% ... %> should support switch statements': function () {
var compiled = Micro.compile(
'test' +
'<% switch (data.foo) { %>' +
'<% case "a": %>' +
'a' +
'<% break; %>' +

'<% case "b": %>' +
'b' +
'<% break; %>' +

'<% case "c": %>' +
'c' +
'<% break; %>' +
'<% } %>' +
'test'
);

Assert.areSame('testatest', compiled({foo: 'a'}));
Assert.areSame('testbtest', compiled({foo: 'b'}));
Assert.areSame('testctest', compiled({foo: 'c'}));
},

'<%= ... %> should be rendered as HTML-escaped output': function () {
Assert.areSame('at&amp;t', Micro.render('<%= data.name %>', {name: 'at&t'}));
},
Expand Down

0 comments on commit 6b97684

Please sign in to comment.