Permalink
Browse files

if a child is an array, assume it's an array of jaml nodes and render…

… them
  • Loading branch information...
1 parent 4cc0884 commit 1c5084d16cb7da13a0a841d9c85b53a2d2583306 @sconover sconover committed with Dec 8, 2010
Showing with 34 additions and 4 deletions.
  1. +22 −1 specs/node_spec.js
  2. +2 −0 specs/template_spec.js
  3. +10 −3 src/Node.js
View
@@ -137,7 +137,28 @@ describe("Jaml.Node", function() {
});
});
- describe("textnodes", function() {
+ describe("array children", function() {
+ it("renders an array of nodes as a series of child nodes. " +
+ "this is often useful when you want to $.map or _.map an array of objects into the equivalent array of nodes.", function(){
+ expect(fooBar.addChild([new Jaml.Node("x"), new Jaml.Node("y")]).
+ addChild(new Jaml.Node("z")).render()).
+ toEqual("<fooBar>\n" +
+ " <x/>\n" +
+ " <y/>\n" +
+ " <z/>\n" +
+ "</fooBar>\n");
+ });
+
+ it("works with mixed node and textnodes", function(){
+ expect(fooBar.addChild([new Jaml.Node("x"), new Jaml.TextNode("y")]).
+ addChild(new Jaml.TextNode("z")).render()).
+ toEqual("<fooBar>\n" +
+ " <x/>\n" +
+ "yz</fooBar>\n");
+ });
+ })
+
+ describe("textnode children", function() {
it("renders", function(){
expect(new Jaml.TextNode("x").render()).
toEqual("x");
View
@@ -112,5 +112,7 @@ describe("Jaml.Template", function() {
"</ul>\n");
});
});
+
+
});
View
@@ -84,11 +84,18 @@ Jaml.Node.prototype = {
node.push(">");
if (multiline) node.push("\n");
-
+
for (var i=0; i < this.children.length; i++) {
- node.push(this.children[i].render(lpad + 2));
+ var child = this.children[i];
+ if (child instanceof Array) {
+ for (var j=0; j < child.length; j++) {
+ node.push(child[j].render(lpad + 2));
+ }
+ } else {
+ node.push(child.render(lpad + 2));
+ }
}
-
+
if (multiline) node.push(this.getPadding(lpad));
node.push("</", this.tagName, ">\n");
}

0 comments on commit 1c5084d

Please sign in to comment.