-
-
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
Provide a template registration callback #500
Provide a template registration callback #500
Conversation
this makes it possible to register helper functions with Handlebars and Tera
Because the callback returns |
@@ -15,6 +15,7 @@ use self::glob::glob; | |||
|
|||
use std::borrow::Cow; | |||
use std::path::{Path, PathBuf}; | |||
use std::marker::{Send, Sync}; |
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.
Oops, this isn't alphabetical
This looks like the original implementation in #431. Perhaps you can finish off the contribution started there? |
Oh, I didn't see that PR because it wasn't linked to the previous issues & PRs. I haven't used |
In #431, I describe a solution to this issue: #431 (comment). We can't accept any PR for this unless testing works as well. We also can't accept a PR for this unless there is documentation and new tests for the feature. |
Refactoring One question: how do you get a |
It needs to be in one PR, otherwise, you can't possibly test this new addition. You don't need to be able to retrieve anything additional from a |
Your previous comment said to update Being very unfamiliar with the code, I was thinking that something like this might work: pub fn show<S, C>(rocket: &Rocket, name: S, context: C) -> Option<String>
where S: Into<Cow<'static, str>>, C: Serialize
{
let ctxt: Context = rocket.fairings.find(|fairing| fairing.info().name == "")?;
Template::render(name, context).finalize(&ctxt).ok().map(|v| v.0)
} Though this appears to handle controller renders: |
error[E0616]: field `state` of struct `rocket::Rocket` is private
--> contrib/src/templates/mod.rs:243:20
|
243 | let ctxt = rocket.state.get::<Context>();
| ^^^^^^^^^^^^
error: aborting due to previous error even though it should be accessible in the crate, as it's defined with pub struct Rocket {
pub(crate) config: Config,
router: Router,
default_catchers: HashMap<u16, Catcher>,
catchers: HashMap<u16, Catcher>,
pub(crate) state: Container,
fairings: Fairings,
} |
@seanlinsley The implementation details of managed state should never be exposed to any outside crate. The I think it's fair (and necessary, here) to expose a method that allows retrieving managed state from a /// Retrieve the managed state of type `T`, if it exists.
///
/// # Example (...)
#[inline(always)]
pub fn state<T: Send + Sync + 'static>(&self) -> Option<&T> {
self.state.try_get()
} You can then call |
Great to hear that someone managed to finish this feature 😄 Thanks @jebrosen! |
This makes it possible to register helper functions with Handlebars and Tera, resolving #64.
I'm using this locally to update Crates.io to use server-side rendering: rust-lang/crates.io#1173.
That branch is out of date currently; here's how I'm using this feature:
There have been two previous PRs to try adding this feature: #60 and #364. @SergioBenitez has wanted the ability to register the same helper functions on both rendering engines simultaneously, but I'm not sure that's possible because of the library-specific code inside of the helper functions themselves.
Instead I opted to make the underlying template objects accessible via a callback passed to
Template::fairing_with
. Is there a better name for this function?Once we're happy with this change, I'll update the documentation and write a test.