Mithril manages similar functionality without any modifications to syntax.
Mithril manages similar functionality without any modifications to syntax.
Because the template is already so coupled with the view logic that you might as well put them in the same place.
And when we've reached that point, it make sense to make the template as legible as possible. Hence JSX.
Also, no one is forcing you to have the JSX and views in the same file. You could have them be separate, with a different file prefix (.jsx) for the template and not break any existing tooling.
And lastly, https://github.com/insin/msx (Mithril + JSX) is also a thing.
const links = ctrl.pages().map(page => <a href={page.url}>{page.title}</a>);
return <div> {links} <button onClick={ctrl.rotate}> Rotate Links </button> </div>;
Compared to
m("div", [
ctrl.pages().map(function(page) {
return m("a", {href: page.url}, page.title);
}),
m("button", {onclick: ctrl.rotate}, "Rotate links")
]);
I can see what the UI will be with JSX at a glance with Mithril I have to parse the code.edit: Mind you both look awful due to HNs poor formatting ability. Honestly how did this website become so popular with tech people?
What would make it "hard" to read? The fact that the third argument is the text?
That's just a tiny convention.
[:div
(for [p ctrl.pages]
[:a {:href p.url} p.title])
[:button {:onclick ctrl.rotate} "Rotate links"]]You mean use clojure(script) , don't be shy ...
React:
const links = ctrl.pages().map(page => <a href={page.url}>{page.title}</a>);
return <div> {links} <button onClick={ctrl.rotate}> Rotate Links </button> </div>;
Mithril: const links = ctrl.pages().map(page => m("a", {href: page.url}, page.title));
return m("div", [links, m("button", {onclick: ctrl.rotate}, "Rotate links")]);
There are JSX equivalents for Mithril too (for both Babel and standalone JSX).Mithril templates look also very good in CoffeeScript and similar languages.
Both of those snippets are intended to generate HTML. In that sense there's no question which one is easier to reason about/read.
I would argue the opposite — it is immediately easier to reason about, since I see what functions are called, and I can go look up the documentations for those functions without having to go through a precompile-step.
The rudimentary formatting keeps away the high-noise/low-signal folks who need shiny bells and whistles with their tech discussions.
The limitations of creating the view in JS seem invisible, yet visceral to us. I think the reason it is jarring is a fundamental expressivity problem of JS not being able to match templating logic of Handlebars like for-loops and conditionals, despite JS having these statement constructs.
An elegant templating solution is a result of Lisp's mixing of data and logic, something called Hiccup in the ClojureScript community, which I've written about here [1].
[1]: https://github.com/shaunlebron/jumping-from-html-to-clojures...
Handlebars is an application of JS (Node) for templating engines and so is Jade. Even though I still prefer Jade over Handlebars for mostly static/frontend oriented web apps.
For people big on JSX, can they tell us please what do they like most about this framework that they don't find equivalents for in Jade or Handlebars for example?
Jade/Handlebars don't map to React components.
I took a look at Hiccup. It looks interesting and promising but I'm an indentation oriented developer and that's why I favor Jade and Sass and the likes.
Also, I like the design philosophy that they chose with Jade by absorbing JS in HTML and not the other way around like in JSX where HTML & CSS being subordinates to JS thus making the process a whole lot more tedious and time-consuming.
> Jade can be used along with code too. I think that you mean view logic by code here as mixing business logic with your view is a big no no.
Code is data. Separating concerns by using different technologies to solve the same problem is not simplification. You can separate views from logic as you would separate different functions in code. We can organize that.
> I took a look at Hiccup. It looks interesting and promising but I'm an indentation oriented developer and that's why I favor Jade and Sass and the likes.
Not liking Hiccup because you prefer indentation doesn't make sense. Hiccup is indented by convention. If you're speaking about the delimiters, those are what keeps it from slipping into limitations shared by JS, Jade, etc.
> Also, I like the design philosophy that they chose with Jade by absorbing JS in HTML and not the other way around like in JSX where HTML & CSS being subordinates to JS thus making the process a whole lot more tedious and time-consuming.
You don't need to choose an absorption direction between JS and HTML if you treat HTML as what it is, a nested data tree, and realize that templating is the mixing of logic into this data tree. S-expressions in Lisp were designed exactly for this.
Especially with Handlebars it's always kind of tricky to debug, because there are all sorts of strange conventions going on, and it ends up being very hard to mentally parse.
With JSX (of course the same with Mithril) you get a data structure that is very easy to reason about.
The Mithril `m()` was designed to be used as is rather than as a serialization format.
It also understands `m("input.bar[type=date]")` which is handy, and the array for children elements is optional (`m(...)` is variadic) which makes it nicer in CoffeeScript-like languages.
m(".hello", 'world')
outputs {"tag":"div","attrs":{"className":"hello"},"children":["world"]}If you haven't written significant front-end code using JSX and React yet, give it a try. I make a case for JSX here: http://www.jasimabasheer.com/posts/evolution-of-view-templat.... There are also a few Medium posts with perspectives on JSX: https://medium.com/search?q=jsx.
I'm quite able to adjust, personally, and I've often represented HTML tags using method calls in a tasteful API, but I get complaints about why I'm not using a template language.