Derive Macro blue_build_template::Template
source · #[derive(Template)]
{
// Attributes available to this derive:
#[template]
}
Expand description
The Template
derive macro and its template()
attribute.
Rinja 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 rinja
code block:
/// ```rinja
/// <div>{{ lines|linebreaksbr }}</div>
/// ```
#[derive(Template)]
#[template(ext = "html", in_doc = true)]
struct Example<'a> {
lines: &'a str,
}
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.
§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 Rinja.