#模板引擎 #ramhorns #ext #mustache #pure #content #md

ramhorns-derive-ext

实验性的类似Mustache的模板引擎

6个版本 (3个重大变更)

0.17.2 2023年10月30日
0.17.1 2023年10月29日
0.16.0 2023年10月28日
0.15.0 2023年10月28日
0.14.0 2023年10月21日

#1277进程宏

Download history 7/week @ 2024-03-11 1/week @ 2024-03-18 4/week @ 2024-03-25 20/week @ 2024-04-01 2/week @ 2024-05-20

每月81次下载
ramhorns-ext 中使用

GPL-3.0 许可

20KB
286

纯Rust实现的快速Mustache模板引擎。

Ramhorns-ext 基于 Ramhorns,它会在运行时加载和处理模板。它附带一个 derive 宏,允许从原生Rust数据结构渲染模板,无需进行临时分配、中间 HashMap 或其他类似操作。

加上一点魔法 🎩、友情的力量 🥂 和一点 FNV哈希 ✨,渲染时间可以轻松与静态模板引擎(如 Askama)相媲美。

你还需要什么,一个贴纸吗?

Cargo.toml

[dependencies]
ramhorns-ext = { version = "0.17", features = ["chrono", "uuid"] }

示例

use ramhorns_ext::{Template, Content};

#[derive(Content)]
struct Post<'a> {
    id: uuid::Uuid,
    create_time: chrono::NaiveDateTime,
    title: &'a str,
    teaser: &'a str,
}

#[derive(Content)]
struct Blog<'a> {
    title: String,        // Strings are cool
    posts: Vec<Post<'a>>, // &'a [Post<'a>] would work too
}

// Standard Mustache action here
let source = "<h1>{{title}}</h1>\
              {{#posts}}<article><h2>{{id}}</h2><h2>{{title}}</h2><p>{{teaser}}</p></article>{{/posts}}\
              {{^posts}}<p>No posts yet :(</p>{{/posts}}";

let tpl = Template::new(source).unwrap();

let rendered = tpl.render(&Blog {
    title: "My Awesome Blog!".to_string(),
    posts: vec![
        Post {
            id: uuid::Uuid::parse_str("02f09a3f-1624-3b1d-8409-44eff7708201").unwrap(),
            create_time: chrono::Utc::now().naive_utc(),
            title: "How I tried Ramhorns and found love 💖",
            teaser: "This can happen to you too",
        },
        Post {
            id: uuid::Uuid::parse_str("12f09a3f-1624-3b1d-8409-44eff7708202").unwrap(),
            create_time: chrono::Utc::now().naive_utc(),
            title: "Rust is kinda awesome",
            teaser: "Yes, even the borrow checker! 🦀",
        },
    ]
});

assert_eq!(rendered, "<h1>My Awesome Blog!</h1>\
                      <article>\
                          <h2>02f09a3f-1624-3b1d-8409-44eff7708201</h2>\
                          <h2>How I tried Ramhorns and found love 💖</h2>\
                          <p>This can happen to you too</p>\
                      </article>\
                      <article>\
                          <h2>12f09a3f-1624-3b1d-8409-44eff7708202</h2>\
                          <h2>Rust is kinda awesome</h2>\
                          <p>Yes, even the borrow checker! 🦀</p>\
                      </article>");

功能

  • Vec等使用{{$value}}。
  • 支持uuid和chrono DateTime
  • 渲染代码块 {{?foo}} ... {{/foo}}
  • 渲染常见类型,如 &strStringbool 和数字到 {{变量}}
  • 使用未转义的打印方法,例如 {{{tripple-brace}}}{{&ampersant}}
  • 渲染部分 {{#foo}} ... {{/foo}}
  • 渲染逆部分 {{^foo}} ... {{/foo}}
  • 渲染部分 {{>file.html}}
  • 从带有标记的 #[md] 的字段中进行零拷贝 CommonMark 渲染。

基准测试

渲染一个微型模板

test a_simple_ramhorns            ... bench:          82 ns/iter (+/- 4) = 1182 MB/s
test b_simple_askama              ... bench:         178 ns/iter (+/- 8) = 544 MB/s
test c_simple_tera                ... bench:         416 ns/iter (+/- 98) = 233 MB/s
test c_simple_tera_from_serialize ... bench:         616 ns/iter (+/- 33) = 157 MB/s
test d_simple_mustache            ... bench:         613 ns/iter (+/- 34) = 158 MB/s
test e_simple_handlebars          ... bench:         847 ns/iter (+/- 40) = 114 MB/s

带有部分的微型模板渲染

test pa_partials_ramhorns         ... bench:          85 ns/iter (+/- 7) = 1141 MB/s
test pb_partials_askama           ... bench:         210 ns/iter (+/- 9) = 461 MB/s
test pc_partials_mustache         ... bench:         827 ns/iter (+/- 39) = 117 MB/s
test pd_partials_handlebars       ... bench:         846 ns/iter (+/- 29) = 114 MB/s

从字符串编译模板

test xa_parse_ramhorns            ... bench:         190 ns/iter (+/- 10) = 821 MB/s
test xb_parse_mustache            ... bench:       3,229 ns/iter (+/- 159) = 48 MB/s
test xe_parse_handlebars          ... bench:       6,883 ns/iter (+/- 383) = 22 MB/s

值得注意的是,Askama 在编译时处理模板,并为渲染生成静态 Rust 代码。这对于性能来说很棒,但也意味着您无法在不重新编译 Rust 二进制文件的情况下更换模板。在某些情况下,例如对于静态网站生成器,这不幸地是一个交易破坏者。

在运行时解析模板永远都不会是免费的,但是 Ramhorns 基于 构建了一个非常快的解析器 Logos,这使得整个过程的这一部分也非常快。

设计上和功能集最接近 RamhornsMustache crate

许可

Ramhorns 是免费软件,并按照 GNU 通用公共许可证版本 3 的条款发布。请参阅 LICENSE

依赖关系

~2MB
~44K SLoC