-
Notifications
You must be signed in to change notification settings - Fork 52
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to change layout Title #31
Comments
I first drafted this question here, but asked in fiber eventually. However, having closed it on fiber, and thinking back, I think this is still an valid question/issue -- people need their layout to be a template for all their pages, and they need to change the HTML Title for every page. So, would be OK that I change the default html template to satisfy this, so that the next people won't need to wonder again? If I did so, would you accept the PR? |
I have same problem. How to change layout in several places |
Hi @Fenny, Please advice -- now it is more than me facing the same problem. (I kind of solve it a hacky way, but don't want to miss-lead people). |
May I ask what template engine you are using? From my previous experience, I can tell that |
django |
Hi @suntong and @BellZaph, rendering templates using the main.go package main
import (
"github.com/gofiber/fiber"
"github.com/gofiber/template/django"
)
func main() {
engine := django.New("./views", ".django")
// Pass the engine to the Views
app := fiber.New(&fiber.Settings{
Views: engine,
})
app.Get("/", func(c *fiber.Ctx) {
// Render index
_ = c.Render("index", fiber.Map{
"Title": "Index!",
})
})
_ = app.Listen(8080)
} views/index.django
Thanks for bringing up the issue! 🚀 |
Thanks for the working demo for @BellZaph, @thomasvvugt. My question was for html template, to be specifically, about these lines template/html/views/layouts/main.html Lines 4 to 6 in a06b81a
I.e., how to change the |
for global, use function engine engine.AddFunc("title", func() string {
return "this is title"
}) <head>
<title>{{ title() }}</title>
</head> |
Thanks! If that's the only solution, then I think my hacky one is better, :) |
Hi @suntong, the The example @fuadarradhi showed can be simplified by using the main.go package main
import (
"github.com/gofiber/fiber"
"github.com/gofiber/template/html"
)
func main() {
engine := html.New("./views", ".html")
app := fiber.New(&fiber.Settings{
Views: engine,
})
app.Get("/", func(c *fiber.Ctx) {
// Render index
c.Render("index", fiber.Map{
"Title": "Index!",
})
})
app.Listen(8080)
} views/index.html <!DOCTYPE html>
<html>
<head>
<title>{{ .Title }}</title>
</head>
<body>
<h1>{{ .Title }}</h1>
</body>
</html> Keep in mind: HTML templates treat data values as plain text which should be encoded so they can be safely embedded in an HTML document. The escaping is contextual, so actions can appear within JavaScript, CSS, and URI contexts. You should always escape your data if not already sanitized, the HTML templates provides several functions for this; https://golang.org/pkg/html/template/#hdr-Contexts Once again, thanks for bringing up the issue. Let me know if my example helps you out or leaves you with additional questions! 🔥 |
Hey guys |
Hmm... let me stress again that my question is how to change the yet, @thomasvvugt, both of your demo code are not using the layout file. So let me be specific ./views/index.html {{template "partials/header" .}}
<h1>{{.Title}}</h1>
{{template "partials/footer" .}} ./views/partials/header.html <h2>Header</h2> ./views/partials/footer.html <h2>Footer</h2> ./views/layouts/main.html <!DOCTYPE html>
<html>
<head>
<title>Main</title>
</head>
<body>
{{embed}}
</body>
</html> package main
import (
"github.com/gofiber/fiber"
"github.com/gofiber/template/html"
)
func main() {
// Create a new engine
engine := html.New("./views", ".html")
// Pass the engine to the Views
app := fiber.New(&fiber.Settings{
Views: engine,
})
app.Get("/", func(c *fiber.Ctx) {
// Render index
c.Render("index", fiber.Map{
"Title": "Hello, World!",
})
})
app.Get("/layout", func(c *fiber.Ctx) {
// Render index within layouts/main
c.Render("index", fiber.Map{
"Title": "Hello, World!",
}, "layouts/main")
})
app.Listen(3000)
} The two app.Get("/layout", func(c *fiber.Ctx) {
// Render index within layouts/main
c.Render("index", fiber.Map{
"Title": "Hello, World!",
}, "layouts/main") thx |
@suntong <!DOCTYPE html>
<html>
<head>
<title>{{.Title}}</title>
</head>
<body>
{{embed}}
</body>
</html> |
wow, I should have tried -- I thought it is not possible, and now I'm embarrassed, :). |
Is this above still a valid solution? I am passing PageTitle but my render fails with this error
or
Relevant hbs code
and the corresponding route
|
What's your views file of line 7? |
@suntong This is my view
On line 7 I have
|
I want my
layout
to be a template for all my pages. However theCan only change the content within
body
.I need to change the HTML Title in the header as well.
Any way to do that? thx.
The text was updated successfully, but these errors were encountered: