Liquid-rust:Rust 的 Liquid 模板引擎
Liquid-rust: Liquid templating for Rust

原始链接: https://github.com/cobalt-org/liquid-rust

## liquid-rust:Rust 的合规 Liquid 模板引擎 `liquid-rust` 旨在成为一个高度**合规**、**灵活**和**高性能**的 Rust Liquid 模板引擎,紧密遵循 Shopify/Liquid 规范——任何偏差都将被视为错误。 该库易于通过 Cargo 集成到 Rust 项目中。它提供了一种可定制的方法,从一个最小的核心开始,允许开发者选择性地启用默认过滤器、标签和块,或创建自己的。 **可扩展性**是一个关键特性: * **过滤器** 作为转换输入值的函数实现。 * **标签** 涉及初始化和渲染阶段,使用提供的函数/闭包。 * **块** 类似于标签,但处理块*内部*的标记。 `liquid-rust` 采用 MIT 或 Apache 2.0 许可协议双重许可,为用户和贡献者提供灵活性。 详细示例和 Liquid 语法的参考文档可帮助开发。

## Liquid-rust:一个 Rust 模板引擎 一个新的 Rust 实现的 Liquid 模板语言“liquid-rust” 正在受到关注,可能成为 GitHub Pages 中 Ruby-based Jekyll+Liquid 的替代品。维护者提到希望提高性能和可扩展性,并正在探索像 Koto 嵌入式语言这样的选项。 讨论强调了 Liquid 的关键特性:由于其功能受到有意限制,因此可以安全地执行客户提供的代码。虽然像 `rust-sailfish` 这样的替代方案允许在模板中使用任意 Rust 代码,但 `liquid-rust` 优先考虑与现有 Liquid 标准的兼容性——这对于庞大的 GitHub Pages 用户群至关重要——同时致力于性能改进。 值得注意的是,使用 `liquid-rust` 编译后,静态博客发布速度足够快,可以在 iOS 设备上使用,解决了 Ruby 版本中的性能问题。开发者也在考虑与现有项目的集成,并探索基准测试以进行进一步优化。
相关文章

原文

Liquid templating for Rust

Crates Status

Goals:

  1. Conformant. Incompatibilities with strict shopify/liquid are bugs to be fixed.
  2. Flexible. Liquid embraces variants for different domains and we want to follow in that spirit.
  3. Performant. Do the best we can within what is conformant.

Example applications using liquid-rust:

To include liquid in your project add the following to your Cargo.toml:

Example:

let template = liquid::ParserBuilder::with_stdlib()
    .build().unwrap()
    .parse("Liquid! {{num | minus: 2}}").unwrap();

let globals = liquid::object!({
    "num": 4f64
});

let output = template.render(&globals).unwrap();
assert_eq!(output, "Liquid! 2".to_string());

You can find a reference on Liquid syntax here.

By default, liquid-rust has no filters, tags, or blocks. You can enable the default set or pick and choose which to add to suit your application.

Creating your own filters is very easy. Filters are simply functions or closures that take an input Value and a Vec<Value> of optional arguments and return a Value to be rendered or consumed by chained filters.

See filters/ for what a filter implementation looks like. You can then register it by calling liquid::ParserBuilder::filter.

Tags are made up of two parts, the initialization and the rendering.

Initialization happens when the parser hits a Liquid tag that has your designated name. You will have to specify a function or closure that will then return a Renderable object to do the rendering.

See include_tag.rs for what a tag implementation looks like. You can then register it by calling liquid::ParserBuilder::tag.

Create your own tag blocks

Blocks work very similar to Tags. The only difference is that blocks contain other markup, which is why block initialization functions take another argument, a list of Elements that are inside the specified block.

See comment_block.rs for what a block implementation looks like. You can then register it by calling liquid::ParserBuilder::block.

Licensed under either of

at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual-licensed as above, without any additional terms or conditions.

联系我们 contact @ memedata.com