tera icon indicating copy to clipboard operation
tera copied to clipboard

Allow for hooking a closure that alters the final rendered String.

Open Directory opened this issue 3 years ago • 0 comments

there's a feature that i would love to be implemented in v2. in issue #101 it was mentioned that for minification/optimization it should be left to external 3rd party crates to handle that. ok. how? i would love for a function to be added to the Tera type where you can hook/register a closure that takes the rendered string and allows you to alter it.

so for example lets say tera renders index.html. if the result is Ok and an html/xml string was generated the registered closure will take that string and whatever it returns (like the output of minify_html::minify()) will be the actual returned value of whatever is rendered. the api i had in mind is something like this

use minify_html;

let tera_instance: tera::Tera; // todo initialize 
tera_instance.register_proxy_after(|html: String| -> String {
   minify_html::minify(html.into(), ..).into()
});

maybe even a register_proxy_before function for altering a tera template string before it gets parsed although that's scraping the barrel.

the reason why i would like this is because im using tera in rocket. rocket (or rocket_dyn_templates technically) exposes a mutable reference to Tera when attaching Template::custom(). with this instance exposed here the hypothetical register_proxy_after function can be ran on the Tera instance. And that way in my rocket endpoints i can continue returning the built in Template type from Template::render() and not have to make my own responder type that manually has to check if the template was successful to minify the output of.

Directory avatar Jun 16 '22 09:06 Directory