logo
pub struct Metadata<'a>(_);
Expand description

Request guard for dynamically querying template metadata.

Usage

The Metadata type implements Rocket’s FromRequest trait, so it can be used as a request guard in any request handler.

use rocket_dyn_templates::{Template, Metadata, context};

#[get("/")]
fn homepage(metadata: Metadata) -> Template {
    // Conditionally render a template if it's available.
    if metadata.contains_template("some-template") {
        Template::render("some-template", &context)
    } else {
        Template::render("fallback", &context)
    }
}

fn main() {
    rocket::build()
        .attach(Template::fairing())
        // ...
}

Implementations

Returns true if the template with the given name is currently loaded. Otherwise, returns false.

Example
use rocket_dyn_templates::Metadata;

#[get("/")]
fn handler(metadata: Metadata) {
    // Returns `true` if the template with name `"name"` was loaded.
    let loaded = metadata.contains_template("name");
}

Returns true if template reloading is enabled.

Example
use rocket_dyn_templates::Metadata;

#[get("/")]
fn handler(metadata: Metadata) {
    // Returns `true` if template reloading is enabled.
    let reloading = metadata.reloading();
}

Directly render the template named name with the context context into a String. Also returns the template’s detected ContentType. See Template::render() for more details on rendering.

Examples
use rocket::http::ContentType;
use rocket_dyn_templates::{Metadata, Template, context};

#[get("/")]
fn send_email(metadata: Metadata) -> Option<()> {
    let (mime, string) = metadata.render("email", context! {
        field: "Hello, world!"
    })?;

    send_email(mime, string).await?;
    Some(())
}

#[get("/")]
fn raw_render(metadata: Metadata) -> Option<(ContentType, String)> {
    metadata.render("index", context! { field: "Hello, world!" })
}

// Prefer the following, however, which is nearly identical but pithier:

#[get("/")]
fn render() -> Template {
    Template::render("index", context! { field: "Hello, world!" })
}

Trait Implementations

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.

The associated error to be returned if derivation fails.

Derives an instance of Self from the incoming request metadata. Read more

Returns true if launch should be aborted and false otherwise.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Converts self into a collection.

Should always be Self

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more