35个发布版
0.15.0 | 2023年11月23日 |
---|---|
0.13.1 | 2022年6月27日 |
0.12.5 | 2021年9月11日 |
0.12.2 | 2021年5月24日 |
0.1.1 | 2018年12月3日 |
在 过程宏 中排名第 692
每月下载量 5,351
在 3 个crate中使用 (通过 markup)
43KB
806 行 (不包括注释)
markup.rs
是一个由过程宏驱动的模板引擎,它在编译时解析模板,并在运行时生成最优的Rust代码以渲染模板。模板可以嵌入Rust代码,这些代码由Rust编译器进行类型检查,从而实现完全的类型安全。
特性
- 完全类型安全,当使用类似 rust-analyzer 的编辑器扩展时,会显示内联错误高亮。
- 语法简洁,受 Haml、Slim 和 Pug 启发。
- 无任何不安全代码。
- 无任何运行时依赖。
- ⚡ 非常快。在 此基准测试 中,在不使用不安全代码的过程宏中速度最快,总体排名第二。
安装
[dependencies]
markup = "0.15.0"
框架集成
以下Web框架的集成示例:
快速示例
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>
语法参考 (WIP)
markup::define! 和 markup::new!
定义模板有两种方式: markup::define!
和 markup::new!
。
markup::define!
定义了一个带有命名参数的模板。这些模板无法访问外部作用域中的变量。模板可以具有泛型参数。在底层,markup::define!
编译为 Rust 结构体,该结构体实现了 markup::Render
和 std::fmt::Display
特性。
代码 |
---|
|
输出 |
|
markup::new!
定义了一个不带任何参数的模板。这些模板可以访问外部作用域中的变量。
代码 |
---|
|
输出 |
|
表达式
模板可以有裸字面值,它们将按原样渲染。它们还可以有以 @
符号开头的表达式(包括函数和宏调用)。所有字符串都是 HTML 转义,除非它们被 markup::raw()
包裹。
代码 |
---|
|
输出 |
|
元素
元素使用类似于 CSS 选择器的语法定义。元素可以包含在大括号中的嵌套元素,或者后面跟一个分号以实现自闭合元素。
代码 |
---|
|
输出 |
|
属性
属性在元素名称之后定义。可以使用类似于 CSS 选择器的语法定义 id
和 class
属性,使用 #
和 .
。类可以使用这种简写语法指定多次。其他属性在方括号中指定。
代码 |
---|
|
输出 |
|
@if 和 @if let
@if
和 @if let
与 Rust 的工作方式相似。
代码 |
---|
|
输出 |
|
@match
@match
与 Rust 的工作方式相似,但分支必须用大括号包裹。
代码 |
---|
|
输出 |
|
@for
@for
与 Rust 的工作方式相似。
代码 |
---|
|
输出 |
|
语句
模板可以包含以 @
符号开头的语句。最有用的这种语句是 @let
来计算一个稍后可以重用的值。可以使用 @fn
定义一个函数。还支持 @struct
、@mod
、@impl
、@const
、@static
以及更多。
代码 |
---|
|
输出 |
|
依赖关系
~280–730KB
~18K SLoC