djc/askama


Type-safe, compiled Jinja-like templates for Rust

License: Apache-2.0

Language: Rust

Keywords: custom-derive, jinja2, rust, rust-library, templating


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 14
Repository Size 3.78 MB
Stars 3,325
Forks 215
Watchers 23
Open issues 125
Dependencies 68
Contributors 124
Tags 29
Created
Last updated
Last pushed

Top Contributors See all

Dirkjan Ochtman René Kijewski Guillaume Gomez Christian Vallentin zzau13 dependabot[bot] PizzasBear mash manunio yossyX Anthony Nowell dependabot-preview[bot] larros Jacob Rothstein Matthew Taylor Jonas Platte tizgafa defyrlt Nathan Lapel Paolo Barbolini

Packages Referencing this Repo

askama
Type-safe, compiled Jinja-like templates for Rust
Latest release 0.12.1 - Updated - 3.33K stars
askama_derive
Procedural macro package for Askama
Latest release 0.12.5 - Updated - 3.33K stars
askama_parser
Parser for Askama templates
Latest release 0.2.1 - Updated - 3.33K stars
askama_escape
Optimized HTML escaping code, extracted from Askama
Latest release 0.10.3 - Updated - 3.33K stars
askama_shared
Shared code for Askama
Latest release 0.12.2 - Updated - 3.33K stars
askama_hyper
Hyper integration for Askama templates
Latest release 0.1.0 - Published - 3.33K stars
askama_axum
Axum integration for Askama templates
Latest release 0.4.0 - Updated - 3.33K stars
askama_tide
Tide integration for Askama templates
Latest release 0.15.0 - Updated - 3.33K stars
askama_warp
Warp integration for Askama templates
Latest release 0.13.0 - Updated - 3.33K stars
askama_rocket
Rocket integration for Askama templates
Latest release 0.12.0 - Updated - 3.33K stars
askama_iron
Iron integration for Askama templates
Latest release 0.10.0 - Published - 3.33K stars
askama_gotham
Gotham integration for Askama templates
Latest release 0.14.0 - Updated - 3.33K stars
askama_actix
Actix-Web integration for Askama templates
Latest release 0.14.0 - Updated - 3.33K stars

Recent Tags See all

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
0.6.4 April 27, 2018

Something wrong with this page? Make a suggestion

Last synced: 2024-07-17 10:12:44 UTC

Login to resync this repository