Return Safe Value Strings from Filter in Rust's MiniJinja
I've got some filters that do syntax highlighting in my personal static site builderssb. They look like this:
[! filter highlight_html|safe !]
<div>the quick brown fox</div>
[! endfilter !]Without applying |safe to the filter each time the output gets double escaped (i.e. the tags that do the highlighting are visible in the output instead of doing the actual highlighting).
The reason is that my original filters were returning String content. By using a Value instead that's created with Value::from_safe_string() the need to add |safe goes away.
Here's an example of how things work:
---
anyhow = "1.0.100"
minijinja =
---
use Result;
use ;
use SyntaxConfig;
The example input is already escaped.
Running this filter without safe results in
douple escaping:
&amp;lt; div &amp;gt;
The same filter can be run with `|safe`
so that only single escaping occurs
&lt; div &gt;
Or, the filter that uses `Value::from_safe_string()`
can be used without resuqing `|safe`
to be applied each time the filter is used.
&lt; div &gt;
It's a nice little quality of life improvement MiniJinja continues to impress.
- a
Endnotes
This is for my personal static site builder.
It's super customized to my workflow. You can check out the source code if you're curious about how it works.
References
The bad ass rust templating engine.