mustache / mustache.github.com

The {{official}} website
http://mustache.github.io/
Other
2.32k stars 293 forks source link

Mustache substitution minimum string length #128

Closed leonardopsantos closed 11 months ago

leonardopsantos commented 4 years ago

I'm writing a template that eventually will render C structures. For that, I want to set the minimum substitution length, pretty much like printf("%10s") would do, so I get aligned members in my header file.

Is there a way to set the minimum length of a string when it's substituted or I have to do it on my context?

context = {
    'group' : [
        {'name' : 'Bob',
         'id'   : 2345},
        {'name' : 'Alice',
         'id'   : 4567}
    ],
    'id' : 1234
}

template =\
'''
The group id is {{id}}
{{#group}}
Name: {{name}}, id {{id}}
{{/group}}
'''

renderer = pystache.Renderer()
output = renderer.render(template, context)
print(output)

I would like to render it like:

The group id is 1234
Name: Bob,   id 2345
Name: Alice, id 4567

I know I could pass the resulting header file in a beautifier tool like indent or uncrustify, but I'm curious if it can be done in Mustache.

Thanks!!

jgonggrijp commented 11 months ago

Some Mustache implementations support filters, which would enable you to do such a thing within the template. I don't know whether this is also the case for pystache. Failing filters, you can preprocess the data so they are already formatted when interpolation happens.

Filters have not been standardized yet, you can find previous discussion about them via https://github.com/mustache/spec/discussions/153.

Closing this, as this repository is only about the Mustache website, not about the template language.