martenframework / marten

The pragmatic web framework.
https://martenframework.com
MIT License
405 stars 23 forks source link

Add a template filter to escape string values #110

Closed ellmetha closed 9 months ago

ellmetha commented 1 year ago

Description

Let's introduce a new escape template filter allowing to escape string values in templates. This new filter should probably make use of the HTML#escape method.

For example:

{{ my_var|escape }}

If my_var was set to <b>Let's do it</b>, then the output of the above template would be &lt;b&gt;Let&#39;s do it&lt;/b&gt;.

Special care should be taken to ensure that we don't end up applying multiple rounds of escaping to string values that would've been auto-escaped by Marten's auto-escaping mechanism. To avoid that, string values escaped with the new escape filter should probably be converted to safe string objects.