gofiber/template

Template Rendering Incorrectly

keiranrowan opened this issue · 4 comments

Using the "template/html" package I am having issues rendering templates that derive a base layout all located in the same directory. I have a base layout template like so:

base.html

{{define "base"}}
<!DOCTYPE html>
<html lang="en">
<body>

header...

{{block "content" .}}{{ end }}

footer...

</body>
</html>
{{end}}

a first page like so which renders fine:

first.html

{{ template "base" .}}

{{define "content"}}
Some content
{{end}}

and a second page like so:

second.html

{{ template "base" .}}

{{define "content"}}
Some more content
{{end}}

My template configuration looks as follows:

    engine := html.New("./views", ".html")
    engine.Reload(true)

    app := fiber.New(fiber.Config{
        Views: engine,
    })

    app.Get("/first", func(c *fiber.Ctx) error {
        return c.Render("first", fiber.Map{})
    })

    app.Get("/second", func(c *fiber.Ctx) error {
        return c.Render("second", fiber.Map{})
    })

The issue I am encountering is that the second page renders the content of the first. Is this expected behavior in this situation? Normally with "template/html" I would call template.ParseFiles("first.html", "base.html") for the first template and template.ParseFiles("second.html", "base.html") for the second template. How exactly does Fiber determine which templates to parse based on the single template name? Thanks!

Hi,

I have the same issue. Has this been resolved?

buroz commented

I got the same issue now. Anyone solved?

This is was explained by @ReneWerner87 in this reply: #149 (comment)

problem with your variant with the define blocks is that the identifiers of these are not unique

because before using the template engine everything is parsed and added to the go template engine, later when calling the render method only the template is picked out which should be rendered, but here exist several blocks with the same name and the base file doesn't know which one it should take

sp213 commented

Hi, I got the same issue, any solutions for that?