gofiber/template

๐Ÿค— Html layouts and views, having difficulties and I need help

gregg-cbs opened this issue ยท 7 comments

Question description
Can I please have an example of how views and layouts are supposed to interact with each other in Fiber?

At the moment i have set up layouts and views but if I use the layout in more than one view it seems to bug out.

I have initialised views like so
In my html folder is both my views and layouts

fApp := fiber.New(fiber.Config{
  Views: html.New("./app/html", ".html"),
})

I made a base layout and in it i define base (as per some example from the internet):

{{define "base"}}
   <..some html>
  {{template "main" .}}
{{end}}

I use the base template in a view by defining it

{{template "base" .}}
  {{define "main"}}
     <..some html>
  {{end}}

^ this works, but if I used the template again in another view then it shows this view as if something is cross pollinating.

The fiber docs on this matter were no help.

Thanks for opening your first issue here! ๐ŸŽ‰ Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

Hi, please move this report in our template repository

@gregg-cbs

please use the embed tag in every layout and do not define anything yourself

our logic will then make sure that your template is rendered into the layout,

template/html/html.go

Lines 206 to 211 in 4b29ea1

lay.Funcs(map[string]interface{}{
e.layout: func() error {
return tmpl.Execute(out, binding)
},
})
return lay.Execute(out, binding)

like in the example in the readme
https://github.com/gofiber/template/tree/master/html#html

@ReneWerner87
Thanks for moving this issue.

I have found those docs helpful but with {{embed}} approach how do i pass variables to my template?

For example how do I pass the title of the page, previously in the layout it was:

<title>{{template "title" .}} | Stockr</title>

and in a view that uses the template:

{{define "title"}}All Companies{{end}}

Template system works independently from this embed tag

The render function must be passed variables which can then be used in layout or in the template which is rendered into the layout.

Thanks, i looked through the html.go and noticed that.

Code for anyone should they come across this:

return c.Render("companies", fiber.Map{
	"MetaTitle": "All Companies",
	"MetaDescription": "Some description",
}, "layouts/base")
<title>{{.MetaTitle}} | Stockr</title

Much appreciated for the quick help! I was lost without you.