-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Added closure to configure template engines #431
Conversation
Can you remove all of the style changes? We don't use 'rustfmt' since it doesn't quite work well enough yet. I can review the PR once only functional changes are present. |
Signed-off-by: Ning Sun <[email protected]>
@SergioBenitez disabled my |
contrib/src/templates/context.rs
Outdated
@@ -49,6 +49,10 @@ impl Context { | |||
Context { root, templates, engines } | |||
}) | |||
} | |||
|
|||
pub fn engines_mut(&mut self) -> &mut Engines { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this method.
contrib/src/templates/engine.rs
Outdated
@@ -69,4 +69,14 @@ impl Engines { | |||
|
|||
None | |||
} | |||
|
|||
#[cfg(feature = "handlebars_templates")] | |||
pub fn handlebars(&mut self) -> &mut Handlebars { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this method.
contrib/src/templates/engine.rs
Outdated
} | ||
|
||
#[cfg(feature = "tera_templates")] | ||
pub fn tera(&mut self) -> &mut Tera { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this method.
contrib/src/templates/mod.rs
Outdated
/// # ; | ||
/// } | ||
/// ``` | ||
pub fn config_and_fairing<F>(f: F) -> impl Fairing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Name this method custom
.
contrib/src/templates/mod.rs
Outdated
/// } | ||
/// ``` | ||
pub fn config_and_fairing<F>(f: F) -> impl Fairing | ||
where F: Fn(&mut Context) + Send + Sync + 'static |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pass in an &mut Engines
instead. Make the fields of the Engines
struct public.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, why does F
have to be Send + Sync + 'static
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it Adhoc::attach
requested. It won't compile if F is not marked Send + Sync + 'static
.attach(Template::config_and_fairing(|ctxt| { | ||
ctxt.engines_mut() | ||
.handlebars() | ||
.register_helper("test", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need a real test here. Create a helper than does something, and use it in one of the templates. Ensure that the returned value is what's expected.
@@ -46,6 +47,27 @@ fn rocket() -> rocket::Rocket { | |||
.catch(errors![not_found]) | |||
} | |||
|
|||
// when you want to register handlebars helpers | |||
#[allow(dead_code)] | |||
fn rocket_with_hbs_helper() -> rocket::Rocket { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't create a function that's not used. Instead, perhaps you can name this function something like:
fn install_helper(handlebars: &mut Handlebars) { .. }
and then call it from rocket()
:
.attach(Template::custom(|engines| install_helper(&mut engines.handlebars)));
contrib/src/lib.rs
Outdated
@@ -74,3 +74,9 @@ mod uuid; | |||
|
|||
#[cfg(feature = "uuid")] | |||
pub use uuid::{UUID, UuidParseError}; | |||
|
|||
#[cfg(feature = "handlebars_templates")] | |||
pub extern crate handlebars; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move these extern crate
s to the top of the file below the existing extern crate
s.
Signed-off-by: Ning Sun <[email protected]>
Ok(()) | ||
})); | ||
|
||
})) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just changed default rocket()
to use custom
Signed-off-by: Ning Sun <[email protected]>
Please see the build failure. |
@SergioBenitez it was |
Signed-off-by: Ning Sun <[email protected]>
Signed-off-by: Ning Sun <[email protected]>
So sorry for the delay here! As we spoke about briefly in person, I think the API for Here's the signature I'm imagining for pub fn show<S, C>(rocket: &Rocket, name: S, context: C) -> Option<String>
where S: Into<Cow<'static, str>>, C: Serialize
{ The big change is that What do you think? |
Just a heads up that #500 also seeks to implement this. |
Currently there is no way to access template engine, for example, adding custom helper to handlebars.
This patch added a closure to
Template::fairing
which allows a closure to configure those engines.