djc / askama

Type-safe, compiled Jinja-like templates for Rust
Apache License 2.0
3.25k stars 213 forks source link

Feature/docs request: allow setting escape="none" in askama.toml #1060

Open sxlijin opened 1 month ago

sxlijin commented 1 month ago

We're using askama for codegen (thanks for the wonderful work!) and all of our templates are instantiated with #[template(path = ..., escape = "none)]. It would be nice if we could just set a blanket escape = "none" in askama.toml.

I suspect that, given askama supports custom escapers, which are much more complex, this is possible - but looking at https://djc.github.io/askama/configuration.html#custom-syntaxes I have no idea how to actually do this.

https://github.com/djc/askama/blob/34f84dc10f291a6bd7fc3af2c01da20c9ccd51e5/askama_derive/src/config.rs#L302-L306 suggests that default_escapers is a 2-tuple of file-extension-list to rust-escaper-function - but it's unclear to me how this would be modeled in askama.toml.

djc commented 1 month ago

I think you could do it like this?

[[escaper]]
path = "::askama::Text"
extensions = ["rs"] # Add whatever extensions you need here

Internally, escapers is a Vec<(HashSet<String>, String)>, where the HashSet represents a set of extensions, and the Vec is searched in order with configured escapers coming before the default escapers.

Please try it, and if it works it would be great if you can submit a docs PR!

sxlijin commented 2 weeks ago

Thanks for the quick reply, and sorry for the slow response- this is very far off the daily work track, so it's hard to make time for this.

Unfortunately this doesn't appear to work; when using this with a client.ts.j2 template:

❯ g d ../../engine/language-client-codegen/askama.toml
diff --git a/engine/language-client-codegen/askama.toml b/engine/language-client-codegen/askama.toml
index 8c040ea1..2d296484 100644
--- a/engine/language-client-codegen/askama.toml
+++ b/engine/language-client-codegen/askama.toml
@@ -7,3 +7,7 @@ dirs = [
 # whitespace can be either preserve, suppress, or minimize
 # suppress and minimize are both too aggressive for us
 whitespace = "preserve"
+
+[[escaper]]
+path = "::askama::Text"
+extensions = ["j2"]

I get this:

❯ g d baml_client/client.ts
diff --git a/integ-tests/typescript/baml_client/client.ts b/integ-tests/typescript/baml_client/client.ts
index 4d777d02..5041c9f2 100644
--- a/integ-tests/typescript/baml_client/client.ts
+++ b/integ-tests/typescript/baml_client/client.ts
@@ -19,6 +19,8 @@ import { BamlRuntime, FunctionResult, BamlCtxManager, BamlStream, Image } from "
 import {Blah, ClassOptionalOutput, ClassOptionalOutput2, ClassWithImage, DynInputOutput, DynamicClassOne, DynamicClassTwo, DynamicOutput, Education, Email, Event, FakeImage, InnerClass, InnerClass2, NamedArgsSingleClass, OptionalTest_Prop1, OptionalTest_ReturnType, OrderInfo, Person, RaysData, ReceiptInfo, ReceiptItem, Resume, SearchParams, SomeClassNestedDynamic, TestClassAlias, TestClassNested, TestClassWithEnum, TestOutputClass, UnionTest_ReturnType, WithReasoning, Category, Category2, Category3, Color, DataType, DynEnumOne, DynEnumTwo, EnumInClass, EnumOutput, Hobby, NamedArgsSingleEnum, NamedArgsSingleEnumList, OptionalTest_CategoryType, OrderStatus, Tag, TestEnum} from "./types"
 import TypeBuilder from "./type_builder"

+&lt;div&gt;hello&lt;/div&gt;
+
 export type RecursivePartialNull<T> = T extends object
   ? {
       [P in keyof T]?: RecursivePartialNull<T[P]>;

If I find time to drill into a minimum repro, I'll let you know.

(Aside: I really hate the lack of template filename suffix standardization. I've personally standardized every codebase I use on filename.$generated-ext.j2 but this seems... not so common?)