Liquid: create your own filter: reverse a string


This is using the liquid-filter-reverse-string. Look at its source code


examples/liquid/liquid-filter-reverse-string-use/Cargo.toml
[package]
name = "liquid-filter-reverse-string-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-reverse-string = "0.1"

examples/liquid/liquid-filter-reverse-string-use/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();

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