Derive Macro Template

Source
#[derive(Template)]
{
    // Attributes available to this derive:
    #[template]
}
Available on crate feature derive only.
Expand description

The Template derive macro and its template() attribute.

Askama works by generating one or more trait implementations for any struct type decorated with the #[derive(Template)] attribute. The code generation process takes some options that can be specified through the template() attribute.

§Attributes

The following sub-attributes are currently recognized:

§path

E.g. path = "foo.html"

Sets the path to the template file. The path is interpreted as relative to the configured template directories (by default, this is a templates directory next to your Cargo.toml). The file name extension is used to infer an escape mode (see below). In web framework integrations, the path’s extension may also be used to infer the content type of the resulting response. Cannot be used together with source.

§source

E.g. source = "{{ foo }}"

Directly sets the template source. This can be useful for test cases or short templates. The generated path is undefined, which generally makes it impossible to refer to this template from other templates. If source is specified, ext must also be specified (see below). Cannot be used together with path. ext (e.g. ext = "txt"): lets you specify the content type as a file extension. This is used to infer an escape mode (see below), and some web framework integrations use it to determine the content type. Cannot be used together with path.

§in_doc

E.g. in_doc = true

As an alternative to supplying the code template code in an external file (as path argument), or as a string (as source argument), you can also enable the "code-in-doc" feature. With this feature, you can specify the template code directly in the documentation of the template struct.

Instead of path = "…" or source = "…", specify in_doc = true in the #[template] attribute, and in the struct’s documentation add a askama code block:

/// ```askama
/// <div>{{ lines|linebreaksbr }}</div>
/// ```
#[derive(Template)]
#[template(ext = "html", in_doc = true)]
struct Example<'a> {
    lines: &'a str,
}

§print

E.g. print = "code"

Enable debugging by printing nothing (none), the parsed syntax tree (ast), the generated code (code) or all for both. The requested data will be printed to stdout at compile time.

§block

E.g. block = "block_name"

Renders the block by itself. Expressions outside of the block are not required by the struct, and inheritance is also supported. This can be useful when you need to decompose your template for partial rendering, without needing to extract the partial into a separate template or macro.

#[derive(Template)]
#[template(path = "hello.html", block = "hello")]
struct HelloTemplate<'a> { ... }

§blocks

E.g. blocks = ["title", "content"]

Automatically generates (a number of) sub-templates that act as if they had a block = "..." attribute. You can access the sub-templates with the method my_template.as_block_name(), where block_name is the name of the block:

#[derive(Template)]
#[template(
    ext = "txt",
    source = "
        {% block title %} ... {% endblock %}
        {% block content %} ... {% endblock %}
    ",
    blocks = ["title", "content"]
)]
struct News<'a> {
    title: &'a str,
    message: &'a str,
}

let news = News {
    title: "Announcing Rust 1.84.1",
    message: "The Rust team has published a new point release of Rust, 1.84.1.",
};
assert_eq!(
    news.as_title().render().unwrap(),
    "<h1>Announcing Rust 1.84.1</h1>"
);

§escape

E.g. escape = "none"

Override the template’s extension used for the purpose of determining the escaper for this template. See the section on configuring custom escapers for more information.

§syntax

E.g. syntax = "foo"

Set the syntax name for a parser defined in the configuration file. The default syntax, "default", is the one provided by Askama.

§askama

E.g. askama = askama

If you are using askama in a subproject, a library or a macro, it might be necessary to specify the path where to find the module askama:

#[doc(hidden)]
use askama as __askama;

#[macro_export]
macro_rules! new_greeter {
    ($name:ident) => {
        #[derive(Debug, $crate::askama::Template)]
        #[template(
            ext = "txt",
            source = "Hello, world!",
            askama = $crate::__askama
        )]
        struct $name;
    }
}

new_greeter!(HelloWorld);
assert_eq!(HelloWorld.to_string(), Ok("Hello, world."));