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 日 |
#28 在 模板引擎 中
3,278 每月下载量
用于 5 个 包 (4 个直接使用)
28KB
377 行
markup.rs
是一个由过程宏驱动的 Rust 模板引擎,它会在编译时解析模板,并在运行时生成最优的 Rust 代码以渲染模板。模板可以嵌入 Rust 代码,并由 Rust 编译器进行类型检查,从而实现完全的类型安全。
特性
- 完全类型安全,当使用像 rust-analyzer 这样的编辑器扩展时,会在行内高亮显示错误。
- 语法简洁,受 Haml、Slim 和 Pug 启发。
- 零不安全代码。
- 零运行时依赖。
- ⚡ 闪电般快速。在不使用不安全代码的 benchmark 中,这是最快的,总体排名第二。
安装
[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!
编译为实现了 markup::Render
和 std::fmt::Display
特性的 Rust 结构。
代码 |
---|
|
输出 |
|
markup::new!
定义了一个不带任何参数的模板。这些模板可以访问外部作用域中的变量。
代码 |
---|
|
输出 |
|
表达式
模板可以包含裸文本值,它们将按原样渲染。也可以包含以 @
符号开头的表达式(包括函数和宏调用)。除非它们被 markup::raw()
包裹,否则所有字符串都将进行 HTML 转义。
代码 |
---|
|
输出 |
|
元素
元素使用类似于 CSS 选择器的语法定义。元素可以包含在大括号中的嵌套元素,或者用分号自闭合。
代码 |
---|
|
输出 |
|
属性
属性定义在元素名称之后。可以使用类似于 CSS 选择器的语法使用 #
和 .
定义 id
和 class
属性。类可以使用这种简写语法指定多次。其他属性用方括号指定。
代码 |
---|
|
输出 |
|
@if 和 @if let
@if
和 @if let
的工作方式与 Rust 类似。
代码 |
---|
|
输出 |
|
@match
@match
的工作方式与 Rust 类似,但分支必须用大括号包裹。
代码 |
---|
|
输出 |
|
@for
@for
的工作方式与 Rust 类似。
代码 |
---|
|
输出 |
|
语句
模板可以包含以 @
符号开头的语句。最有用的此类语句是 @let
,用于计算一个值以供以后重用。@fn
可以用来定义一个函数。还支持 @struct
、@mod
、@impl
、@const
、@static
等。
代码 |
---|
|
输出 |
|
依赖
~270–720KB
~17K SLoC