pwwang/liquidpy

Reverse not implemented correctly

Closed this issue · 6 comments

When trying to reverse this JSON
[
{
"Key": "2",
"A": "B"
},
{
"Key": "1",
"A": "B"
}
]
array we get this error [<list_reverseiterator object at 0x3eb14f722f70>]

Are you using the standard mode?

Could you share the python code how you call and render your template?

Are you using the standard mode?

Could you share the python code how you call and render your template?

Hey @pwwang!

We're running it in wild mode with python in the https://github.com/frikky/shuffle project, where this issue shows up. The code that runs it all can be found here: https://github.com/frikky/Shuffle/blob/581f97be8029da91ecd43c64abe1359ca1cb5a29/backend/app_sdk/app_base.py#L1767

Hope it helps! :)

@frikky Thanks for providing the information. It is helpful.

We didn't implement the reverse filter. It is actually a jinja filter:

https://jinja.palletsprojects.com/en/3.0.x/templates/#jinja-filters.reverse

It returns an iterator, instead of a list. If you want a list:

>>> from liquid import Liquid, defaults
>>> defaults.MODE = 'wild'
>>> defaults.FROM_FILE = False
>>> Liquid("{{x | reverse}}").render(x=[1,2,3])
'<list_reverseiterator object at 0x7ff91960c880>'
>>> Liquid("{{x | reverse | list}}").render(x=[1,2,3])
'[3, 2, 1]'

Assume it is solved. Closing.

Assume it is solved. Closing.

Thanks! Parsing straight to | list will indeed be our goto explainer for now.

Our docs are coming along btw. Here's an extensive list of what we aim to implement and use: Liquid docs. We will also add examples of each and every one of them long-term.

Quick Q about strings: How do you think we could solve the problem of e.g. running something with a filter, but the data is not a string? I'd ideally like to see some kind of auto-casting system, where it assumes something is a string (and e.g. adds the quotes itself), without the need of perfect information left of the pipe. Just wondering how that could be implemented well, as I'm not a parser expert ;)

So here it's a little bit confusing. For example:

out = Liquid("{{ x | plus: 1}}").render(x=1)
# out == "2"
# isinstance(out, str)

Are you talking about auto-casting "2" (str) to 2 (int) for the python variable out, or are you expecting the following case to work, instead of a TypeError?

out = Liquid("{{ x | plus: 1}}").render(x="1")
# TypeError: can only concatenate str (not "int") to str