What if we'd had better HTML-in-JS syntax all along?
leontrolski.github.io
What if we'd had better HTML-in-JS syntax all along?
1–10 of 170 posts
Re: What if we'd had better HTML-in-JS syntax all along?
#2It never really caught on.
Re: What if we'd had better HTML-in-JS syntax all along?
#3> It would be constantly staring us in the face that our html is just structured data
Not only html, but css too.
Re: What if we'd had better HTML-in-JS syntax all along?
#4Don't forget E4X: https://developer.mozilla.org/en-US/docs/Archive/Web/E4X_tut... It never really caught on.
In the end we jumped from ES3 to ES5, skipping all of E4X. I wonder if we're better off.
Re: What if we'd had better HTML-in-JS syntax all along?
#5Don't forget E4X: https://developer.mozilla.org/en-US/docs/Archive/Web/E4X_tut... It never really caught on.
And now instead we have virtually the same thing via JSX except we now require a compilation stage to achieve essentially the same thing...
Re: What if we'd had better HTML-in-JS syntax all along?
#6[1]: https://www.oreilly.com/library/view/developing-web-applicat...
[2]: https://www.yesodweb.com/
Copied below to save you a click:
Hamlet (HTML)
$doctype 5
#{pageTitle} - My Site
#{pageTitle}
Here is a list of your friends:
$if null friends
Sorry, I lied, you don't have any friends.
$else
$forall Friend name age #{name} (#{age} years old)
^{copyright}
Cassius (CSS) #myid
color: #{red}
font-size: #{bodyFontSize}
foo bar baz
background-image: url(@{MyBackgroundR})
Lucius (CSS) section.blog {
padding: 1em;
border: 1px solid #000;
h1 {
color: #{headingColor};
}
}
Julius (JavaScript) $(function(){
$("section.#{sectionClass}").hide();
$("#mybutton").click(function(){document.location = "@{SomeRouteR}";});
^{addBling}
});
For those curious, the above is implemented in the metaprogramming framework of Template Haskell.Re: What if we'd had better HTML-in-JS syntax all along?
#7Don'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?
#8 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" ? text(v[hdr.key]) : hdr.key(v))
)
)
)
)
)
The first argument to a tag is optionally an object with the attributes, any subsequent arguments are children of the tag, or an array with the children.EDIT:
the library as a gist: https://gist.github.com/glutamate/69d38fb1d2024cb829edfff57d...
Re: What if we'd had better HTML-in-JS syntax all along?
#9Reminds me of the “Shakespearean Templates“ [1] from the Yesod web framework [2]. [1]: https://www.oreilly.com/library/view/developing-web-applicat... [2]: https://www.yesodweb.com/ Copied below to save you a click: Hamlet (HTML) $doctype 5 #{pageTitle} - My Site #{pageTitle} Here is a list of your friends: $if null friends Sorry, I lied, you don't have any friends. $else $forall Friend name age #{name} (#{age} years…
Re: What if we'd had better HTML-in-JS syntax all along?
#10Don't forget E4X: https://developer.mozilla.org/en-US/docs/Archive/Web/E4X_tut... It never really caught on.