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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] #2214

Closed
billizzard opened this issue Sep 23, 2023 · 3 comments
Closed

[BUG] #2214

billizzard opened this issue Sep 23, 2023 · 3 comments
Assignees

Comments

@billizzard
Copy link

billizzard commented Sep 23, 2023

I have main layout


func initApp() *iris.Application {
	app := iris.New()
	tmpl := iris.HTML("./src/public/html", ".html")
	tmpl.Layout("layout/main.html")
	app.RegisterView(tmpl)

	return app
}

stucture public/html folder


-layout
--main.html
-files
--list.html
-menu
--menu.html

layout/main.html has this code (this is main layout)


<html>
<head>
    {{ block "title" .}}<title>Default</title>{{end}}
</head>
<body>
</body>
</html>

There are also pages that are rendered like this (routes different)
ctx.View("files/list.html");
ctx.View("menu/menu.html");

files/list.html content


{{ define "title"}}
    <title>222</title>
{{ end }}

menu/menu.html content


{{ define "title"}}
    <title>111</title>
{{ end }}

no matter which page of these two you go to, it’s always 111 in title, but expect 222 in list.html page

@kataras
Copy link
Owner

kataras commented Sep 23, 2023

Hi @billizzard, actually this is not a bug, this is how Go HTML templates work. If you parse more than one template with the same {{ define }}, the one overrides the other, you can read it at: https://stackoverflow.com/questions/66613213/rendering-golang-template-defining-the-same-block-multiple-times. For that reason we have two features: the custom {{ yield . }}, however in your case that's not what you need, the 2nd one is the Blocks template engine which uses similar solution (map/set of templates) and supports multiple {{define}} tags.

@billizzard
Copy link
Author

Thank you, sorry for bothering you

@kataras
Copy link
Owner

kataras commented Sep 24, 2023

Hello @billizzard, it's ok, indeed you "forced" me to make blocks even more modular. I uploaded an example for you at: https://github.com/kataras/iris/tree/main/_examples/view/template_blocks_2. Hope that you enjoy the framework!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants