3 ms·
An interesting native approach; there have been library-based approaches to this that date back a while. Laconic (https://github.com/joestelmach/laconic https:
by andrewla 6y ago
An interesting native approach; there have been library-based approaches to this that date back a while.
Laconic (https://github.com/joestelmach/laconic https://github.com/joestelmach/laconic) is one of the oldest that I'm aware of, and suffers from some jsquery-ness:
$.el.div({'class' : 'example'},
$.el.div('content'));
Pithy (https://github.com/caolan/pithy https://github.com/caolan/pithy) is another; uses function syntax to get the effect of the tag name. This was discussed previously on hn, https://news.ycombinator.com/item?id=5486239 https://news.ycombinator.com/item?id=5486239
html.div('#main', [
html.h1(null, 'Hello, world!'),
html.img({src: 'foo.jpg'})
]);
In that discussion, there are various other implementations of similar ideas - https://github.com/markgandolfo/el.js https://github.com/markgandolfo/el.js and https://github.com/insin/DOMBuilder https://github.com/insin/DOMBuilder and https://github.com/jed/domo https://github.com/jed/domo and so on.
el.js came to my attention a long time ago, and I ended up writing a clone that I have used in my personal projects since the library almost writes itself once you steal the basic idea.
Using function application is the easiest way to get the effect of TFA's syntax without the cumbersome overloading of braces.
- insin 6y agoHow far back can we go? insin/DOMBuilder was initially based on a 2006 library of the same - but differently-cased - name by Dan Webb (https://www.webstandards.org/2006/04/13/dom-builder https://www.webstandards.org/2006/04/13/dom-builder) and the earliest variant I can recall was Mochikit.DOM (https://mochi.github.io/mochikit/doc/html/MochiKit/DOM.html https://mochi.github.io/mochikit/doc/html/MochiKit/DOM.html) By the end of DOMBuilder's life it could generate DOM or HTML from the same code and I'd tried unsuccessfully to implement a way to rehydrate HTML by hooking up events using the same code on the client. Not long after, I happened to be in the audience for Pete Hunt's React: Rethinking best practices talk at JSConf EU (https://www.youtube.com/watch?v=x7cQ3mrcKaY https://www.youtube.com/watch?v=x7cQ3mrcKaY) and I was totally floored; they'd solved everything I had been trying to do with UI as code and much more, and JSX was the icing on the cake, as using nested object/array syntax has always been rife with comma-management hell when you're maintaining UI code.