#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

#517 in Template engine

Download history 5/week @ 2023-12-18 9/week @ 2023-12-25 7/week @ 2024-01-01 9/week @ 2024-01-08 9/week @ 2024-02-26 1/week @ 2024-03-11 46/week @ 2024-03-25 66/week @ 2024-04-01

113 downloads per month

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