11次发布
0.2.3 | 2024年4月14日 |
---|---|
0.2.2 | 2024年3月1日 |
0.2.1 | 2024年1月20日 |
0.1.6 | 2024年1月16日 |
#122 in 模板引擎
781 每月下载量
49KB
1K SLoC
templr
一个受templ启发的Rust模板引擎。templr使用宏在编译时从模板生成Rust代码。
特性
- 模板实例化(带有children!)
- 传递信息的上下文
- Rust样式的
for
、if-else
、if-let
、match
和let
语句(允许在起始标签中使用) - 完整的Rust变量
- 可选的HTML转义退出
- 友好的属性
- 我们有一个格式化器:templrfmt!
- 可选的内置对Actix-Web、Axum、Gotham、Rocket、Salvo、Tide和Warp Web框架的支持。
如何开始
首先,将templr依赖项添加到您的crate的Cargo.toml
cargo add templr
在您的crate中的任何Rust文件内,添加以下内容
use templr::{Template, templ, templ_ret};
pub fn hello(name: &str) -> templ_ret!['_] {
templ! {
<p>Hello, {name}!</p>
}
}
fn main() {
let html = hello("world").render(&()).unwrap();
println!("{html}");
}
您应该能够编译并运行此代码。
依赖项
~0.4–35MB
~547K SLoC