noahmorrison / chevron

A Python implementation of mustache
MIT License
486 stars 52 forks source link

Support Section Lambdas #27

Closed dunkelstern closed 5 years ago

dunkelstern commented 6 years ago

Currently if a section is a function, the function will not be called.

Example:

template = '{{{postcode}}} {{#first}} {{{city}}} || {{{town}}} || {{{village}}} || {{{state}}} {{/first}}'

So i define first to be a function that does something to the content of the section

def first(content):
    return "not implemented"

data = {
    "postcode": "1234",
    "city": "Mustache City",
    "state": "Nowhere",
    "first": first,
}

print(chevron.render(template, data))

What currently happens: 1234 || || || What I intend to happen: 1234 not implemented

Of course in reality when implementing the first function proper, i would like to have: 1234 Mustache City

This is an example from https://github.com/OpenCageData/address-formatting, they use a yaml file for defining how to format an address for another country which contains mustache templates.

noahmorrison commented 5 years ago

Thanks a lot to @klorenz for implementing this in PR #27!

dunkelstern commented 5 years ago

Thanks a lot from me too :)