Skip to content

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

Open
@flowonyx

Description

@flowonyx

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions