diff --git a/ctx.go b/ctx.go index e07ce30da6..92d7fad985 100644 --- a/ctx.go +++ b/ctx.go @@ -1437,7 +1437,10 @@ func (c *Ctx) renderExtensions(bind interface{}) { // Bind view map if c.viewBindMap != nil { for _, v := range c.viewBindMap.D { - bindMap[v.Key] = v.Value + // make sure key does not exist already + if _, ok := bindMap[v.Key]; !ok { + bindMap[v.Key] = v.Value + } } } diff --git a/ctx_test.go b/ctx_test.go index 0d20898065..c2b2c2d9e6 100644 --- a/ctx_test.go +++ b/ctx_test.go @@ -2851,6 +2851,29 @@ func Test_Ctx_RenderWithBind(t *testing.T) { } +func Test_Ctx_RenderWithOverwrittenBind(t *testing.T) { + t.Parallel() + + app := New() + c := app.AcquireCtx(&fasthttp.RequestCtx{}) + + err := c.Bind(Map{ + "Title": "Hello, World!", + }) + utils.AssertEqual(t, nil, err) + defer app.ReleaseCtx(c) + err = c.Render("./.github/testdata/index.tmpl", Map{ + "Title": "Hello from Fiber!", + }) + + buf := bytebufferpool.Get() + _, _ = buf.WriteString("overwrite") + defer bytebufferpool.Put(buf) + + utils.AssertEqual(t, nil, err) + utils.AssertEqual(t, "

Hello from Fiber!

", string(c.Response().Body())) +} + func Test_Ctx_RenderWithBindLocals(t *testing.T) { t.Parallel()