-
Notifications
You must be signed in to change notification settings - Fork 78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question: supporting object literals with unknown keys on custom filters #507
Comments
Hi @epage, sorry to bother you, do you have any insights on how I might be able to achieve this? |
Hi @epage, sorry to ping you directly again here, do you know if there are any easy workarounds for this issue? |
I made a custom use liquid_core::{Display_filter, Filter, FilterReflection, ParseFilter};
use liquid_core::{Error, Result, Runtime};
use liquid_core::{Value, ValueView};
#[derive(Clone, ParseFilter, FilterReflection)]
#[filter(
name = "json",
description = "Convert a JSON string into a liquid object",
parsed(JsonFilter)
)]
pub struct Json;
#[derive(Debug, Default, Display_filter)]
#[name = "json"]
pub struct JsonFilter {}
impl Filter for JsonFilter {
fn evaluate(&self, input: &dyn ValueView, _: &dyn Runtime) -> Result<Value> {
serde_json::from_str(&input.to_kstr().as_str()).map_err(|e| Error::with_msg(e.to_string()))
}
} Usage in template: {% assign obj = '{"name": "john"}' | json %} |
Hi @TomzBench, thanks for your reply! |
Hey @TomzBench this won't work as I would like because of two things.
The example I shared above captures what I need:
|
per the If you have this JSON local file. {
"blog": {
"comment": {
"email": "Su correo electrónico"
}
}
} You could do {{ assign local = '{ "blog": { "comment": ... }' | json }}
{{ "blog.comment.email" | t : local }} If you dont want to pass the You need another scheme to parse the dot notation on the input. |
I'm pretty sure you can, the example I shared above came from the same docs on the interpolation section (link). Regarding the file, I'm already passing it as a global, that's not the issue. Using liquid-rust you can either define parameters as positional or named. If you define them as named, you need to know all keywords in advance which doesn't for me, because the object literal should support any arbitrary object literal. I haven't been able to figure out if there is a way of bypassing this restriction. |
woops, looks like you're right
I couldn't find in the docs where you can even pass named parameters. I have been restricting myself to positional and json objects when I have complex args. So if you absolutely need dynamically keyed values and can't use a json string then I think this crate can't do that is my guess. The maintainer doesn't seem available for comment though. Good luck! |
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
And the following liquid template:
We would expect this example to render (assuming the first name of the customer is John):
I'm running into the following issue when implementing this as a custom filter.
This is how I'm defining the filter's parameters:
But the problem is that when I run the same example I get the following error:
I haven't been able to get the filter working with object literals with arbitrary keys. What's the best way to accomplish this?
The text was updated successfully, but these errors were encountered: