markup.rs
A blazing fast, type-safe template engine for Rust.
markup.rs
is a template engine for Rust powered by procedural macros which
parses the template at compile time and generates optimal Rust code to render
the template at run time. The templates may embed Rust code which is type
checked by the Rust compiler enabling full type-safety.
Features
- Fully type-safe with inline highlighted errors when using editor extensions like rust-analyzer.
- Less error-prone and terse syntax inspired by Haml, Slim, and Pug.
- Zero unsafe code.
- Zero runtime dependencies.
- ⚡ Blazing fast. The fastest in this benchmark among the ones which do not use unsafe code, the second fastest overall.
Install
[]
= "0.15.0"
Framework Integration
We have integration examples for the following web frameworks:
Quick Example
!
define
Output
Example Domain Example Domain This domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission. More information... (c) 2020
Output (manually prettified)
<!doctype html>
Example Domain
Example Domain
This domain is for use in illustrative examples in documents. You may
use this domain in literature without prior coordination or asking for
permission.
More information...
(c) 2020
Syntax Reference (WIP)
markup::define! and markup::new!
There are two ways to define templates: markup::define!
and markup::new!
.
markup::define!
defines a template with named arguments. These templates cannot access variables from outer scope. The templates can have generic parameters. Under the hood, markup::define!
compiles to a Rust struct that implements markup::Render
and std::fmt::Display
traits.
!
// The template can now be printed directly or written to a stream:
println!;
writeln!.unwrap;
// The template can also be rendered to a String:
let string = Hello .to_string;
println!;
define
Hello, World!
Hello, World 2!
Hello, World 3!
markup::new!
defines a template without any arguments. These can access variables from outer scope.
let name = "World";
let template = new! ;
// The template can now be printed directly or written to a stream:
println!;
writeln!.unwrap;
// The template can also be rendered to a String:
let string = template.to_string;
println!;
Hello, World!
Hello, World!
Hello, World!
Expressions
Templates can have bare literal values, which are rendered as is. They can also have expressions (including function and macro calls) preceded by @
sign. All strings are HTML-escaped unless they are wrapped in markup::raw()
.
!
println!;
define
1 + 2 = 3
5 - 3 = 2
5 * 3 = 15
5 ^ 4 = 625
<>
Elements
Elements are defined using a CSS selector-like syntax. Elements can contain other nested elements in braces or be followed by a semicolon for self-closing elements.
!
println!;
define
Sidebar
Attributes
Attributes are defined after the element name. id
and class
attributes can be defined using CSS selector-like syntax using #
and .
. Classes may be specified multiple times using this shorthand syntax. Other attributes are specified in square brackets.
!
println!;
define
@if and @if let
@if
and @if let
works similar to Rust.
!
println!;
println!;
define
x = 2
y = 2
x is neither 1 nor 2
y is None
@match
@match
works similar to Rust, but the branches must be wrapped in braces.
!
println!;
println!;
println!;
define
x is None
x is 1 or 2
x is something else
@for
@for
works similar to Rust.
!
println!;
define
0: 1
1: 2
2: 4
3: 8
Statements
Templates can have statements preceded by @
sign. The most useful such
statement is @let
to compute a value for later reuse. @fn
can be used to
define a function. Also supported are @struct
, @mod
, @impl
, @const
,
@static
and more.
!
println!;
define
4
6