Debian Codegraph @

third_party/rust/askama/README.md

raw
1 # askama
2
3 [![Crates.io](https://img.shields.io/crates/v/askama?logo=rust&style=flat-square&logoColor=white "Crates.io")](https://crates.io/crates/askama)
4 [![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/askama-rs/askama/rust.yml?branch=master&logo=github&style=flat-square&logoColor=white "GitHub Workflow Status")](https://github.com/askama-rs/askama/actions/workflows/rust.yml)
5 [![Book](https://img.shields.io/readthedocs/askama?label=book&logo=readthedocs&style=flat-square&logoColor=white "Book")](https://askama.readthedocs.io/)
6 [![docs.rs](https://img.shields.io/docsrs/askama?logo=docsdotrs&style=flat-square&logoColor=white "docs.rs")](https://docs.rs/askama/)
7
8 **Askama** implements a template rendering engine based on [Jinja](https://jinja.palletsprojects.com/),
9 and generates type-safe Rust code from your templates at compile time
10 based on a user-defined `struct` to hold the template's context.
11
12 At some point, **Askama** got forked into **Rinja** (explained [here](https://blog.guillaume-gomez.fr/articles/2024-07-31+docs.rs+switching+jinja+template+framework+from+tera+to+rinja))
13 before getting merged back into **Askama**.
14
15 All feedback welcome! Feel free to file bugs, requests for documentation and
16 any other feedback to the [issue tracker][issues].
17
18 You can find the documentation about our syntax, features, configuration in our book:
19 [askama.readthedocs.io](https://askama.readthedocs.io/).
20
21 Have a look at our [*Askama Playground*](https://askama-rs.github.io/askama_playground/),
22 if you want to try out askama's code generation online.
23
24 ### Feature highlights
25
26 * Construct templates using a familiar, easy-to-use syntax
27 * Benefit from the safety provided by Rust's type system
28 * Template code is compiled into your crate for optimal performance
29 * Debugging features to assist you in template development
30 * Templates must be valid UTF-8 and produce UTF-8 when rendered
31 * Works on stable Rust
32
33 ### Supported in templates
34
35 * Template inheritance
36 * Loops, if/else statements and include support
37 * Macro support
38 * Variables (no mutability allowed)
39 * Some built-in filters, and the ability to use your own
40 * Whitespace suppressing with '-' markers
41 * Opt-out HTML escaping
42 * Syntax customization
43
44 [issues]: https://github.com/askama-rs/askama/issues
45
46
47 How to get started
48 ------------------
49
50 First, add the askama dependency to your crate's `Cargo.toml`:
51
52 ```sh
53 cargo add askama
54 ```
55
56 Now create a directory called `templates` in your crate root.
57 In it, create a file called `hello.html`, containing the following:
58
59 ```jinja
60 Hello, {{ name }}!
61 ```
62
63 In any Rust file inside your crate, add the following:
64
65 ```rust
66 use askama::Template; // bring trait in scope
67
68 #[derive(Template)] // this will generate the code...
69 #[template(path = "hello.html")] // using the template in this path, relative
70 // to the `templates` dir in the crate root
71 struct HelloTemplate<'a> { // the name of the struct can be anything
72 name: &'a str, // the field name should match the variable name
73 // in your template
74 }
75
76 fn main() {
77 let hello = HelloTemplate { name: "world" }; // instantiate your struct
78 println!("{}", hello.render().unwrap()); // then render it.
79 }
80 ```
81
82 You should now be able to compile and run this code.