minijinja icon indicating copy to clipboard operation
minijinja copied to clipboard

Custom block tags

Open jameysharp opened this issue 1 year ago • 10 comments

Jinja2 supports extensions that implement custom block tags. I'd like to use minijinja to render existing templates that use extension tags. But at least for my purposes there are several good-enough alternatives that are less complicated than Jinja2's full extensible parsing.

The tags I currently care about just minify their rendered contents in various ways, so for a first cut it's sufficient to just ignore the tags and pass through the contents unminified. My plan in the absence of any support from this library is to search for the relevant tags in the template source text and delete them before passing them to minijinja.

But I wondered if it's worth discussing various ways minijinja could provide more support for extensions. Here are a few options that I think would work for me and might help others:

  • Allow associating a pair of block tags (e.g. compress/endcompress or spaceless/endspaceless) with a filter that implements the intended string->string transformation on the rendered contents between the two tags. This doesn't require fully general extensible parsing but still supports more unmodified Jinja2 templates.

  • Make the lexer public and allow preprocessing the stream of tokens before they're parsed. This allows embedders to implement the above filter-based transformation themselves, but fixes some implementation details as public API.

  • Or, of course, make the full AST public and provide all the hooks Jinja2 extensions have today. I'm guessing it's too soon in minijinja's development to commit to that much public API, but I'm including it for completeness.

jameysharp avatar Jan 28 '23 20:01 jameysharp