cobalt-org / liquid-rust

Liquid templating for Rust
docs.rs/liquid
MIT License
473 stars 79 forks source link

Is there a way to map new filter to existing filter? #429

Open mckingho opened 3 years ago

mckingho commented 3 years ago

liquid-rust version: 0.21.4 rust version: 1.49.0 OS: Any

I want to add a custom filter called url_param_escape, which is exactly the same function of a standard filter url_encode in the module. Can I bind new filter name to existing filter so that both filter name are valid and function the same way?

Edit: This is my solution. originalFilter is the struct of original filter that impl Filter trait.

#[derive(Clone, ParseFilter, FilterReflection)]
#[filter(
    name = "alias_name",
    description = "Alias of original filter.",
    parsed(originalFilter)
)]
pub struct AliasFilter;
epage commented 3 years ago

All of the filters are exposed and can be added to the parser like the default set does: https://github.com/cobalt-org/liquid-rust/blob/master/src/parser.rs#L109

You'll have to use a newtype to give it a new name.

Whats the motivation for renaming?

mckingho commented 3 years ago

url_param_escape is a filter supported in shopify. It is very similar to url_encode. Before I posted this question, I want to find a way to do some alias mapping rather than duplicate all the codes from url_encode. Because I want to support both filters, not renaming.

But I just review shopify's document, there is slight difference between two filters. So I am going to write a custom one instead.

epage commented 3 years ago

Either way, if you are up for contributing it back, that'd be great! We are working on having the official liquid filters, shopify, jekyll, and custom, see https://github.com/cobalt-org/liquid-rust/tree/master/crates/lib/src