From 55518174a774aef7d0aaec84d35642ae2ac9bfb0 Mon Sep 17 00:00:00 2001 From: Bluexo <> Date: Sun, 23 Aug 2026 14:14:34 +0200 Subject: [PATCH 1/3] Added a feature "tera2" to use tera version 2 --- contrib/dyn_templates/Cargo.toml | 2 ++ contrib/dyn_templates/src/engine/mod.rs | 16 +++++++++------- contrib/dyn_templates/src/engine/tera.rs | 9 +++++++-- contrib/dyn_templates/src/lib.rs | 3 +++ 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/contrib/dyn_templates/Cargo.toml b/contrib/dyn_templates/Cargo.toml index 9a0eea923a..cf974a3fe5 100644 --- a/contrib/dyn_templates/Cargo.toml +++ b/contrib/dyn_templates/Cargo.toml @@ -17,6 +17,7 @@ workspace = true [features] tera = ["dep:tera"] +tera2 = ["dep:tera2"] handlebars = ["dep:handlebars"] minijinja = ["dep:minijinja"] @@ -26,6 +27,7 @@ notify = "7" normpath = "1" tera = { version = "1.19.0", optional = true } +tera2 = { package = "tera", version = "2.2.0", optional = true } handlebars = { version = "6.0", optional = true } [dependencies.minijinja] diff --git a/contrib/dyn_templates/src/engine/mod.rs b/contrib/dyn_templates/src/engine/mod.rs index b190a52bb9..4b213e62a7 100644 --- a/contrib/dyn_templates/src/engine/mod.rs +++ b/contrib/dyn_templates/src/engine/mod.rs @@ -5,10 +5,12 @@ use rocket::serde::Serialize; use crate::template::TemplateInfo; -#[cfg(feature = "tera")] +#[cfg(any(feature = "tera", feature = "tera2"))] mod tera; #[cfg(feature = "tera")] use ::tera::Tera; +#[cfg(feature = "tera2")] +use ::tera2::Tera; #[cfg(feature = "handlebars")] mod handlebars; @@ -69,7 +71,7 @@ pub struct Engines { /// This field is only available when the `tera` feature is enabled. When /// calling methods on the `Tera` instance, ensure you use types imported /// from `rocket_dyn_templates::tera` to avoid version mismatches. - #[cfg(feature = "tera")] + #[cfg(any(feature = "tera", feature = "tera2"))] pub tera: Tera, /// The Handlebars templating engine. @@ -93,7 +95,7 @@ pub struct Engines { impl Engines { pub(crate) const ENABLED_EXTENSIONS: &'static [&'static str] = &[ - #[cfg(feature = "tera")] Tera::EXT, + #[cfg(any(feature = "tera", feature = "tera2"))] Tera::EXT, #[cfg(feature = "handlebars")] Handlebars::EXT, #[cfg(feature = "minijinja")] Environment::EXT, ]; @@ -109,7 +111,7 @@ impl Engines { } Some(Engines { - #[cfg(feature = "tera")] + #[cfg(any(feature = "tera", feature = "tera2"))] tera: match inner::(templates) { Some(tera) => tera, None => return None @@ -133,7 +135,7 @@ impl Engines { info: &TemplateInfo, context: C, ) -> Option { - #[cfg(feature = "tera")] { + #[cfg(any(feature = "tera", feature = "tera2"))] { if info.engine_ext == Tera::EXT { return Engine::render(&self.tera, name, context); } @@ -156,7 +158,7 @@ impl Engines { /// Returns iterator over template (name, engine_extension). pub(crate) fn templates(&self) -> impl Iterator { - #[cfg(feature = "tera")] + #[cfg(any(feature = "tera", feature = "tera2"))] let tera = self.tera.get_template_names().map(|name| (name, Tera::EXT)); #[cfg(feature = "handlebars")] @@ -167,7 +169,7 @@ impl Engines { let minijinja = self.minijinja.templates() .map(|(name, _)| (name, Environment::EXT)); - #[cfg(not(feature = "tera"))] let tera = std::iter::empty(); + #[cfg(all(not(feature = "tera"), not(feature = "tera2")))] let tera = std::iter::empty(); #[cfg(not(feature = "handlebars"))] let handlebars = std::iter::empty(); #[cfg(not(feature = "minijinja"))] let minijinja = std::iter::empty(); diff --git a/contrib/dyn_templates/src/engine/tera.rs b/contrib/dyn_templates/src/engine/tera.rs index 33817c38f4..8a97391ed4 100644 --- a/contrib/dyn_templates/src/engine/tera.rs +++ b/contrib/dyn_templates/src/engine/tera.rs @@ -1,7 +1,10 @@ -use std::path::Path; +use std::{fs, path::Path}; use std::error::Error; +#[cfg(feature = "tera")] use tera::{Context, Tera}; +#[cfg(feature = "tera2")] +use tera2::{Context, Tera}; use rocket::serde::Serialize; use crate::engine::Engine; @@ -36,12 +39,14 @@ impl Engine for Tera { } fn render(&self, template: &str, context: C) -> Option { + // We only need this block for tera v1 + #[cfg(feature = "tera")] if self.get_template(template).is_err() { error!(template, "requested template does not exist"); return None; }; - let tera_ctx = Context::from_serialize(context) + let tera_ctx = Context::from_serialize(&context) .map_err(|e| error!("Tera context error: {}.", e)) .ok()?; diff --git a/contrib/dyn_templates/src/lib.rs b/contrib/dyn_templates/src/lib.rs index c2446aa4b8..433c20e8a5 100644 --- a/contrib/dyn_templates/src/lib.rs +++ b/contrib/dyn_templates/src/lib.rs @@ -187,6 +187,9 @@ /// The tera templating engine library, reexported. pub use tera; +#[cfg(feature = "tera2")] +pub use tera2 as tera; + #[doc(inline)] #[cfg(feature = "handlebars")] /// The handlebars templating engine library, reexported. From 722d9db7257f240a9307cfda3b1f05a309490a6a Mon Sep 17 00:00:00 2001 From: Bluexo <> Date: Sun, 23 Aug 2026 14:16:16 +0200 Subject: [PATCH 2/3] --amend --- contrib/dyn_templates/src/lib.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/contrib/dyn_templates/src/lib.rs b/contrib/dyn_templates/src/lib.rs index 433c20e8a5..1ac80e669f 100644 --- a/contrib/dyn_templates/src/lib.rs +++ b/contrib/dyn_templates/src/lib.rs @@ -180,6 +180,9 @@ #![doc(html_favicon_url = "https://rocket.rs/images/favicon.ico")] #![doc(html_logo_url = "https://rocket.rs/images/logo-boxed.png")] +#[cfg(all(feature = "v1", feature = "v2"))] +compile_error!("features \"v1\" and \"v2\" are mutually exclusive — enable only one"); + #[macro_use] extern crate rocket; #[doc(inline)] From 21fb317bbe7ff59d699732fcd3671467a327091b Mon Sep 17 00:00:00 2001 From: axoking <110609189+axoking@users.noreply.github.com> Date: Sun, 23 Aug 2026 16:46:44 +0200 Subject: [PATCH 3/3] Fix wrong feature names in lib.rs Co-authored-by: Peter W --- contrib/dyn_templates/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contrib/dyn_templates/src/lib.rs b/contrib/dyn_templates/src/lib.rs index 1ac80e669f..9119543b30 100644 --- a/contrib/dyn_templates/src/lib.rs +++ b/contrib/dyn_templates/src/lib.rs @@ -180,8 +180,8 @@ #![doc(html_favicon_url = "https://rocket.rs/images/favicon.ico")] #![doc(html_logo_url = "https://rocket.rs/images/logo-boxed.png")] -#[cfg(all(feature = "v1", feature = "v2"))] -compile_error!("features \"v1\" and \"v2\" are mutually exclusive — enable only one"); +#[cfg(all(feature = "tera", feature = "tera2"))] +compile_error!("features \"tera\" and \"tera2\" are mutually exclusive — enable only one"); #[macro_use] extern crate rocket;