Skip to content
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

Lamda Support #30

Open
arshanaslam1 opened this issue Oct 28, 2024 · 1 comment
Open

Lamda Support #30

arshanaslam1 opened this issue Oct 28, 2024 · 1 comment

Comments

@arshanaslam1
Copy link

arshanaslam1 commented Oct 28, 2024

### 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:

```python
def render(self, engine, context):
    values = engine.fetch_section_data(context, self.key)

    parts = []
    for val in values:
        if callable(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:

import chevron

def first(text, render):
    # Return only the first occurrence of items
    result = render(text)
    return [x.strip() for x in result.split(" || ") if x.strip()][0]

def inject_x(text, render):
    # Inject data into scope
    return render(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)
@bakert
Copy link
Member

bakert commented Oct 28, 2024

Is this a bug (it's against the Mustache spec) or a preference in how it should work? Not ruling out changing it either way I'm just curious.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants