Permalink
Browse files

Closes #16 Merge branch 'issue-16'

  • Loading branch information...
2 parents 13d1ae5 + 62e7d14 commit c9546b3fd936c8b1fedd6eb67357f0062b783303 @erikvold erikvold committed Jan 29, 2011
Showing with 43 additions and 41 deletions.
  1. +8 −5 specs/template_spec.js
  2. +35 −36 src/Template.js
@@ -15,7 +15,9 @@ describe("Jaml.Template", function() {
br(), hr(),
table(
thead(tr(th())),
+ tfoot(tr(td())),
tbody(tr(td()))
+
),
ul(li(), ol()),
dl(), dt(), dd(),
@@ -39,6 +41,7 @@ describe("Jaml.Template", function() {
" <br/>\n <hr/>\n" +
" <table>\n" +
" <thead>\n <tr>\n <th/>\n </tr>\n </thead>\n" +
+ " <tfoot>\n <tr>\n <td/>\n </tr>\n </tfoot>\n" +
" <tbody>\n <tr>\n <td/>\n </tr>\n </tbody>\n" +
" </table>\n" +
" <ul>\n <li/>\n <ol/>\n </ul>\n" +
@@ -88,8 +91,8 @@ describe("Jaml.Template", function() {
describe("array data", function() {
it("renders the template for each item in the array", function(){
- expect(new Jaml.Template(function(widget){
- ul(
+ expect(new Jaml.Template(function(widget, i){
+ ul({id: i},
li(widget.primaryColor),
li(widget.secondaryColor)
)
@@ -98,15 +101,15 @@ describe("Jaml.Template", function() {
{primaryColor: "orange", secondaryColor: "blue"},
{primaryColor: "yellow", secondaryColor: "purple"}
])).
- toEqual("<ul>\n" +
+ toEqual("<ul id=\"0\">\n" +
" <li>red</li>\n" +
" <li>green</li>\n" +
"</ul>\n" +
- "<ul>\n" +
+ "<ul id=\"1\">\n" +
" <li>orange</li>\n" +
" <li>blue</li>\n" +
"</ul>\n" +
- "<ul>\n" +
+ "<ul id=\"2\">\n" +
" <li>yellow</li>\n" +
" <li>purple</li>\n" +
"</ul>\n");
View
@@ -38,7 +38,7 @@ Jaml.Template.prototype = {
with(this) {
for (var i=0; i < data.length; i++) {
- eval("(" + this.tpl.toString() + ")(data[i])");
+ eval("(" + this.tpl.toString() + ")(data[i], i)");
};
}
@@ -73,7 +73,7 @@ Jaml.Template.prototype = {
tags: [
"html", "head", "body", "script", "meta", "title", "link",
"div", "p", "span", "a", "img", "br", "hr",
- "table", "tr", "th", "td", "thead", "tbody",
+ "table", "tr", "th", "td", "thead", "tbody", "tfoot",
"ul", "ol", "li",
"dl", "dt", "dd",
"h1", "h2", "h3", "h4", "h5", "h6", "h7",
@@ -86,46 +86,45 @@ Jaml.Template.prototype = {
*/
(function() {
var tags = Jaml.Template.prototype.tags;
-
- for (var i = tags.length - 1; i >= 0; i--){
- var tagName = tags[i];
-
- /**
- * This function is created for each tag name and assigned to Template's
- * prototype below
- */
- var fn = function(tagName) {
- return function(attrs) {
- var node = new Jaml.Node(tagName);
-
- var firstArgIsAttributes = (typeof attrs == 'object')
- && !(attrs instanceof Jaml.Node)
- && !(attrs instanceof Jaml.TextNode);
- if (firstArgIsAttributes) node.setAttributes(attrs);
+ /**
+ * This function is created for each tag name and assigned to Template's
+ * prototype below
+ */
+ var makeTagHelper = function(tagName) {
+ return function(attrs) {
+ var node = new Jaml.Node(tagName);
+
+ var firstArgIsAttributes = (typeof attrs == 'object')
+ && !(attrs instanceof Jaml.Node)
+ && !(attrs instanceof Jaml.TextNode);
+
+ if (firstArgIsAttributes) node.setAttributes(attrs);
- var startIndex = firstArgIsAttributes ? 1 : 0;
+ var startIndex = firstArgIsAttributes ? 1 : 0;
- for (var i=startIndex; i < arguments.length; i++) {
- var arg = arguments[i];
+ for (var i=startIndex; i < arguments.length; i++) {
+ var arg = arguments[i];
- if (typeof arg == "string" || arg == undefined) {
- arg = new Jaml.TextNode(arg || "");
- }
-
- if (arg instanceof Jaml.Node || arg instanceof Jaml.TextNode) {
- arg.parent = node;
- }
+ if (typeof arg == "string" || arg == undefined) {
+ arg = new Jaml.TextNode(arg || "");
+ }
- node.addChild(arg);
- };
-
- this.nodes.push(node);
-
- return node;
+ if (arg instanceof Jaml.Node || arg instanceof Jaml.TextNode) {
+ arg.parent = node;
+ }
+
+ node.addChild(arg);
};
+
+ this.nodes.push(node);
+
+ return node;
};
-
- Jaml.Template.prototype[tagName] = fn(tagName);
+ };
+
+ for (var i = tags.length - 1; i >= 0; i--){
+ var tagName = tags[i];
+ Jaml.Template.prototype[tagName] = makeTagHelper(tagName);
};
})();

0 comments on commit c9546b3

Please sign in to comment.