chevron icon indicating copy to clipboard operation
chevron copied to clipboard

Interpolation when the key mirrors a built-in `str` method

Open tdy opened this issue 2 years ago • 1 comments

If the mustache key mirrors a built-in str method (e.g., title), chevron doesn't interpolate the conditional value:

args = {
    'template': '{{#title}}{{title}}{{/title}}',
    'data': {'title': 'foo'},
}
chevron.render(**args)

# '<built-in method title of str object at 0x7f5007b6d430>'

Is this expected or a bug?

Currently I'm using a lambda workaround, but not sure if I'm missing something obvious:

args = {
    'template': '{{#title}}{{title}}{{/title}}',
    'data': {'title': lambda x, render: render(x, {'title': 'foo'})},
}
chevron.render(**args)

# foo

(chevron version 0.14.0)

tdy avatar Jan 21 '23 04:01 tdy

Another workaround:

>>> chevron.render('{{#title}}{{title}}{{/title}}', {'title': {'title':'bob'}})
'bob'

As further evidence that this shouldn't be the expected behavior in Chevron, note the inconsistency otherwise:

>>> chevron.render('{{#title}}{{title}}{{/title}}', {'title': 'bob'})
'<built-in method title of str object at 0x7efd4d6bfe70>'
>>> chevron.render('{{#foo}}{{foo}}{{/foo}}', {'foo': 'bob'})
'bob'

echuber2 avatar Jan 21 '23 20:01 echuber2