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

Added closure to configure template engines #431

Closed
wants to merge 5 commits into from
Closed

Added closure to configure template engines #431

wants to merge 5 commits into from

Conversation

sunng87
Copy link
Contributor

@sunng87 sunng87 commented Sep 15, 2017

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.

@SergioBenitez
Copy link
Member

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.

@sunng87
Copy link
Contributor Author

sunng87 commented Sep 16, 2017

@SergioBenitez disabled my format-on-save and commit again.

@@ -49,6 +49,10 @@ impl Context {
Context { root, templates, engines }
})
}

pub fn engines_mut(&mut self) -> &mut Engines {
Copy link
Member

Choose a reason for hiding this comment

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

Remove this method.

@@ -69,4 +69,14 @@ impl Engines {

None
}

#[cfg(feature = "handlebars_templates")]
pub fn handlebars(&mut self) -> &mut Handlebars {
Copy link
Member

Choose a reason for hiding this comment

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

Remove this method.

}

#[cfg(feature = "tera_templates")]
pub fn tera(&mut self) -> &mut Tera {
Copy link
Member

Choose a reason for hiding this comment

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

Remove this method.

/// # ;
/// }
/// ```
pub fn config_and_fairing<F>(f: F) -> impl Fairing
Copy link
Member

Choose a reason for hiding this comment

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

Name this method custom.

/// }
/// ```
pub fn config_and_fairing<F>(f: F) -> impl Fairing
where F: Fn(&mut Context) + Send + Sync + 'static
Copy link
Member

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.

Copy link
Member

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?

Copy link
Contributor Author

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",
Copy link
Member

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 {
Copy link
Member

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)));

@@ -74,3 +74,9 @@ mod uuid;

#[cfg(feature = "uuid")]
pub use uuid::{UUID, UuidParseError};

#[cfg(feature = "handlebars_templates")]
pub extern crate handlebars;
Copy link
Member

Choose a reason for hiding this comment

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

Move these extern crates to the top of the file below the existing extern crates.

Ok(())
}));

}))
Copy link
Contributor Author

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

@SergioBenitez
Copy link
Member

Please see the build failure.

@sunng87
Copy link
Contributor Author

sunng87 commented Sep 17, 2017

@SergioBenitez it was Template::show that has no helper config. Will a API break change on Template::show work for you? I'm going to add a closure argument to it. Of course we can a Template::custom_and_show but I think it's a little verbose for a test method. WDYT?

@SergioBenitez
Copy link
Member

So sorry for the delay here! As we spoke about briefly in person, I think the API for Template::show needs to change a bit. The upside is that I think it'll actually be cleaner.

Here's the signature I'm imagining for Template::show():

    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 show() now takes in a Rocket instance explicitly. This means that we can use the cached versions of templates (yay!) along with their configuration (yay!) and carrying around a root path is no longer required (yay!). An instance of Rocket should be available at all points necessary. In tests, it can be obtained via client.rocket().

What do you think?

@SergioBenitez
Copy link
Member

Just a heads up that #500 also seeks to implement this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants