93 days ago · Tech · hide · Fediverse post

Last year I made a version of ECR (the Crystal templating language) that allows for automatic escaping of values injected into an HTML template. This avoids having to litter your code with HTML.escape around everything that you put into the template. A little while after writing that post, I realised that it was a whole lot simpler if instead of using to_s to build my template, I created a new method, to_html. This got rid of the Builder and the unsafe_write and all that nonsense. Instead, a class can override to_html(io : IO) and write HTML content into the IO. If it don’t implement it, the default implementation will proxy to_s through an HTMLEscapingIO that calls HTML.escape on any data as it’s written. This made it easier to implement “partials” that can be placed into a larger template, just by creating objects that override to_html. You can see the new code on Codeberg. HTML-safe ECR is cool, but sometimes you just want to build a quick and dirty string, without making a…

Commenting on Bubbles has been removed. Why? If you want to talk about this post, write to the author, use the comments on their blog if it has them, or reply with a "RE:" post on your own blog. Suggestions? feedback@bubbles.town