Struct rocket_contrib::templates::Template [−][src]
Responder that renders a dynamic template.
Usage
To use, add the handlebars_templates
feature, the tera_templates
feature, or both, to the rocket_contrib
dependencies section of your
Cargo.toml
:
[dependencies.rocket_contrib]
version = "0.5.0-dev"
default-features = false
features = ["handlebars_templates", "tera_templates"]
Then, ensure that the template Fairing
is attached to your Rocket
application:
use rocket_contrib::templates::Template; fn main() { rocket::ignite() .attach(Template::fairing()) // ... }
The Template
type implements Rocket’s Responder
trait, so it can be
returned from a request handler directly:
use rocket_contrib::templates::Template; #[get("/")] fn index() -> Template { let context = context(); Template::render("index", &context) }
Helpers, Filters, and Customization
You may use the Template::custom()
method to construct a fairing with
customized templating engines. Among other things, this method allows you to
register template helpers and register templates from strings.
Implementations
impl Template
[src]
pub fn fairing() -> impl Fairing
[src]
Returns a fairing that initializes and maintains templating state.
This fairing, or the one returned by Template::custom()
, must be
attached to any Rocket
instance that wishes to render templates.
Failure to attach this fairing will result in a “Uninitialized template
context: missing fairing.” error message when a template is attempted to
be rendered.
If you wish to customize the internal templating engines, use
Template::custom()
instead.
Example
To attach this fairing, simple call attach
on the application’s
Rocket
instance with Template::fairing()
:
extern crate rocket; extern crate rocket_contrib; use rocket_contrib::templates::Template; fn main() { rocket::ignite() // ... .attach(Template::fairing()) // ... }
pub fn custom<F: Send + Sync + 'static>(f: F) -> impl Fairing where
F: Fn(&mut Engines),
[src]
F: Fn(&mut Engines),
Returns a fairing that initializes and maintains templating state.
Unlike Template::fairing()
, this method allows you to configure
templating engines via the function f
. Note that only the enabled
templating engines will be accessible from the Engines
type.
This method does not allow the function f
to fail. If f
is fallible,
use Template::try_custom()
instead.
Example
extern crate rocket; extern crate rocket_contrib; use rocket_contrib::templates::Template; fn main() { rocket::ignite() // ... .attach(Template::custom(|engines| { // engines.handlebars.register_helper ... })) // ... }
pub fn try_custom<F: Send + Sync + 'static>(f: F) -> impl Fairing where
F: Fn(&mut Engines) -> Result<(), Box<dyn Error>>,
[src]
F: Fn(&mut Engines) -> Result<(), Box<dyn Error>>,
Returns a fairing that initializes and maintains templating state.
This variant of Template::custom()
allows a fallible f
. If f
returns an error during initialization, it will cancel the launch. If
f
returns an error during template reloading (in debug mode), then the
newly-reloaded templates are discarded.
Example
extern crate rocket; extern crate rocket_contrib; use rocket_contrib::templates::Template; fn main() { rocket::ignite() // ... .attach(Template::try_custom(|engines| { // engines.handlebars.register_helper ... Ok(()) })) // ... }
pub fn render<S, C>(name: S, context: C) -> Template where
S: Into<Cow<'static, str>>,
C: Serialize,
[src]
S: Into<Cow<'static, str>>,
C: Serialize,
Render the template named name
with the context context
. The
context
can be of any type that implements Serialize
. This is
typically a HashMap
or a custom struct
.
Example
use std::collections::HashMap; use rocket_contrib::templates::Template; // Create a `context`. Here, just an empty `HashMap`. let mut context = HashMap::new(); let template = Template::render("index", context);
pub fn show<S, C>(rocket: &Rocket, name: S, context: C) -> Option<String> where
S: Into<Cow<'static, str>>,
C: Serialize,
[src]
S: Into<Cow<'static, str>>,
C: Serialize,
Render the template named name
with the context context
into a
String
. This method should not be used in any running Rocket
application. This method should only be used during testing to validate
Template
responses. For other uses, use render()
instead.
The context
can be of any type that implements Serialize
. This is
typically a HashMap
or a custom struct
.
Returns Some
if the template could be rendered. Otherwise, returns
None
. If rendering fails, error output is printed to the console.
None
is also returned if a Template
fairing has not been attached.
Example
use std::collections::HashMap; use rocket_contrib::templates::Template; use rocket::local::blocking::Client; fn main() { let rocket = rocket::ignite().attach(Template::fairing()); let client = Client::untracked(rocket).expect("valid rocket"); // Create a `context`. Here, just an empty `HashMap`. let mut context = HashMap::new(); let template = Template::show(client.rocket(), "index", context); }
Trait Implementations
impl Debug for Template
[src]
impl<'r> Responder<'r, 'static> for Template
[src]
Returns a response with the Content-Type derived from the template’s
extension and a fixed-size body containing the rendered template. If
rendering fails, an Err
of Status::InternalServerError
is returned.
fn respond_to(self, req: &'r Request<'_>) -> Result<'static>
[src]
Auto Trait Implementations
impl RefUnwindSafe for Template
impl Send for Template
impl Sync for Template
impl Unpin for Template
impl UnwindSafe for Template
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T> Instrument for T
[src]
pub fn instrument(self, span: Span) -> Instrumented<Self>
[src]
pub fn in_current_span(self) -> Instrumented<Self>
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> IntoCollection<T> for T
pub fn into_collection<A>(self) -> SmallVec<A> where
A: Array<Item = T>,
A: Array<Item = T>,
pub fn mapped<U, F, A>(self, f: F) -> SmallVec<A> where
F: FnMut(T) -> U,
A: Array<Item = U>,
F: FnMut(T) -> U,
A: Array<Item = U>,
impl<T> IntoSql for T
pub fn into_sql<T>(self) -> Self::Expression where
Self: AsExpression<T>,
Self: AsExpression<T>,
pub fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expression where
&'a Self: AsExpression<T>,
&'a Self: AsExpression<T>,
impl<T> Same<T> for T
type Output = T
Should always be Self
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,