You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
### Issue: Parameter Evaluation in Python Function Rendering with Pystache#### Description
When rendering a Python function with Pystache, parameters are passed as strings without evaluating them within the function context. This limits the ability to dynamically render content based on function evaluations.
#### Example Code Comparison
The example below demonstrates the intended behavior using Chevron, where the `render` method correctly evaluates the function’s return values.
**Pystache's Approach**
The following `render` method in Pystache illustrates how lambdas are handled but lacks parameter evaluation:
```pythondefrender(self, engine, context):
values = engine.fetch_section_data(context, self.key)
parts = []
for val in values:
ifcallable(val):
# Special case for lambda functions in sections:# The lambda should be treated as an arity-1 function and be passed a string with unprocessed content.
val = val(self.template[self.index_begin : self.index_end])
val = engine._render_value(val, context, delimiters=self.delimiters)
parts.append(val)
continue
context.push(val)
parts.append(self.parsed.render(engine, context))
context.pop()
return''.join(parts)
Chevron’s Approach
In contrast, Chevron passes a render function that directly evaluates values within the template. Here’s an example:
importchevrondeffirst(text, render):
# Return only the first occurrence of itemsresult=render(text)
return [x.strip() forxinresult.split(" || ") ifx.strip()][0]
definject_x(text, render):
# Inject data into scopereturnrender(text, {'x': 'data'})
args= {
'template': 'Hello, {{# first}} {{x}} || {{y}} || {{z}} {{/ first}}! {{# inject_x}} {{x}} {{/ inject_x}}',
'data': {
'y': 'foo',
'z': 'bar',
'first': first,
'inject_x': inject_x
}
}
chevron.render(**args)
The text was updated successfully, but these errors were encountered:
Chevron’s Approach
In contrast, Chevron passes a
render
function that directly evaluates values within the template. Here’s an example:The text was updated successfully, but these errors were encountered: