Replies: 1 comment
-
Hi @twillis,
For object property access, your class need to implement the import collections.abc
from liquid import Environment
class TestObject(collections.abc.Mapping):
def __init__(self, name, value):
self._data = {
"name": name,
"value": value,
"computed": f"computed_{value}",
}
self.liquid_called = False
self.getitem_called = False
def __liquid__(self):
"""Return a Liquid primitive value for this object."""
print("__liquid__ called")
self.liquid_called = True
return 2
def __getitem__(self, k):
print(f"__getitem__ called on {k}")
self.getitem_called = True
# We're simply wrapping a dictionary in this example.
# You could delegate to a method or read from a database here.
return self._data[k]
def __iter__(self):
return iter(self._data)
def __len__(self):
return len(self._data)
env = Environment()
obj = TestObject("test_object", "test_value")
template = env.from_string(
"Name: {{obj.name}}, Value: {{obj.value}}, Array Item: {{some_list[obj]}}"
)
result = template.render(obj=obj, some_list=["a", "b", "c"])
print(f"Result: {result}") # Result: Name: test_object, Value: test_value
print(f"Was __liquid__ called? {obj.liquid_called}") # True
print(f"Was __getitem__ called? {obj.getitem_called}") # True The docs do mention using |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Description
The python-liquid library documentation states that objects can implement a
__liquid__
method that will be called automatically when the object is accessed in liquid templates. However, this functionality does not work as documented - the__liquid__
method is never called for property access like{{obj.name}}
.Steps to Reproduce
__liquid__
method that returns a dictionary__liquid__
is never called and properties are emptyReproduction Code
See attached
liquid_bug_reproduction.py
:Expected Behavior
{{obj.name}}
should callobj.__liquid__()
{'name': 'test_object', 'value': 'test_value', 'computed': 'computed_test_value'}
should be used for property lookupsName: test_object, Value: test_value
Actual Behavior
__liquid__
method is never calledName: , Value:
Source Code Analysis
From examining the python-liquid source code, the
__liquid__
method is only referenced in thedefault
filter implementation, not in the core property resolution logic. This suggests the feature was partially implemented but not integrated into the main template evaluation system.Environment
Impact
This bug prevents the intended object serialization workflow, forcing developers to manually convert objects to dictionaries before passing them to templates. This defeats the purpose of the
__liquid__
method and breaks the documented API contract.Suggested Fix
The
__liquid__
method should be checked and called in the core property resolution logic, similar to how Django templates handle custom object serialization methods.Files Attached
liquid_bug_reproduction.py
- Minimal reproduction scriptliquid_bug_documentation.md
- Detailed analysis and test resultsWorkaround
Currently, objects must be manually converted:
Beta Was this translation helpful? Give feedback.
All reactions