Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When calling Render with a layout and a nil binding: panic: assignment to entry in nil map #134

Open
flowonyx opened this issue Aug 22, 2021 · 0 comments

Comments

@flowonyx
Copy link

The issue is that in jetVarMap it assumes that a nil binding should result in a nil jet.VarMap.

template/jet/jet.go

Lines 233 to 237 in c4b2c7e

func jetVarMap(binding interface{}) jet.VarMap {
var bind jet.VarMap
if binding == nil {
return bind
}

But if a layout is specified, it is then used to set a function and panics:

template/jet/jet.go

Lines 219 to 229 in c4b2c7e

bind := jetVarMap(binding)
if len(layout) > 0 {
lay, err := e.Templates.GetTemplate(layout[0])
if err != nil {
return err
}
bind.Set(e.layout, func() {
_ = tmpl.Execute(out, bind, nil)
})
return lay.Execute(out, bind, nil)
}

This could be solved either by replacing var bind jet.VarMap with bind := make(jet.VarMap) or by checking if bind is nil (and makeing it if not) in Render right after if len(layout) > 0 {.

// var bind jet.VarMap
bind := make(jet.VarMap)

Or in Render

bind := jetVarMap(binding) 
 if len(layout) > 0 { 
        if bind == nil {
                bind = make(jet.VarMap)
        }
 	lay, err := e.Templates.GetTemplate(layout[0]) 
 	if err != nil { 
 		return err 
 	} 
 	bind.Set(e.layout, func() { 
 		_ = tmpl.Execute(out, bind, nil) 
 	}) 
 	return lay.Execute(out, bind, nil) 
 } 

I would send a pull request, but I'm not sure if it matters to you which way it is fixed. I would think it would be much clearer to put it in jetVarMap and get rid of the other calls to make there, but I guess there was a reason to not do that in the first place.


The work around is for the caller of Render to just specify an empty jet.VarMap or fiber.Map but that is not obvious and panicing from forgetting is not great.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant