hummingbird-project / swift-mustache

Mustache templating engine
Apache License 2.0
21 stars 2 forks source link

Add or override transforms for a type the library already provides an implementation for? #45

Open CallumTodd7 opened 2 months ago

CallumTodd7 commented 2 months ago

I'm struggling to add a custom transform for a string value. Is this supported by the library?

import Mustache

public extension StringProtocol {
  func transform(_ name: String) -> Any? {
    switch name {
    case "appendWithCake":
      self + "cake"
    default:
      nil
    }
  }
}

extension String: MustacheTransformable {}
extension Substring: MustacheTransformable {}
let template = try MustacheTemplate(string: """
{{ appendWithCake(value) }}
""")
let object: [String: Any] = ["value": "Cup"]
XCTAssertEqual(template.render(object), "Cupcake") // Actual: ""
adam-fowler commented 2 months ago

Currently it isn't possible to add additional transforms to a type that already has transforms. What is the transform you want to add? I assume it isn't the appendWithCake function.

CallumTodd7 commented 2 months ago

Ah, that's a shame. Thanks for the reply.

I was looking to add a UTF8 base64 encoding transform. Not a huge issue for me now as I've got it working without transforms.