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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃 Html layouts and views, having difficulties and I need help #168

Closed
gregg-cbs opened this issue Mar 6, 2022 · 7 comments
Closed
Labels
馃 Question Further information is requested

Comments

@gregg-cbs
Copy link

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.

@welcome
Copy link

welcome bot commented Mar 6, 2022

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

@ReneWerner87
Copy link
Member

Hi, please move this report in our template repository

@ReneWerner87 ReneWerner87 reopened this Mar 7, 2022
@ReneWerner87 ReneWerner87 transferred this issue from gofiber/fiber Mar 7, 2022
@ReneWerner87 ReneWerner87 added the 馃 Question Further information is requested label Mar 7, 2022
@ReneWerner87
Copy link
Member

@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

@gregg-cbs
Copy link
Author

gregg-cbs commented Mar 7, 2022

@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}}

@ReneWerner87
Copy link
Member

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.

@gregg-cbs
Copy link
Author

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

@gregg-cbs
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
馃 Question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants