tyr84
October 11, 2024, 7:42pm
1
I got the following structure:
layouts
-> _default
baseof.html
-> pages
contact.html
-> partials
contactform.html
index.html
baseof.html:
...
{{ block "header_css" . }}{{ end }}
...
index.html:
...
{{ define "header_css" }}{{ end }}
...
contact.html:
...
{{ define "header_css" }}{{ end }}
...
contactform.html:
...
{{ define "header_css" }}
{{ $tomSelectCSS := resources.Get "css/tom-select.bootstrap5.css" }}
{{ if .Site.IsServer }}
<link rel="stylesheet" href="{{ $tomSelectCSS.RelPermalink }}">
{{ else }}
<link rel="stylesheet" href="{{ ($tomSelectCSS | minify | fingerprint).RelPermalink }}">
{{ end }}
{{ end }}
...
This works for the contact page, but the problem is that the stylesheet shows up on the index page when viewed in the browser. I can’t figure out if this is by design or error
irkode
October 11, 2024, 8:29pm
2
I am sure I did not get all of that. Too much missing around the snippets. No way to replicate that.
But you cannot define a block in a partial. That may explain taht the output is weird.
To get in detail I would recommend to share a replayablw example.
tyr84
October 11, 2024, 10:11pm
3
tried moving the css out of the partial, that fixed things.
Is this limitation by design?
irkode
October 11, 2024, 11:06pm
4
tyr84:
limitation by design
Yes.
quote=“tyr84, post:3, topic:51910”]
moving the css out of the partial
[/quote]
What ever that means…
You can define multiple blocks in baseof.
Then define these blocks in a layout like index.html. and call partials in blocks.
baseof.html
<html>
<head>
{{ block "head"....
</head>
<body>
{{ block "main" ... }}
</body>
index.html
{{ define "head" }}
{{ partial "contactform" . }}
{{ end }}
{{ define "main" }}
{{ .Content }}
...
There’s more, like defaults for blocks, omit a definition to skip a block…
Check the docs