1 hour ago · Tech · hide · 0 comments

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…

No comments yet. Log in to reply on the Fediverse. Comments will appear here.