djc / askama

Type-safe, compiled Jinja-like templates for Rust

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add support for filter section

GuillaumeGomez opened this issue · comments

Would you be open to add support for filter section?

So, this filter first collects all produced text into a buffer, e.g. String, then the collected data is passed to the filter? How does this work with escaped data?

{% filter upper %}
{{ "<script>" }}
{% endfilter %}

&LT;SCRIPT&GT;

because "<script>" gets escaped eagerly? Or

&lt;SCRIPT&gt;

because the escaping is only done after the whole section was processed?

Could you tell me how you want to use a filter section?

I suppose that if we are in "html mode", we want it to be escaped indeed. So generated code would actually look like:

let _x = escape_html(format!("<script>"));

I need it for the tera to askama migration in docs.rs. Examples:

I feel like this has come up before, but I can't find any prior issues discussing it.

What alternatives have you come up with?

I don't like it that much, on account of the idea that I don't feel like the code generated it for it is conceptually obvious. But, I can't think of a strong reason to reject it, either... I notice that your use cases are relying on filters we haven't implemented yet (dedent and highlight), do you have have an implementation for those already?

Yes we already have them. Conceptually, I see it as very close as what we do for the rest in general: we generate a format! (in a sub-block would be best I think) and call filters on it and "return" the result of all this that we put either in a variable that will be used later on.

Okay, I'm open to reviewing changes for this. I assume we'll collect, filter, then escape as @Kijewski suggested.