yama
September 7, 2022, 3:08am
1
Hello, in my current code I have
{{ $currentCategory := .Params.categories }}
and this worked because I only had one taxonomy that I used, called category.
However I now have more taxonomies, and I want $currentCategory
to be whatever taxonomy the current page belongs to.
idarek
September 7, 2022, 5:59am
2
Example for categories:
{{ if .Params.categories }}
<div>
{{ range .Params.categories }}
<a href="category/{{ . | urlize }}/">{{ . }}</a>
{{ end }}
</div>
{{ end }}
for tags:
{{ if .Params.tags }}
<footer>
<ul>
{{ range .Params.tags }}
<li>#<a href="{{ "tag/" | absURL }}{{ . | urlize }}/">{{ . | lower }}</a>
</li>
{{ end }}
</ul>
</footer>
{{ end }}
1 Like
yama
September 7, 2022, 1:54pm
4
Okay this makes sense, however what’s actually happening when I try to implement it doesn’t make sense to me:
This works:
{{ $currentCategory := .Params.categories }}
{{ print $currentCategory }}
However, this results in an undefined variable:
{{ if .Params.categories }}
{{ $currentCategory := .Params.categories }}
{{ else if .Params.illustrations }}
{{ $currentCategory := .Params.illustrations }}
{{ end }}
{{ print $currentCategory }}
if I print the $currentCategory within the if block, then it prints just fine, but it seems as if the variable gets erased after the if block closes? What’s going on?
tut
September 7, 2022, 1:59pm
5
How are you defining these taxonomies? Does it mean some posts have more than one taxonomy? Please share your code.
yama
September 7, 2022, 2:05pm
6
Posts have one taxonomy, either categories: category-name
or illustrations: illustration-name
or rule34: r34-name
This is what I’m trying to modify: layouts/partials/prev_next_post.html · master · indeedwatson / cheeky-icarus · GitLab
idarek
September 7, 2022, 2:12pm
7
Have you noticed range, which will bring what’s needed?
yama
September 7, 2022, 3:14pm
8
I did but I’m not sure how to adapt that logic to my current code, where all I need to do is define a variable.
In any regular programming language, I feel this would be a very simple thing to do, such as in the if block that I defined.
Additionally, I copy pasted your code and it results in an error: can’t iterate over category-name
idarek
September 7, 2022, 3:19pm
9
Could you paste example of your frontmatter to see how you specify your categories etc.
yama
September 7, 2022, 3:22pm
10
title: page 12
categories: bath-house
date: 2022-03-01
page-number: 012
thumb: "/img/Bath House/thumbs/012.png"
---
{{< figure src="/img/Bath House/012.png" alt="page 12"
link="/img/Bath House/012.png" target="_blank">}}
idarek
September 7, 2022, 3:28pm
11
yama:
categories: bath-house
Could you try to change this bit to
categories:
- bath-house
Every category in separate line with - on front
And use my code for categories. Paste output/error in full here to look at as it shall work.
idarek
September 7, 2022, 3:35pm
12
Ps. Avoid white spaces in your file names for images as this can cause issues on various hosting environments (for example can work locally but will not work online). Use underscore instead.
yama
September 7, 2022, 3:59pm
13
categories:
I implemented this and now your code returns no errors. However, I still don’t understand how your code addresses my specific use case, or how to adapt it.
I’d really appreciate an explanation for why the if block that I posted, to define $currentCategory is not working. What am I doing wrong? Why does the variable get defined while inside the if block, but then doesn’t exist outside of it? This is very confusing and counter intuitive.
yama
September 7, 2022, 4:00pm
14
thanks, appreciate the tip
yama:
However, this results in an undefined variable:
{{ if .Params.categories }}
{{ $currentCategory := .Params.categories }}
{{ else if .Params.illustrations }}
{{ $currentCategory := .Params.illustrations }}
{{ end }}
{{ print $currentCategory }}
Variables initialized ( :=
) within a block are not available outside of the block.
Initialize ( :=
) before the block, then assign ( =
) within the block.
{{ $foo := "" }}
{{ if true }}
{{ $foo = "wibble" }}
{{ end }}
{{ $foo }} --> wibble
See https://pkg.go.dev/text/template#hdr-Variables
yama
September 7, 2022, 4:13pm
16
@jmooring You have saved me once again, thank you!
My code looks a bit messy now but it works.
@idarek There’s a simpler way to render terms:
{{ with .GetTerms "tags" }}
<p>Tags:
{{ range . }}
<a href="{{ .Permalink }}">{{ .LinkTitle }}</a>
{{ end }}
</p>
{{ end }}
See https://gohugo.io/templates/taxonomy-templates/#example-list-tags-in-a-single-page-template
3 Likes
tut
September 7, 2022, 7:56pm
18
Hi Joe. Is there a difference between $foo := ""
and $foo := slice
?
{{ $foo := "" }}
{{ printf "%T" $foo }} --> string
{{ $foo := slice }}
{{ printf "%T" $foo }} --> []interface {}
{{ $foo := "" }}
{{ if true }}
{{ $foo = slice "a" 1 }}
{{ end }}
{{ printf "%T" $foo }} --> []interface {}
{{ $foo := slice }}
{{ if true }}
{{ $foo = "a" }}
{{ end }}
{{ printf "%T" $foo }} --> string
6 Likes
system
Closed
September 9, 2022, 10:06pm
20
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.