DEV Community

Eray Ates
Eray Ates

Posted on

3 2 1 1 1

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

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay