What if we'd had better HTML-in-JS syntax all along?
71–80 of 170 posts
Re: What if we'd had better HTML-in-JS syntax all along?
#72> I have a theory that a grave mistake was made in 1995 - the decision not to have a neat, succinct and declarative way of representing html elements in javascript. It may be interesting to recall that initially the only way to output HTML was by `document.write()` using strings (which was pretty declarative). The genuine way of generating HTML in JS were various methods of `String.prototype` for generating HTML form…
There was also the the idea that JavaScript was just one way of scripting the page. VisualBasic Script (VBScript) had momentum in IE releases. The general idea was that the user-agent was going to be able to parse multiple scripting languages, the list of which would probably grow as time went on. Think native TypeScript parsing in a browser today!
Re: What if we'd had better HTML-in-JS syntax all along?
#73 div class: "language-javascript" do
a href: "index.html" { img style: "height:2em", src: "pic.png"; "⇦"}
h1 {"What if we'd had better " + code{"html"} + "-in-" code{"js"} syntax all along?"}
p{"I have a theory that a grave mistake was made in 1995 ..."}
endRe: What if we'd had better HTML-in-JS syntax all along?
#74https://developer.mozilla.org/en-US/docs/Archive/Web/E4X
(Not quite "all along"/1995, but available by the early 2000s...)
Re: What if we'd had better HTML-in-JS syntax all along?
#75Don't forget E4X: https://developer.mozilla.org/en-US/docs/Archive/Web/E4X_tut... It never really caught on.
Re: What if we'd had better HTML-in-JS syntax all along?
#76Earlier quoted context omitted.
Wow. I'd never heard of this. And now instead we have virtually the same thing via JSX except we now require a compilation stage to achieve essentially the same thing...
I'm sure it was one of the inspirations for JSX -- https://facebook.github.io/jsx/#prior-art
Re: What if we'd had better HTML-in-JS syntax all along?
#77Re: What if we'd had better HTML-in-JS syntax all along?
#78Re: What if we'd had better HTML-in-JS syntax all along?
#79Suspiciously absent, but right up this alley is hyperscript: h('div', {id: 'foo', onclick: f}, 'Hello') The benefits of hyperscript over `div()` is that in JS, variable names must be imported by name (so `import {div, ul, li, ...}` gets annoying fast). Also, React is super popular, and hyperscript interoperates nicely with JSX.
Re: What if we'd had better HTML-in-JS syntax all along?
#80Something close to this is possible already in JS, I'm working on a library for generating HTML in node, heavily inspired by Haskell's blaze and lucid. Example code: div( { class: "table-responsive" }, table( { class: ["table table-sm", opts.onRowSelect && "table-hover"] }, thead(tr(hdrs.map(hdr => headerCell(hdr)))), tbody( vs.map(v => tr( mkClickHandler(opts, v), hdrs.map(hdr => td(typeof hdr.key === "string" ? tex…
The Re:DOM library might be of interest — it's very similar to your syntax above and it also comes with a robust toolkit for handling updates without the overhead of a virtual DOM: https://redom.js.org/#elements