#liquid #template #templating

liquid-filter-commafy

Filter for the liquid template engine to commafy a number (put comma after every 3 digtist from right to left)

1 unstable release

0.1.1 Oct 3, 2023

#425 in Template engine

MIT/Apache

5KB
51 lines

Liquid filter for Rust to commafy a number (put comma after every 3 digtist from right to left)

The liquid crate, the Rust implementation of the liquid template system has many filters to manipulate the data in the template, but AFAIK there is no filter to commafy a number.

Usage:

  • Cargo.toml:
[dependencies]
liquid = "0.26"
liquid-filter-reverse-string = "0.1"
  • src/main.rs:
use liquid_filter_commafy::Commafy;

fn main() {
    println!("{}", render("{{value | commafy}}", liquid::object!({ "value": "2345" })));
    println!("{}", 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()
}

The important pieces:

The use statement:

use liquid_filter_commafy::Commafy;

The use of the commafy filter in the template:

let template = "{{value | commafy}}";
  • adding the filter to the engine:
  .filter(Commafy)

Release

  • update the version number in Cargo.toml
cargo publish
git tag -a v0.1.0 -m v0.1.0
git push --tags

Dependencies

~6–8MB
~153K SLoC