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.
Quick Start
Add the markup
crate to your dependencies:
[]
= "0.4.1"
Define your template using the markup::define!
macro:
!
define
Render your template by either:
-
Writing it to any instance of
std::io::Write
:write!;
-
Converting it to a string and using it however you like:
let string = Hello .to_string;
Rendering the template produces (manually prettified):
Hello Ferris
Hello Everyone!
Hello Ferris!
Note
Due to a limitation in syn, the crate this crate uses to parse templates, it is necessary to wrap identifiers which precede an opening brace to be put in parentheses.
Example:
// Wrong
@if let Some = bar
@match foo
// Right
@if let Some = *&
@match *&
Syntax
You can define multiple templates in a define!
block.
!
define
println!;
println!;
First!
Second!
A template can have bare literal strings and arbitrary expressions in braces.
!
define
println!;
Hello, world!
3Ï€345
Elements can either have children inside {}
or be a void tag, ending with ;
.
!
define
println!;
An id and multiple classes can be applied to an element using CSS like selectors.
The value after #
and .
can be either an identifier, a literal string, or an expression inside braces.
!
define
println!;
Attributes can either be normal or boolean (ends with ?). The name can be an identifier, a literal string, or an expression inside braces. Boolean attributes are printed without value if true and omitted if false. The value can be an Option, where None values are omitted and Some are unwrapped.
!
define
println!;
An element can have zero or more children inside braces.
!
define
println!;
One1 Two2
Automatic HTML escaping can be disabled using the markup::raw
function.
This function accepts any type implementing std::fmt::Display.
!
define
println!;
<&">
A template can accept simple arguments as well as generic arguments with where clauses. The arguments may have an optional trailing comma.
!
define
println!;
3hello
!
define
println!;
(1, 2)arg2str
Other templates can be embedded by simply putting them in braces.
!
define
println!;
3 7
@if
!
define
println!;
-42 is negative.
0 is zero.
42 is positive.
@if let
!
define
println!;
None
Some(ZERO)
Some(1)
@match
!
define
println!;
None
Other
1 or 2
1 or 2
33
@for
!
define
println!;
1 * 2 = 2;
2 * 2 = 4;
3 * 2 = 6;
4 * 2 = 8;
Curly braces also accept single statements and items and outputs it as-is in the generated code.
!
define
println!;
2