Liquid: create your own filter: commafy



examples/liquid/liquid-filter-commafy-use/Cargo.toml
[package]
name = "liquid-filter-commafy-use"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
liquid = "0.26"
liquid-filter-commafy = "0.1"

examples/liquid/liquid-filter-commafy-use/src/main.rs
use liquid_filter_commafy::Commafy;

fn main() {
    assert_eq!(
        "2,345",
        render("{{value | commafy}}", liquid::object!({ "value": 2345 }))
    );
    assert_eq!(
        "123,456",
        render("{{value | commafy}}", liquid::object!({ "value": 123456 }))
    );
    assert_eq!(
        "123,456",
        render(
            "{{value | commafy}}",
            liquid::object!({ "value": "123456" })
        )
    );
}

fn render(tmpl: &str, glob: liquid::Object) -> String {
    let template = liquid::ParserBuilder::with_stdlib()
        .filter(Commafy)
        .build()
        .unwrap()
        .parse(tmpl)
        .unwrap();

    template.render(&glob).unwrap()
}