Arif
February 13, 2024, 5:11pm
1
I have a use case for a section title in a multilingual site that I need help. Let’s assume the section is called news
with an seoTitle
of News
. I want to assign the <title></title>
value in head.html
as follows.
The section itself gets the value of seoTitle
in _index.md
.
The nested sections and pages get the value of their title and also append the seoTitle
from news _index.md
at the end.
How to achieve this?
You’ll need to access the parent page, something like .Page.Parent.Params.seoTitle
.
Arif
February 13, 2024, 6:16pm
3
Which is the correct way to target the section (section’s _index.md
) only? Coz in my code I have to assign a weight to it as below.
{{- $title := .Title }}
{{- if and (.IsSection) (ne .Layout "contact") }}
{{- if (eq .Section "news") }}
{{- if .Page.CurrentSection.Params.seoTitle }}
{{- $title = .Params.seoTitle }}
{{- else }}
{{- $title = (printf "%s | %s" $title .Page.Parent.Params.seoTitle ) }}
{{- end }}
{{- else }}
{{- $title = (printf "%s | %s" $title site.Title ) }}
{{- end }}
{{- end }}
Compare .Page.Kind
to “section” or test for Page.IsSection
. Using weight
is fragile.
You can also use .Page.CurrentSection.Params.foo
. This is slightly different than .Parent
because you can use it with the section page too.
Arif
February 13, 2024, 6:30pm
6
I made an edit to the code to show the full code because the <title>
is empty for nested sections
If you need to get to the top use .FirstSection.
Arif
February 13, 2024, 9:05pm
8
Still empty. Edited the code.
I should have been clearer. You can use .FirstSection
instead of .Parent
if you need to get the top level section page, skipping the nested sections in between.
Arif
February 13, 2024, 9:14pm
10
What I mean is that, any usage of FirstSection
makes the <title>
attribute for nested sections to disappear in <head>
.
This doesn’t make any sense.
No, the .IsSection method will always return true or false.
Arif
February 13, 2024, 9:25pm
14
I forgot this also works. But I wish there was a straightforward way to only target the section only.
{{- if .Page.FirstSection.Params.seoTitle }}
I think you take a break and revisit this later.
Arif
February 13, 2024, 9:28pm
16
Returns some broken code %!s(<nil>)
. I will stick with FirstSection
,
Arif
February 13, 2024, 9:29pm
17
Actually I always use workarounds for these issues. Working with nested sections is a headache.
system
Closed
February 15, 2024, 9:30pm
18
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.