• Stars
    star
    3,302
  • Rank 13,584 (Top 0.3 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created almost 8 years ago
  • Updated 3 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

instant-distance

Fast approximate nearest neighbor searching in Rust, based on HNSW index
Rust
308
star
3

couchdb-python

Python library for working with CouchDB
Python
202
star
4

tokio-imap

Tokio-based IMAP implementation
Rust
115
star
5

instant-segment

Fast English word segmentation in Rust
Rust
89
star
6

instant-acme

Simple ACME client
Rust
85
star
7

runa

A Python-like systems programming language
Python
84
star
8

pyrtls

rustls-based modern TLS for Python
Rust
75
star
9

gcp_auth

Minimal authentication library for Google Cloud Platform (GCP)
Rust
63
star
10

rustc-version-rs

Rust
58
star
11

mendes

Rust web toolkit for impatient perfectionists
Rust
40
star
12

topfew-rs

Rust implementation of Tim Bray's topfew tool
Rust
40
star
13

awmy

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

no-close-buttons

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

instant-xml

Rust
18
star
16

rnc2rng

RELAX NG Compact to regular syntax conversion library
Python
16
star
17

persona-totp

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

fixlib

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

mailsync

Rust
12
star
20

imap-proto

IMAP protocol parser and datastructures in Rust
12
star
21

abna

Python library to automatically retrieve mutations from ABN Amro
Python
10
star
22

instant-akismet

Akismet spam detection client written in Rust
Rust
9
star
23

galatna

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

appdirs-rs

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

talks

Materials for presentations I've given
HTML
8
star
26

instant-epp

Rust
7
star
27

s3-fifo

Rust
6
star
28

corda-rpc

Rust libraries for doing Corda RPC
Rust
6
star
29

sign-cert-remote

Rust
5
star
30

pmf

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

mbox-reader

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

jasinja

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

candotti

Oxidizing Thread & Matter
Rust
3
star
34

rst-parser

reStructuredText parser and writers in Rust
Rust
3
star
35

awijan

Tool to help create release notes
Rust
3
star
36

lecertman

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

cs143-python

Python parser for CS143 AST serialization
Python
2
star
38

cryptopals

Rust
2
star
39

edit-chunks

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

email-parser

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

nested-input

Rust
1
star
42

djc-overlay

My experimental Gentoo overlay
1
star
43

kv

Rust
1
star
44

lang-repo-analysis

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

mime-types

Rust
1
star
46

clang-format-find

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

wsgrep

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

tz-tmp

Rust
1
star
49

modern-lang-analysis

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