Live data from Hacker News

Laconic: A Sane Method of Generating DOM Content in JavaScript

joestelmach.github.com

11–20 of 51 posts

Re: Laconic: A Sane Method of Generating DOM Content in JavaScript

#11
post #5

I simply generate global functions corresponding to tags, so my stuff looks like this: div(span('hello world')).inject(document.body); Very simple to do and even simpler to use.

Many would frown upon polluting the global name space with functions corresponding to all known HTML tags.

If you end up wrapping your code in an immediately called lambda anyhow I'm sure it would be fine.

Re: Laconic: A Sane Method of Generating DOM Content in JavaScript

#12
post #10

This is pretty, but unfortunately the only performant way to generate dom elements in javascript is with HTML. HTML parsing is orders of magnitude faster in most browsers. How about a library like this to generate a string of HTML instead of dom elements?

Seems to me like an implementation detail that shouldn't have to affect the interface at all.

Re: Laconic: A Sane Method of Generating DOM Content in JavaScript

#13

This is really neat. The coolest part is being able to register your own "tags" which take any arguments you want. The problem I see is that this only works for "all known HTML tags." If you want to use custom tags, like (I believe that's what meteor uses), this won't cut it since it uses $.el.tagname syntax, every tag has to be loaded into the $.el object. How about a very similar, more jQuery-like syntax instead: $…

Though I did a poor job at documenting it, $.el is actually a function that accepts a tag name as the first parameter. The following two snippets are equivalent: $.el.div({className : 'foo'}); $.el('div', {className : 'foo'});

That's awesome! Yeah I see it now in the source's comments. Thanks for pointing that out; I'm sold.

Re: Laconic: A Sane Method of Generating DOM Content in JavaScript

#14
post #10

This is pretty, but unfortunately the only performant way to generate dom elements in javascript is with HTML. HTML parsing is orders of magnitude faster in most browsers. How about a library like this to generate a string of HTML instead of dom elements?

Like a template engine?

Re: Laconic: A Sane Method of Generating DOM Content in JavaScript

#15
post #10

This is pretty, but unfortunately the only performant way to generate dom elements in javascript is with HTML. HTML parsing is orders of magnitude faster in most browsers. How about a library like this to generate a string of HTML instead of dom elements?

While I don't entirely agree with your first statement, you could just grab the outerHTML instead of appending the element directly to the DOM:

  $.el.div($.el.span()).outerHTML

Re: Laconic: A Sane Method of Generating DOM Content in JavaScript

#16
post #10

This is pretty, but unfortunately the only performant way to generate dom elements in javascript is with HTML. HTML parsing is orders of magnitude faster in most browsers. How about a library like this to generate a string of HTML instead of dom elements?

Like a template engine?

Perhaps like some template engine, but not like any I know of.

Re: Laconic: A Sane Method of Generating DOM Content in JavaScript

#19
post #10

This is pretty, but unfortunately the only performant way to generate dom elements in javascript is with HTML. HTML parsing is orders of magnitude faster in most browsers. How about a library like this to generate a string of HTML instead of dom elements?

While I don't entirely agree with your first statement, you could just grab the outerHTML instead of appending the element directly to the DOM: $.el.div($.el.span()).outerHTML

Seems like a reasonable solution.

My first statement was certainly true in the previous generation of browsers, including IE 7. I'm not sure about more modern browsers. What basis do you have for disagreeing?

Refer to:

http://www.quirksmode.org/dom/innerhtml.html

Re: Laconic: A Sane Method of Generating DOM Content in JavaScript

#20

i don't understand the appeal of this when there's tons of client side js templating options (handlebars, jade, haml-js,...) that result in far more readable code than a $.el. prefixathon

While I agree the $.el prefix is annoying, I don't believe it has a negative effect on readability. For me, templates are just another level of unneccesary indirection.
Post reply on HN