00:00:04.560
How is everyone? Okay, are you all good? I will need a little bit of audience participation because today we will talk about Flex and more. Let's talk about ERB, about rendering pretty things with HTML.
00:00:11.160
Most of us write a lot of HTML, and I'm wondering, who here is really in love with it? Are there a few helpers in Ruby on Rails? Can I hear some enthusiasm? Who is in love with ERB? A bit more enthusiasm, please! Who is in love with Ruby and Rails?
00:00:24.680
Exactly! I feel there's a lot of enthusiasm about Rails and Ruby, but not so much when it comes to the view layer. That was the same for me until I found the Flex framework. So, what is Flex? Flex is a way to write HTML with Ruby to generate HTML. We get this basic class called Flex HTML.
00:00:47.280
Let’s build something renderable, like a list that will inherit from the Flex HTML base class. To get content in there, you respond with a template instance method in this class. We have wonderful methods like UL, label, div, and article. For every HTML tag, there is a corresponding Ruby method that accepts keyword arguments for the attributes and a block to hold the content. This way, we now have this pretty class with two wonderful attributes.
00:01:31.759
You might think, 'Yes, I could have written that in HTML!' I completely agree. It only really shows its strength when we take it and make it Ruby. Once we have that, we can extract methods, use inheritance, create entire DSLs, and express our views that can be very dynamic.
00:01:50.240
For example, let me show you a quick ERB example from Ruby’s chs.org search show page. There's a lot going on with HTML tags and conditionals; it’s actually hard to orient yourself. If we express that with a Flex view, we can extract methods for different parts of the view. For instance, there's an error message that we can isolate into its own method, resulting in a much clearer structure.
00:02:56.840
In this example, we have a nested conditional. If there's a Check, we show the search results, otherwise, we show the suggestion results. Here, we separate the logic of the view into two, and the actual HTML parts are generated in private methods. Another example is when I think of tables: I think in columns rather than rows and headers.
00:03:38.080
With Flex components, it's possible to create something that resembles a table, and you call a column method where you define the specifics, like a column name for products along with a description. You can do that with Flex, and it allows for greater abstraction.
00:04:10.840
Flex gives you the ability to abstract as much, or as little, as you want. You can go to the extreme and create a form object that not only validates and saves something but can also render itself. The template can indicate which fields to permit parameters for.
00:04:35.080
So, Flex is really a lot of fun! It's not only performant but also very fast. I encourage you to explore it and have fun with Flex. If you want to find more about it, the address is flex.fun, where you can find the documentation. Thank you very much!