35 releases
0.15.0 | Nov 23, 2023 |
---|---|
0.13.1 | Jun 27, 2022 |
0.12.5 | Sep 11, 2021 |
0.12.2 | May 24, 2021 |
0.1.1 | Dec 3, 2018 |
#33 in Template engine
3,436 downloads per month
Used in 5 crates
(4 directly)
28KB
377 lines
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
[dependencies]
markup = "0.15.0"
Framework Integration
We have integration examples for the following web frameworks:
Quick Example
markup::define! {
Home<'a>(title: &'a str) {
@markup::doctype()
html {
head {
title { @title }
style {
"body { background: #fafbfc; }"
"#main { padding: 2rem; }"
}
}
body {
@Header { title }
#main {
p {
"This domain is for use in illustrative examples in documents. You may \
use this domain in literature without prior coordination or asking for \
permission."
}
p {
a[href = "https://www.iana.org/domains/example"] {
"More information..."
}
}
}
@Footer { year: 2020 }
}
}
}
Header<'a>(title: &'a str) {
header {
h1 { @title }
}
}
Footer(year: u32) {
footer {
"(c) " @year
}
}
}
fn main() {
println!(
"{}",
Home {
title: "Example Domain"
}
)
}
Output
<!DOCTYPE html><html><head><title>Example Domain</title><style>body { background: #fafbfc; }#main { padding: 2rem; }</style></head><body><header><h1>Example Domain</h1></header><div id="main"><p>This domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission.</p><p><a href="https://www.iana.org/domains/example">More information...</a></p></div><footer>(c) 2020</footer></body></html>
Output (manually prettified)
<!doctype html>
<html>
<head>
<title>Example Domain</title>
<style>
body {
background: #fafbfc;
}
#main {
padding: 2rem;
}
</style>
</head>
<body>
<header><h1>Example Domain</h1></header>
<div id="main">
<p>
This domain is for use in illustrative examples in documents. You may
use this domain in literature without prior coordination or asking for
permission.
</p>
<p>
<a href="https://www.iana.org/domains/example">More information...</a>
</p>
</div>
<footer>(c) 2020</footer>
</body>
</html>
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.
Code |
---|
|
Output |
|
markup::new!
defines a template without any arguments. These can access variables from outer scope.
Code |
---|
|
Output |
|
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()
.
Code |
---|
|
Output |
|
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.
Code |
---|
|
Output |
|
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.
Code |
---|
|
Output |
|
@if and @if let
@if
and @if let
works similar to Rust.
Code |
---|
|
Output |
|
@match
@match
works similar to Rust, but the branches must be wrapped in braces.
Code |
---|
|
Output |
|
@for
@for
works similar to Rust.
Code |
---|
|
Output |
|
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.
Code |
---|
|
Output |
|
Dependencies
~265–710KB
~17K SLoC