Crates.io | markup-proc-macro |
lib.rs | markup-proc-macro |
version | 0.15.0 |
source | src |
created_at | 2018-12-03 08:50:56.6005 |
updated_at | 2023-11-23 07:50:58.276052 |
description | A blazing fast, type-safe template engine for Rust. |
homepage | https://github.com/utkarshkukreti/markup.rs |
repository | https://github.com/utkarshkukreti/markup.rs |
max_upload_size | |
id | 99802 |
size | 42,299 |
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.
[dependencies]
markup = "0.15.0"
We have integration examples for the following web frameworks:
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"
}
)
}
<!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>
<!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>
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 |
|
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 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 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
works similar to Rust.
Code |
---|
|
Output |
|
@match
works similar to Rust, but the branches must be wrapped in braces.
Code |
---|
|
Output |
|
@for
works similar to Rust.
Code |
---|
|
Output |
|
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 |
|