ランキングに戻る

rinja-rs/askama

Rust

Type-safe, compiled Jinja-like templates for Rust

templatingjinja2rust-librarycustom-deriverust
スター
3.6k
フォーク
227
週間成長
Issue
111
成果物crates.iocargo add askama
README

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.

関連リポジトリ
twigphp/Twig

Twig, the flexible, fast, and secure template language for PHP

PHPPackagistBSD 3-Clause "New" or "Revised" Licensetwigphp
twig.symfony.com
8.4k1.3k
scriban/scriban

A fast, powerful, safe and lightweight scripting language and engine for .NET

C#BSD 2-Clause "Simplified" Licensetemplatingtemplating-language
scriban.github.io
3.9k408
askama-rs/askama-old

The original askama repo, please go to https://github.com/askama-rs/askama

Rustcrates.ioApache License 2.0templatingjinja2
github.com/askama-rs/askama
3.5k212
hairyhenderson/gomplate

A flexible commandline tool for template rendering. Supports lots of local and remote datasources.

GoGo ModulesMIT Licensegolangtemplate
gomplate.ca
3.2k216
matcornic/hermes

Golang package that generates clean, responsive HTML e-mails for sending transactional mail

GoGo ModulesApache License 2.0golanghermes
3k248
hyperhype/hyperscript

Create HyperText with JavaScript.

HTMLMIT Licensehyperscripttemplating
2.7k110
JohnSundell/Plot

A DSL for writing type-safe HTML, XML and RSS in Swift.

SwiftMIT Licensedslhtml
2k144
deadc0de6/dotdrop

Save your dotfiles once, deploy them everywhere

PythonPyPIGNU General Public License v3.0dotfilesjinja2
dotdrop.readthedocs.io
1.9k110
carvel-dev/ytt

YAML templating tool that works on YAML structure instead of text

GoGo ModulesApache License 2.0cligo
carvel.dev/ytt
1.9k160
templui/templui

A growing collection of beautifully designed UI components for Go and templ. Install via CLI. Customize everything. Own your code.

templMIT Licensegogo-web
templui.io
1.6k92
cruft/cruft

Allows you to maintain all the necessary cruft for packaging and building projects separate from the code you intentionally write. Built on-top of, and fully compatible with, CookieCutter.

PythonPyPIMIT Licensepythoncruft
cruft.github.io/cruft/
1.6k103
felangel/mason

Tools which allow developers to create and consume reusable templates called bricks.

Dartdart-packagedart
docs.brickhub.dev
1.1k109