Noteflakes - P2 - a Functional HTML Templating Engine for Ruby

submited by
Style Pass
2025-08-07 12:00:03

I’ve just released P2, a new HTML templating engine for Ruby. P2 builds on the work I did in Papercraft, but takes things quite a bit farther: templates are expressed as plain procs and are automatically compiled in order to make it fast. How fast?

Of course, Papercraft and P2 are not the only Ruby gems that allow expressing HTML using plain Ruby. Here’s a simple example of a template that will work in both Papercraft and P2:

This approach to writing HTML templates is very appealing (to me at least). I find that it is easy to write, easy to read, and also very easy to embed dynamic values in the produced HTML. But, this approach does have a cost: all those curly brackets - they denote blocks, and calling methods with blocks is kind of slow.

It’s not like ERB is super fast, but ERB templates are normally compiled to code that just stuffs strings and interpolated values into a buffer. Surely we can do the same with the above template. An ideal compiled version of the above template would look like the following:

Leave a Comment
Related Posts