1

I am using https://github.com/cbroglie/mustache for rendering mustache file.I want basically this example to work in go lang.But I guess custom function is not there.

Template:

{{#wrapped}}
  {{name}} is awesome.
{{/wrapped}

}

{
  "name": "Willy",
  "wrapped": function() {
    return function(text, render) {
      return "<b>" + render(text) + "</b>"
    }
  }
}

Output:

Willy is awesome.

Basically I want to use my custom defined function to render mustache in go.How is this possible can anyone tell me.

  • I don't see anywhere where it says it supports that in the docs or README, nor anywhere such functionality is described in [the moustache spec](https://mustache.github.io/mustache.5.html), where are you getting this from? – Adrian Jun 09 '20 at 17:47
  • the moustache spec lambdas does that functionality but in go lang I am struggling.Syntax can be different.I am concerned over functionality. – Divanshu Aggarwal Jun 09 '20 at 18:19
  • Lamdas in the spec work absolutely nothing like what you described in the question. – Adrian Jun 09 '20 at 18:24
  • @Adrian {{#wrapped}} {{name}} is awesome. {{/wrapped}} produces Willy is awesome. it takes wrapped as Utility function without any binding context and produces o/p .I need similar functionality in golang. – Divanshu Aggarwal Jun 09 '20 at 18:31

1 Answers1

1

I don't see a direct way of doing this based on the documentation. But here's what i could get working. May be you can read more in the docs and try to get a better solution. Playground link here

    package main

    import (
        "fmt"
        "strings"

        "github.com/cbroglie/mustache"
    )

    type CustomString string

    func (s CustomString) ToLower() string {
        return strings.ToLower(string(s))
    }

    func main() {
        out, err := mustache.Render(`Hey {{#myString}}{{ToLower}}{{/myString}}`, map[string]interface{}{"myString": []CustomString{CustomString("dEF")}})
        if err != nil {
            panic(err)
        }

        fmt.Println(out)
    }
poWar
  • 745
  • 6
  • 15