Will phlex overthow haml and erb?
If you are looking for a fast and easy way to render views in ruby-on-rails, you might want to check out phlex. Traditionally, the view is the slowest step in most ruby on rails projects. Phlex is a new view engine that is much faster than the traditional HAML or ERB. I decided to give it a try for my project MeritFront, and I was impressed by the results. Not only did phlex reduce the render time significantly, but it also simplified the syntax and improved the readability of my views.
First look:
Lets look at a (simplified) strip of haml code.
And now lets look at it in phlex.
The first thing that you will notice is that the phlex code is longer. This is mainly because:
- It is including object oriented features (‘initialize’, ‘class’, ‘module’). This becomes less relevant as most views are larger.
- it has end statements ('}', 'end')
That being said, if your code is largely static, haml might be a good choice. In my experience though, I would rather have those more dynamic features.
Its Functional:
In the haml example, you can see some unusual statements such as @view_flow, content_for, ‘render partial’, and yield. These are workarounds for the limitations of haml, which is neither object oriented nor functional.
In contrast, the phlex example simply uses a function.
Phlex offers many advantages over haml for writing view code. Since phlex is pure ruby code, you can benefit from:
- modules
- subclassing
- functions
- tools like syntax highlighting (which helped me find many unused variables in my haml during conversion)
- and required arguments
That last feature is especially useful. With phlex, you know exactly what arguments to pass. With haml, you don’t know, and they are left undefined if you miss them. Having functionality like this leads to a super robust code-base (which is much easier to test)! Why write haml when you can just keep writing ruby?
Its easy to use:
Phlex is written in ruby. Thats it. You do not have to learn haml. You don’t have to learn what different symbols mean (‘%’, ‘.’, ‘-’, ‘=’) and you don’t have to worry about indentation. Most importantly, you are not constantly switching contexts. Imagine being new to rails. At the end of the day, haml is just another thing to learn. And if you have a mainly front end focus, haml can even lead you to avoid learning ruby almost entirely. If you are trying to work as a full stack developer, or you want at least some exposure past the view layer - then you should use phlex.
Phlex is ruby code, but most importantly, it still looks like html. Its the best of both worlds, and a great way to bridge the gap between front end and back end skill sets.
One reason that one might argue for haml is to enforce separation between the view layer of the application from the model and controller. That is a core tenant of the MVC design pattern (Model View Controller). There are definitely good reasons for the MVC model! I am not saying there are not. Generally, database tasks should be done in the database, rendering should be done in the view, and requests should be handled in the controller. But... In my experience at least, that line is much blurrier than people want to admit. The model is called in view code all the time. Whether you are retrieving a list of things to render, quickly checking a setting, or some other task, it happens! Why make it difficult for yourself?
Speed:
Changing to different technologies is always a little of a chore, especially if you have to transition pre-existing code. There has to be some immediate benefits in order to make that transition. So how fast is Phlex? Looking at some old logs and comparing, its about 1.5 – 2 times as fast. Of course this is a super rough estimation, but its a rough question. It all depends on the particular code you are running. So results may vary! You can expect even more speedups if you are implementing yjit and other newer technologies coming out.
Side Note:
I am not referencing erb here because it is very similar to haml but it looks like this:
take that as you will 🤷♂️
Thanks for reading! Please feel free to edit this article (especially if you have any timing data), I will be getting alerts for any rivals so I will definitely take a look! Also check out my haml2phlex gem!
Hot comments
about anything