DEV Community

Eray Ates
Eray Ates

Posted on

Go Template Playground

If you need to test go template with sprig functionality and HTML, Text template switch settings. https://repeatit.io meet these needs.
I made it with very little code and it is working on browser with go's webassembly output.

First start, use promise in go code. So we can return error and see it in browser. When creating promise in javascript, you are adding a function which is take resolve and reject parameters.
In golang is same so our promise implementation like this.
Directly use main function arguments in promise function!

func Render(this js.Value, args []js.Value) interface{} {
    mainArgs := args

    handler := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
        resolve := args[0]
        reject := args[1]

        go func() {
            data, err := renderWithError(mainArgs)
            if err != nil {
                errorConstructor := js.Global().Get("Error")
                errorObject := errorConstructor.New(err.Error())
                reject.Invoke(errorObject)
            } else {
                resolve.Invoke(js.ValueOf(data))
            }
        }()

        return nil
    })

    promiseConstructor := js.Global().Get("Promise")

    return promiseConstructor.New(handler)
}
Enter fullscreen mode Exit fullscreen mode

After that set this function and use it with js.

renderjs := js.FuncOf(Render)
js.Global().Set("render", renderjs)
Enter fullscreen mode Exit fullscreen mode
try {
  output = await render(values.input, values.template, config.template);
} catch (error) {
  // inform to somewhere
}
Enter fullscreen mode Exit fullscreen mode

Whole website is opensource on https://github.com/rytsh/repeatit

If you want to see a feature about that write me I will handle it.

          _---~~\\~~----_
  _ / _ *^____      _____``*h*\"*/
 / __/ /'     ^   /      \ ^@q   F
[  @L | @))    |  | @))   l  0 _/
 \ /   \~____ / __ \_____/    \
  |           _l__l_           I
  }          [______]           I
  ]            | | |            |
  ]             ~ ~             |
Enter fullscreen mode Exit fullscreen mode

https://javascript.info/promise-basics
https://gist.github.com/belbomemo/b5e7dad10fa567a5fe8a

Discussion (0)