2 releases
new 0.13.2 | Feb 8, 2025 |
---|---|
0.13.1 |
|
0.13.0 | Feb 8, 2025 |
#183 in Template engine
61 downloads per month
Used in 2 crates
70KB
1.5K
SLoC
🦝 Reva
WIP :)
Fork of Askama since they're about to merge with Rinja, which doesn't work for me because the syntax changed so much and I don't plan on rewriting 103 HTML files + 53 Rust files.
lib.rs
:
Reva implements a type-safe compiler for Jinja-like templates.
It lets you write templates in a Jinja-like syntax,
which are linked to a struct
defining the template context.
This is done using a custom derive implementation (implemented
in reva_derive
).
For feature highlights and a quick start, please review the README.
The primary documentation for this crate now lives in the book.
Creating Reva templates
An Reva template is a struct
definition which provides the template
context combined with a UTF-8 encoded text file (or inline source, see
below). Reva can be used to generate any kind of text-based format.
The template file's extension may be used to provide content type hints.
A template consists of text contents, which are passed through as-is, expressions, which get replaced with content while being rendered, and tags, which control the template's logic. The template syntax is very similar to Jinja, as well as Jinja-derivatives like Twig or Tera.
The template()
attribute
Reva 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. The following sub-attributes are currently
recognized:
path
(aspath = "foo.html"
): sets the path to the template file. The path is interpreted as relative to the configured template directories (by default, this is atemplates
directory next to yourCargo.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 withsource
.source
(assource = "{{ 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. Ifsource
is specified,ext
must also be specified (see below). Cannot be used together withpath
.ext
(asext = "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 withpath
.print
(asprint = "code"
): enable debugging by printing nothing (none
), the parsed syntax tree (ast
), the generated code (code
) orall
for both. The requested data will be printed to stdout at compile time.escape
(asescape = "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
(assyntax = "foo"
): set the syntax name for a parser defined in the configuration file. The default syntax , "default", is the one provided by Reva.
Dependencies
~2–2.9MB
~60K SLoC