Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a template registration callback #500

Closed
Closed
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
4 changes: 2 additions & 2 deletions contrib/src/templates/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ pub trait Engine: Send + Sync + 'static {

pub struct Engines {
#[cfg(feature = "tera_templates")]
tera: Tera,
pub tera: Tera,
#[cfg(feature = "handlebars_templates")]
handlebars: Handlebars,
pub handlebars: Handlebars,
}

impl Engines {
Expand Down
12 changes: 10 additions & 2 deletions contrib/src/templates/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use self::glob::glob;

use std::borrow::Cow;
use std::path::{Path, PathBuf};
use std::marker::{Send, Sync};
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, this isn't alphabetical


use rocket::State;
use rocket::request::Request;
Expand Down Expand Up @@ -158,7 +159,11 @@ impl Template {
/// }
/// ```
pub fn fairing() -> impl Fairing {
AdHoc::on_attach(|rocket| {
Template::fairing_with(|_context| ())
}

pub fn fairing_with<F>(callback: F) -> impl Fairing where F: Fn(&mut Context) + Send + Sync + 'static {
AdHoc::on_attach(move |rocket| {
let mut template_root = rocket.config()
.root_relative(DEFAULT_TEMPLATE_DIR);

Expand All @@ -172,7 +177,10 @@ impl Template {
};

match Context::initialize(template_root) {
Some(ctxt) => Ok(rocket.manage(ctxt)),
Some(mut context) => {
callback(&mut context);
Ok(rocket.manage(context))
},
None => Err(rocket)
}
})
Expand Down