I really like what Dan Webb has done in LowPro (
http://www.danwebb.net/
lowpro) for making DOM building easier. His site isn't responding just
at the moment so I can't grab his exact code, but since I still prefer
scripty's Builder.node() I have this in my extensions that achieves
the same result:
("p|div|span|strong|em|img|table|tr|td|th|thead|tbody|tfoot|pre|
code|" +
"h1|h2|h3|h4|h5|h6|ul|ol|li|form|input|textarea|legend|fieldset|"
+
"select|option|blockquote|cite|br|hr|dd|dl|dt|address|a|button|abbr|
acronym|" +
"script|link|style|bdo|ins|del|object|param|col|colgroup|optgroup|
caption|" +
"label|dfn|kbd|samp|var").split("|").each(
function(el) {
window['$' + el] = function() {
return Element.extend(Builder.node.apply(Builder,
[el].concat(Array.prototype.slice.apply(arguments))));
};
});
And you end up with a $-prefixed function for each different type of
element, so you could do your above table something like this:
var table = $table({ id: 'myTable' }, {
$thead({
$tr({ $th('Title A'), $th('Title B') })
}),
$tbody({
$tr({ $td('Cell A'), $td('Cell B') })
})
});
Which I reckon makes it pretty readable. And with Builder.node()'s
flexibility with arguments you end up with a pretty powerful DOM
builder that requires a small amount of code and is easy to maintain.
That is of course if you're comfortable cluttering up the global
namespace with all these extra functions.
(btw, if you're going to use the above then you need to make sure it
is executed only after scripty's builder.js is loaded, so if you're
loading it the normal way you might need to do it after a dom:loaded
event).
-- Rod