-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support minijinja in 'dyn_templates'.
- Loading branch information
1 parent
0edbb6d
commit 8614a7f
Showing
17 changed files
with
781 additions
and
529 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...dyn_templates/src/handlebars_templates.rs → ...ib/dyn_templates/src/engine/handlebars.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use std::sync::Arc; | ||
use std::path::Path; | ||
use std::collections::HashMap; | ||
|
||
use rocket::serde::Serialize; | ||
use minijinja::{Environment, Error, ErrorKind, AutoEscape}; | ||
|
||
use crate::engine::Engine; | ||
|
||
impl Engine for Environment<'static> { | ||
const EXT: &'static str = "j2"; | ||
|
||
fn init<'a>(templates: impl Iterator<Item = (&'a str, &'a Path)>) -> Option<Self> { | ||
let _templates = Arc::new(templates | ||
.map(|(k, p)| (k.to_owned(), p.to_owned())) | ||
.collect::<HashMap<_, _>>()); | ||
|
||
let templates = _templates.clone(); | ||
let mut env = Environment::new(); | ||
env.set_loader(move |name| { | ||
let Some(path) = templates.get(name) else { | ||
return Ok(None); | ||
}; | ||
|
||
match std::fs::read_to_string(path) { | ||
Ok(result) => Ok(Some(result)), | ||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None), | ||
Err(e) => Err( | ||
Error::new(ErrorKind::InvalidOperation, "template read failed").with_source(e) | ||
), | ||
} | ||
}); | ||
|
||
let templates = _templates.clone(); | ||
env.set_auto_escape_callback(move |name| { | ||
templates.get(name) | ||
.and_then(|path| path.to_str()) | ||
.map(minijinja::default_auto_escape_callback) | ||
.unwrap_or(AutoEscape::None) | ||
}); | ||
|
||
Some(env) | ||
} | ||
|
||
fn render<C: Serialize>(&self, name: &str, context: C) -> Option<String> { | ||
let Ok(template) = self.get_template(name) else { | ||
error_!("Minijinja template '{name}' was not found."); | ||
return None; | ||
}; | ||
|
||
template.render(context) | ||
.map_err(|e| error_!("Minijinja: {}", e)) | ||
.ok() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
contrib/dyn_templates/src/tera_templates.rs → contrib/dyn_templates/src/engine/tera.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.