liquid-filter-commafy

Crates.ioliquid-filter-commafy
lib.rsliquid-filter-commafy
version0.1.1
sourcesrc
created_at2023-10-03 09:05:35.198238
updated_at2023-10-03 09:05:35.198238
descriptionFilter for the liquid template engine to commafy a number (put comma after every 3 digtist from right to left).
homepage
repositoryhttps://github.com/szabgab/liquid-filter-commafy.rs
max_upload_size
id990861
size4,988
Gábor Szabó (szabgab)

documentation

README

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
Commit count: 1

cargo fmt