• Stars
    star
    2,946
  • Rank 14,730 (Top 0.3 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created over 7 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Type-safe, compiled Jinja-like templates for 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.

More Repositories

1

bb8

Full-featured async (tokio-based) postgres connection pool (like r2d2)
Rust
647
star
2

couchdb-python

Python library for working with CouchDB
Python
200
star
3

tokio-imap

Tokio-based IMAP implementation
Rust
115
star
4

runa

A Python-like systems programming language
Python
84
star
5

rustc-version-rs

Rust
58
star
6

gcp_auth

Minimal authentication library for Google Cloud Platform (GCP)
Rust
57
star
7

mendes

Rust web toolkit for impatient perfectionists
Rust
41
star
8

topfew-rs

Rust implementation of Tim Bray's topfew tool
Rust
36
star
9

awmy

arewemeetingyet.com: help communicate meeting times to timezone-challenged participants
Python
34
star
10

no-close-buttons

Firefox addon to remove close buttons from tabs
Makefile
18
star
11

persona-totp

A minimal Python implementation of a Persona IdP
Python
15
star
12

fixlib

Pythonic library for dealing with the FIX protocol
Python
14
star
13

rnc2rng

RELAX NG Compact to regular syntax conversion library
Python
14
star
14

mailsync

Rust
12
star
15

imap-proto

IMAP protocol parser and datastructures in Rust
12
star
16

abna

Python library to automatically retrieve mutations from ABN Amro
Python
9
star
17

galatna

Create SVG-based maps from Google Latitude location history
Python
9
star
18

appdirs-rs

Rust crate for determining platform-specific application directories
Rust
9
star
19

talks

Materials for presentations I've given
HTML
7
star
20

s3-fifo

Rust
6
star
21

corda-rpc

Rust libraries for doing Corda RPC
Rust
6
star
22

sign-cert-remote

Rust
5
star
23

pmf

Script to submit RSS/Atom feed content to Reddit
Python
4
star
24

mbox-reader

Minimal mmap-backed mbox format reader in Rust
Rust
4
star
25

jasinja

Jasinja is a compiler for Jinja templates targeting Javascript
Python
4
star
26

candotti

Oxidizing Thread & Matter
Rust
3
star
27

rst-parser

reStructuredText parser and writers in Rust
Rust
3
star
28

pyrtls

rustls-based TLS for Python
Rust
3
star
29

awijan

Tool to help create release notes
Rust
3
star
30

lecertman

Let's Encrypt certificate manager -- manage a group of certificates, ensure they are renewed on time
Python
3
star
31

cs143-python

Python parser for CS143 AST serialization
Python
2
star
32

cryptopals

Rust
2
star
33

edit-chunks

Split out chunks of a large file for editing, then put them back together again
Rust
2
star
34

email-parser

Lazy liberal email (RFC 5322) parser in Rust
Rust
1
star
35

nested-input

Rust
1
star
36

djc-overlay

My experimental Gentoo overlay
1
star
37

kv

Rust
1
star
38

lang-repo-analysis

Code for https://dirkjan.ochtman.nl/writing/2017/05/19/rust-is-a-big-tent.html
Python
1
star
39

clang-format-find

Find the clang-format configuration that best fits your codebase
Python
1
star
40

wsgrep

Proof of concept grep (Rust Utrecht workshop 2018-05-03)
Rust
1
star
41

tz-tmp

Rust
1
star
42

modern-lang-analysis

Code and data for my blog post "Rust is a big tent"
Python
1
star