liquid-filter-reverse-string

Crates.ioliquid-filter-reverse-string
lib.rsliquid-filter-reverse-string
version0.1.1
sourcesrc
created_at2023-10-03 07:27:22.124328
updated_at2023-10-03 07:40:06.57475
descriptionFilter for the liquid template engine to reverse a string.
homepage
repositoryhttps://github.com/szabgab/liquid-filter-reverse-string.rs
max_upload_size
id990807
size4,683
Gábor Szabó (szabgab)

documentation

README

Liquid filter for Rust to reverse a string

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 reverse a string.

This crate was originally developed as a demo on how to add a filter to liquid.

Usage:

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

use liquid_filter_reverse_string::ReverseStr;

fn main() {
    let template = "reversed: {{text | reversestr}}";
    let text = "Hello World!";

    let result = render(&template, &text);
    println!("{}", result);
    assert_eq!(result, "reversed: !dlroW olleH");
}


fn render(tmpl: &str, text: &str) -> String {
    let globals = liquid::object!({
        "text": text,
    });

    let template = liquid::ParserBuilder::with_stdlib()
        .filter(ReverseStr)
        .build()
        .unwrap()
        .parse(tmpl).unwrap();

    let output = template.render(&globals).unwrap();
    return output
}

The important pieces:

The use statement:

use liquid_filter_reverse_string::ReverseStr;

The use of the reversestr filter in the template

let template = "reversed: {{text | reversestr}}";
  • adding the filter to the engine:
  .filter(ReverseStr)

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: 6

cargo fmt