Kijewski/test-askama-rtd


License: Apache-2.0

Language: Rust


Askama

Documentation Latest version Build Status Chat

Askama implements a template rendering engine based on Jinja. It generates Rust code from your templates at compile time based on a user-defined struct to hold the template's context. See below for an example, or read the book.

"Pretty exciting. I would love to use this already." -- Armin Ronacher, creator of Jinja

All feedback welcome. Feel free to file bugs, requests for documentation and any other feedback to the issue tracker or tweet me.

Askama was created by and is maintained by Dirkjan Ochtman. If you are in a position to support ongoing maintenance and further development or use it in a for-profit context, please consider supporting my open source work on Patreon.

Feature highlights

  • Construct templates using a familiar, easy-to-use syntax
  • Benefit from the safety provided by Rust's type system
  • Template code is compiled into your crate for optimal performance
  • Optional built-in support for Actix, Axum, Rocket, and warp web frameworks
  • Debugging features to assist you in template development
  • Templates must be valid UTF-8 and produce UTF-8 when rendered
  • IDE support available in JetBrains products
  • Works on stable Rust

Supported in templates

  • Template inheritance
  • Loops, if/else statements and include support
  • Macro support
  • Variables (no mutability allowed)
  • Some built-in filters, and the ability to use your own
  • Whitespace suppressing with '-' markers
  • Opt-out HTML escaping
  • Syntax customization

How to get started

First, add the Askama dependency to your crate's Cargo.toml:

cargo add askama

Now create a directory called templates in your crate root. In it, create a file called hello.html, containing the following:

Hello, {{ name }}!

In any Rust file inside your crate, add the following:

use askama::Template; // bring trait in scope

#[derive(Template)] // this will generate the code...
#[template(path = "hello.html")] // using the template in this path, relative
                                 // to the `templates` dir in the crate root
struct HelloTemplate<'a> { // the name of the struct can be anything
    name: &'a str, // the field name should match the variable name
                   // in your template
}

fn main() {
    let hello = HelloTemplate { name: "world" }; // instantiate your struct
    println!("{}", hello.render().unwrap()); // then render it.
}

You should now be able to compile and run this code.

Review the test cases for more examples.

Project Statistics

Sourcerank 4
Repository Size 1.69 MB
Stars 0
Forks 0
Watchers 1
Open issues 0
Dependencies 0
Contributors 162
Tags 28
Created
Last updated
Last pushed

Top Contributors See all

Dirkjan Ochtman René Kijewski René Kijewski Guillaume Gomez Christian Vallentin Christian Vallentin zzau13 zzau13 dependabot[bot] dependabot[bot] PizzasBear PizzasBear mash yossyX Anthony Nowell larros Jacob Rothstein Matthew Taylor tizgafa Jonas Platte

Recent Tags See all

0.12.1 rtd May 10, 2024
0.10.2 April 05, 2024
0.10.3 April 05, 2024
0.10.4 April 05, 2024
0.10.5 April 05, 2024
0.11.1 April 05, 2024
0.12.1 December 07, 2023
0.12.0 March 06, 2023
0.11.0 December 21, 2021
0.10.0 June 30, 2020
0.9.0 February 24, 2020
0.8.0 February 20, 2019
0.7.2 October 08, 2018
0.7.1 July 23, 2018
0.7.0 June 25, 2018

Something wrong with this page? Make a suggestion

Last synced: 2024-06-09 10:12:51 UTC

Login to resync this repository