Skip to content

Question: supporting object literals with unknown keys on custom filters #507

Open
@juanazam

Description

@juanazam

Hello!

I'm working on supporting Shopify's t filter (translation filter).

Here there is a simple a example of how it works. If we have the following translations file

/locales/en.default.json

{
  "layout": {
    "header": {
      "hello_user": "Hello {{ name }}!"
    }
  }
}

And the following liquid template:

<h1>{{ 'layout.header.hello_user' | t: name: customer.first_name }}</h1>

We would expect this example to render (assuming the first name of the customer is John):

<h1>Hello John</h1>

I'm running into the following issue when implementing this as a custom filter.
This is how I'm defining the filter's parameters:

#[derive(Debug, FilterParameters)]
struct TArgs {
    #[parameter(
        description = "Variables to be used for revaluating liquid once translation is resolved.",
    )]
    variables: Option<Expression>,
}

#[derive(Clone, ParseFilter, FilterReflection)]
#[filter(
    name = "t",
    description = "Access the desired translation for the recipient local",
    parameters(TArgs),
    parsed(TFilter)
)]
pub struct T;

#[derive(Debug, FromFilterParameters, Display_filter)]
#[name = "t"]
struct TFilter {
    #[parameters]
    args: TArgs,
}

But the problem is that when I run the same example I get the following error:

 "liquid: Unexpected named argument `name`\nfrom: Filter parsing error\n  with:\n    filter=t: name: customer.name\n"}

I haven't been able to get the filter working with object literals with arbitrary keys. What's the best way to accomplish this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions