-
-
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.
Merge branch 'master' into feature/mtls
- Loading branch information
Showing
51 changed files
with
562 additions
and
140 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
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
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
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,89 @@ | ||
use rocket::{Request, State, Outcome}; | ||
use rocket::http::Status; | ||
use rocket::request::{self, FromRequest}; | ||
|
||
use templates::Context; | ||
|
||
/// The `TemplateMetadata` type: implements `FromRequest`, allowing dynamic | ||
/// queries about template metadata. | ||
/// | ||
/// # Usage | ||
/// | ||
/// First, ensure that the template [fairing](`rocket::fairing`) is attached to | ||
/// your Rocket application: | ||
/// | ||
/// ```rust | ||
/// # extern crate rocket; | ||
/// # extern crate rocket_contrib; | ||
/// # | ||
/// use rocket_contrib::Template; | ||
/// | ||
/// fn main() { | ||
/// rocket::ignite() | ||
/// .attach(Template::fairing()) | ||
/// // ... | ||
/// # ; | ||
/// } | ||
/// ``` | ||
/// | ||
/// The `TemplateMetadata` type implements Rocket's `FromRequest` trait, so it can | ||
/// be used as a request guard in any request handler. | ||
/// | ||
/// ```rust | ||
/// # #![feature(plugin, decl_macro)] | ||
/// # #![plugin(rocket_codegen)] | ||
/// # extern crate rocket; | ||
/// # #[macro_use] extern crate rocket_contrib; | ||
/// # fn main() { } | ||
/// # | ||
/// use rocket_contrib::{Template, TemplateMetadata}; | ||
/// | ||
/// #[get("/")] | ||
/// fn homepage(metadata: TemplateMetadata) -> Template { | ||
/// // Conditionally render a template if it's available. | ||
/// if metadata.contains_template("some-template") { | ||
/// Template::render("some-template", json!({ /* .. */ })) | ||
/// } else { | ||
/// Template::render("fallback", json!({ /* .. */ })) | ||
/// } | ||
/// } | ||
/// ``` | ||
pub struct TemplateMetadata<'a>(&'a Context); | ||
|
||
impl<'a> TemplateMetadata<'a> { | ||
/// Returns `true` if the template with name `name` was loaded at start-up | ||
/// time. Otherwise, returns `false`. | ||
/// | ||
/// # Example | ||
/// | ||
/// ```rust | ||
/// use rocket_contrib::TemplateMetadata; | ||
/// | ||
/// fn handler(metadata: TemplateMetadata) { | ||
/// // Returns `true` if the template with name `"name"` was loaded. | ||
/// let loaded = metadata.contains_template("name"); | ||
/// } | ||
/// ``` | ||
pub fn contains_template(&self, name: &str) -> bool { | ||
self.0.templates.contains_key(name) | ||
} | ||
} | ||
|
||
/// Retrieves the template metadata. If a template fairing hasn't been attached, | ||
/// an error is printed and an empty `Err` with status `InternalServerError` | ||
/// (`500`) is returned. | ||
impl<'a, 'r> FromRequest<'a, 'r> for TemplateMetadata<'a> { | ||
type Error = (); | ||
|
||
fn from_request(request: &'a Request) -> request::Outcome<Self, ()> { | ||
request.guard::<State<Context>>() | ||
.succeeded() | ||
.and_then(|ctxt| Some(Outcome::Success(TemplateMetadata(ctxt.inner())))) | ||
.unwrap_or_else(|| { | ||
error_!("Uninitialized template context: missing fairing."); | ||
info_!("To use templates, you must attach `Template::fairing()`."); | ||
info_!("See the `Template` documentation for more information."); | ||
Outcome::Failure((Status::InternalServerError, ())) | ||
}) | ||
} | ||
} |
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
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
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.