diff --git a/framework/h/render.go b/framework/h/render.go index bad3b35..e19558b 100644 --- a/framework/h/render.go +++ b/framework/h/render.go @@ -20,6 +20,12 @@ func WithDocType() RenderOpt { } } +func WithNonce(nonce string) RenderOpt { + return func(context *RenderContext, opt *RenderOptions) { + context.nonce = nonce + } +} + // Render renders the given node recursively, and returns the resulting string. func Render(node Ren, opts ...RenderOpt) string { builder := &strings.Builder{} diff --git a/framework/h/renderer.go b/framework/h/renderer.go index b0969a9..d0b2b1c 100644 --- a/framework/h/renderer.go +++ b/framework/h/renderer.go @@ -2,10 +2,11 @@ package h import ( "fmt" - "github.com/maddalax/htmgo/framework/hx" "html" "html/template" "strings" + + "github.com/maddalax/htmgo/framework/hx" ) type CustomElement = string @@ -44,16 +45,17 @@ type RenderContext struct { builder *strings.Builder scripts []ScriptEntry currentElement *Element + nonce string } func (ctx *RenderContext) AddScript(funcName string, body string) { script := fmt.Sprintf(` - `, funcName, funcName, body) + `, funcName, ctx.nonce, funcName, body) ctx.scripts = append(ctx.scripts, ScriptEntry{ Body: script,