Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions contrib/dyn_templates/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ workspace = true

[features]
tera = ["dep:tera"]
tera2 = ["dep:tera2"]
handlebars = ["dep:handlebars"]
minijinja = ["dep:minijinja"]

Expand All @@ -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]
Expand Down
16 changes: 9 additions & 7 deletions contrib/dyn_templates/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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,
];
Expand All @@ -109,7 +111,7 @@ impl Engines {
}

Some(Engines {
#[cfg(feature = "tera")]
#[cfg(any(feature = "tera", feature = "tera2"))]
tera: match inner::<Tera>(templates) {
Some(tera) => tera,
None => return None
Expand All @@ -133,7 +135,7 @@ impl Engines {
info: &TemplateInfo,
context: C,
) -> Option<String> {
#[cfg(feature = "tera")] {
#[cfg(any(feature = "tera", feature = "tera2"))] {
if info.engine_ext == Tera::EXT {
return Engine::render(&self.tera, name, context);
}
Expand All @@ -156,7 +158,7 @@ impl Engines {

/// Returns iterator over template (name, engine_extension).
pub(crate) fn templates(&self) -> impl Iterator<Item = (&str, &'static str)> {
#[cfg(feature = "tera")]
#[cfg(any(feature = "tera", feature = "tera2"))]
let tera = self.tera.get_template_names().map(|name| (name, Tera::EXT));

#[cfg(feature = "handlebars")]
Expand All @@ -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();

Expand Down
9 changes: 7 additions & 2 deletions contrib/dyn_templates/src/engine/tera.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -36,12 +39,14 @@ impl Engine for Tera {
}

fn render<C: Serialize>(&self, template: &str, context: C) -> Option<String> {
// 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()?;

Expand Down
6 changes: 6 additions & 0 deletions contrib/dyn_templates/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,19 @@
#![doc(html_favicon_url = "https://rocket.rs/images/favicon.ico")]
#![doc(html_logo_url = "https://rocket.rs/images/logo-boxed.png")]

#[cfg(all(feature = "tera", feature = "tera2"))]
compile_error!("features \"tera\" and \"tera2\" are mutually exclusive — enable only one");

#[macro_use] extern crate rocket;

#[doc(inline)]
#[cfg(feature = "tera")]
/// 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.
Expand Down