The view stack in Rails is one of the creakiest part of the stack. A lot of people don't realize it, but the way partials in Rails leaks the state of the parent renderer is the source of a lot of bugs and code organization issues.<p>It's great to see more attention coming to it. Some other libraries worth keeping an eye on:<p><a href="https://www.phlex.fun" rel="nofollow">https://www.phlex.fun</a> - Joel shipped this a few months ago ... it's very similar to Rux except it uses Ruby classes to achieve a similar effect. What's particularly interesting about Joel's efforts is that he is very worried about speed and performance, so it's a very fast templating framework.<p><a href="https://youtu.be/9-rqBLjr5Eo?t=560" rel="nofollow">https://youtu.be/9-rqBLjr5Eo?t=560</a> - This is the best overview of what Phoenix is shipping for their HTML framework, which is called HeeX (most of the docs already assume you're "in the know" with Elixir). HeeX is nice (like Rux) in that you can use HTML tags to embed server-side rendered components in markup with tags like `<.icon/>`. Not sure about Rux, but HeeX can reason about HTML from within itself, which means it can validate the DOM and make technologies like LiveView possible (<a href="https://hexdocs.pm/phoenix_live_view/Phoenix.LiveView.html" rel="nofollow">https://hexdocs.pm/phoenix_live_view/Phoenix.LiveView.html</a>).<p>I'm hoping at some point in the future a "de facto" (or actual) standard for Rails view components come into being so we can have an HTML-aware templating layer in Rails and improve the ecosystem through more standard UI components.
This is pretty cool, I like the idea of using inline templates in ViewComponent. I wonder how this would look using something like Markaby[1], so the templating stayed pure ruby, instead of having to be passed through a transpiler...<p>[1]: <a href="https://github.com/markaby/markaby">https://github.com/markaby/markaby</a><p>Modifying their own example, I kinda like it.<p><pre><code> class GreetingComponent
def call
div {
"Hey There" +
NameComponent(first_name: "Homer", last_name: "Simpson")
}
end
end</code></pre>
I didn't know about ViewComponent until now, it looks more appealing to me than Rux. Dealing with transpilers is a necessary evil in JS land, I really don't want to bring that headache into my Ruby code.
I'm surprised no one has yet done React SSR in Rails using Graal (using the polyglot bridge between Rails running on Truffleruby and React on Graal.js).
Does this support HAML-style syntax? We're 100% HAML-only for templating, whether normal Rails views or ViewComponent... <a href="https://github.com/haml/haml">https://github.com/haml/haml</a> <a href="https://haml.info/" rel="nofollow">https://haml.info/</a> so going back to writing HTML or ERB feels like a huge downgrade.
Rails has had Builder::XmlMarkup for a long time. It's been extracted into a gem: <a href="https://www.rubydoc.info/gems/builder/Builder/XmlMarkup" rel="nofollow">https://www.rubydoc.info/gems/builder/Builder/XmlMarkup</a><p>You could easily write a method to do this:<p><pre><code> def html(&blk)
io = StringIO.new()
builder = Builder::XmlMarkup.new(:target => io, :indent => 2)
blk.call(builder)
io.to_s
end
html do |t|
t.span "#{@first_name} #{@last_name}"
end
</code></pre>
Not the prettiest, but it doesn't require additional gems or a new syntax highlighter and linter.
Kind of interesting,i would like it if it had a cleaner fallback to a non JSX version. If you one day decide to not use rux<p>~~~
h('div.example', [<p><pre><code> h('h1#heading', 'This is hyperscript'),
h('h2', 'creating React.js markup'),
h(AnotherComponent, {foo: 'bar'}, [
h('li', [
h('a', {href: 'http://whatever.com'}, 'One list item')
]),
h('li', 'Another list item')
])
])
);</code></pre>
~~~
here's a couple more to throw into the mix.<p>Hypertext allows you to write HTML from Ruby.
<a href="https://github.com/soveran/hypertext">https://github.com/soveran/hypertext</a><p>rbexy - A Ruby template language inspired by JSX
<a href="https://github.com/patbenatar/rbexy">https://github.com/patbenatar/rbexy</a><p>imho, hypertext has a great deal of potential especially the DSL.
I've been building a Javascript server that implements Hotwire/Turbo lately powered by JSX templating (not React!) and minimal client-side JS. Its a wonderful dev experience but this might make me port those ideas back to Ruby. The rest of Rails is just too good to pass up.
The html inside the call method looks cool but I have hard time understanding to how the syntax highlighting would react to seeing html inside the ruby code? Would it highlight it correctly?