4 ms·
I've written a similar library and I can heavily recommend the approach: https://github.com/judofyr/tubby https://github.com/judofyr/tubby. The main difference
by judofyr 2y ago
I've written a similar library and I can heavily recommend the approach: https://github.com/judofyr/tubby https://github.com/judofyr/tubby. The main difference between this and the other approaches (HTML_slice, Phlex) is that Tubby doesn't depend on inheritance or module inclusion. This means that it's slightly more verbose (you need to add "t." to each tag), but there's never any confusion of what `self` is nor any method collision. (For instance, looking at HTML_slice it appears that if you class defines the instance variable "@html_slice" weird things will happen.)
If you have a server-side rendered Ruby application with complicated view logic I can 100% recommend this approach (either Tubby, HTML_slice or Phlex). It's a super powerful way of structuring logic. You can create standalone classes which represents concepts (e.g. I've created a "ResourceCalendar" which renders a calendar view showing which resources are available for a given day), add methods which act on these, and finally make them renderable. It's heavily composable. It's very similar to React components, but here you also have the power of defining separate methods instead of having everything be hooks (with weird limitations) at the top of the component.
I also wrote a longer article here describing the different types of components you can build using Tubby: https://github.com/judofyr/timeless/blob/master/posts/composing-templates-with-tubby.adoc https://github.com/judofyr/timeless/blob/master/posts/compos.... I think this would also apply to HTML_slice and Phlex.