Skip to content

Add Tera v2 support - #3010

Open
axoking wants to merge 3 commits into
rwf2:masterfrom
axoking:MigrateToTeraV2
Open

Add Tera v2 support#3010
axoking wants to merge 3 commits into
rwf2:masterfrom
axoking:MigrateToTeraV2

Conversation

@axoking

@axoking axoking commented Aug 23, 2026

Copy link
Copy Markdown

I added a feature tera2 which will use tera v2 instead of the deprecated v1. I left the old tera feature working for compatibility.

An if condition that checks if the template exists could be omitted, because tera 2 early returns before that function is even called if the template does not exist.

I did not write any documentation or testing, because I'm not familiar with that. But I tested both tera and tera2 with a seperate crate I wrote, and they work and render using the correct tera version.

Comment thread contrib/dyn_templates/src/lib.rs Outdated
Co-authored-by: Peter W <peter.wischer@gmail.com>
@axoking

axoking commented Aug 27, 2026

Copy link
Copy Markdown
Author

I just realized that this also breaks tera autoescaping for some reason.

I understand that the .html.tera files are not auto-escaped on version 2, because they are registered to tera without their file extension. However, I don't understand why this wasn't a problem with tera v1.

@martynp

martynp commented Aug 28, 2026

Copy link
Copy Markdown

@axoking Thanks for the PR!

It wasn't an issue with Tera v1 because it had a fallback to look for the files the extensions - this functionality is different in v2.

A few points to note:

I have integrated a tera_v2 feature into github.com/rustfoo/rkt with the above fixed. I have some more revisions to make before making it public - for rkt I am contemplating making the tera feature use tera_v2 and tera_v1 point to the now deprecated features.

@axoking

axoking commented Aug 28, 2026

Copy link
Copy Markdown
Author

@martynp Thank you!

So you got a solution for auto escaping? I'm also currently looking into the tera crate to see if you could add auto escaping functionality there, e.g. getting the extension from the file path, or giving other crates an option to manually enable auto-escaping on files (maybe this is already an option, I will have a look later)

@martynp

martynp commented Aug 29, 2026

Copy link
Copy Markdown

@axoking Yes — it's working in rustfoo/rkt now.

Rather than change how templates are registered I kept the names and made the escape list explicit: pick out the escapable templates by their source path and pass their names to autoescape_on().

@axoking

axoking commented Aug 29, 2026

Copy link
Copy Markdown
Author

Ok nice! I will check that out

I'm also working on the autoescaping stuff here: Keats/tera#1028

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants