GRMustache.swift icon indicating copy to clipboard operation
GRMustache.swift copied to clipboard

Lambdas with arguments

Open mbalmer opened this issue 11 months ago • 2 comments

We use this to send data to line printers (receipt printers).

With filters and lambdas we can already to a lot, e.g.

{{ ean13(barcode) }}

with produce the ESC/POS sequence to print {{barcode}} as an EAN13 symbol on an ESC/POS printer.

Now it would be nice to have filters accept more than one argument, e.g. to place a string in a field of fixed with:

{{ width(40, product.name) }}

would be sth like printf("%40s", product_name)

And them even lambdas that take aribitrary arguments, but not from the context:

{{ qrCodeErrorCorrectionLevel('l') }}

mbalmer avatar Apr 28 '25 17:04 mbalmer

@mbalmer Thank you for your suggestion!

The kind of syntax you’re proposing (e.g. {{ width(40, product.name) }} or {{ qrCodeErrorCorrectionLevel('l') }}) falls outside the scope of what Mustache is designed for, as it goes against the “logic-less” philosophy that underpins the template engine.

As such, this isn’t something we plan to support in the core of GRMustache.swift.

That said, Mustache in Swift is extensible, and it’s definitely possible to achieve similar results using custom lambdas or value providers. If you’re interested, I encourage you to try implementing this as an extension yourself.

fumito-ito avatar May 03 '25 08:05 fumito-ito

Am 03.05.2025 um 10:09 schrieb Fumito Ito @.***>:

fumito-ito left a comment (groue/GRMustache.swift#107) https://github.com/groue/GRMustache.swift/issues/107#issuecomment-2848503288 @mbalmer https://github.com/mbalmer Thank you for your suggestion!

The kind of syntax you’re proposing (e.g. {{ width(40, product.name) }} or {{ qrCodeErrorCorrectionLevel('l') }}) falls outside the scope of what Mustache is designed for, as it goes against the “logic-less” philosophy that underpins the template engine.

Thank you for your reply.

I think you reasoning does only make limited sense: you allow the definition of user defined functions, lambdas, but the you prevent to pass any arguments to them.

So for now I to use an ugly hack to left align a text field in a field of 40 characters wide:

{{ left(name) }}40{{ /}}

This is abusing Mustache, imho. Having lambdas with more than just one or more MustacheBox(es) as parameter(s) would be useful.

I will see if I can fork Mustache and add this myself, but I would have preferred having such a mechanism in the main repo.

As such, this isn’t something we plan to support in the core of GRMustache.swift.

That said, Mustache in Swift is extensible, and it’s definitely possible to achieve similar results using custom lambdas or value providers. If you’re interested, I encourage you to try implementing this as an extension yourself.

Will see how far I get….

mbalmer avatar May 03 '25 09:05 mbalmer