[][src]Struct rocket_contrib::templates::Template

pub struct Template { /* fields omitted */ }

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 = "*"
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.

Methods

impl Template
[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())
        // ...
}

Returns a fairing that initializes and maintains templating state.

Unlike Template::fairing(), this method allows you to configure templating engines via the parameter f. Note that only the enabled templating engines will be accessible from the Engines type.

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 ...
        }))
        // ...
}

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

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::Client;

fn main() {
    let rocket = rocket::ignite().attach(Template::fairing());
    let client = Client::new(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 Responder<'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.

Auto Trait Implementations

impl Send for Template

impl Sync for Template

Blanket Implementations

impl<T> From for T
[src]

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom for T where
    T: From<U>, 
[src]

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T> Typeable for T where
    T: Any

Get the TypeId of this object.

impl<T> IntoCollection for T

impl<T, I> AsResult for T where
    I: Input, 

impl<T> IntoSql for T

Convert self to an expression for Diesel's query builder. Read more

Convert &self to an expression for Diesel's query builder. Read more

impl<T> Same for T

Should always be Self

impl<T> Erased for T

impl<T, U> TryInto for T where
    U: TryFrom<T>,