Can anyone explain why the output of this:
{{ $related := false }}
{{ if .Page.Params.lang }}
Lang set
{{ $related := first 3 (where .Site.Pages “Params.lang” .Page.Params.lang | shuffle ) }}
{{ else if .Page.Params.tags }}
{{ $related := first 3 (.Site.RegularPages.Related .) }}
{{ end }}
{{$related}}
IS
Lang set
false
1 Like
As far as I can tell, it’s because $related inside the if is a different variable than the $related outside the if. So you need this inside the if:
{{ $.related = whatever }}
The ‘.’ tells Hugo to look just outside the current scope.
{{ $foo := 1}}
{{ if true }}
{{ $foo := 2 }}
{{ end }}
{{ $foo }}
1
{{ $foo := 1}}
{{ if true }}
{{ $foo = 2 }}
{{ end }}
{{ $foo }}
2
See https://golang.org/pkg/text/template/#hdr-Variables
2 Likes
Thanks jmorring. That worked for me.
I have one more question, how can I get this double condition to work?
{{ $langs := "de fr"}}
{{ $related = first 3 (where not in .Params.lang $langs ) (.Site.RegularPages.Related .) }}
Assuming your frontmatter contains something like this…
lang = "en"
Then…
{{- $langs := slice "de" "fr" -}}
{{- $related := where (.Site.RegularPages.Related .) "Params.lang" "not in" $langs | first 3 -}}
Unfortunately only non english pages have that lang parameter set, so the english pages have no front matter variable for lang. So I can guess instead of checking for the value i should be checking whether it’s set.
{{- $related := where (.Site.RegularPages.Related .) "Params.lang" nil | first 3 -}}
Hmm, that looks like exactly what I need but it’s returning any results. Is nil
check for no value or not set or both?
This will return pages where lang
is not set in frontmatter. It will not return pages where lang = ""
in frontmatter.
OK, not sure why but it’s not working. I don’t have that lang
parameter set at all for most of the pages so some should be showing up.
I’d be happy to troubleshoot if you provide a link to the public repository for your project. Otherwise, I’d just be guessing…
I can’t unfortunately.
Here’s some example front matter for a page that should show:
---
title: "Page title"
description:
date: 2020-10-22
draft: false
authors:
- Author Name
resources:
- name: cover
src: images/image.jpg
title: "Cover image"
- name: twitterimg
src: images/image-twitter.jpg
- name: ogimage
src: images/image-facebook.jpg
tags:
- php
---
Please share your site configuration file.
baseurl: "/"
languageCode: "en-us"
languageLang: "en"
title: ""
buildDrafts: true
buildFuture: true
enableRobotsTXT: false
removePathAccents: true
theme: "hugo-demos"
taxonomies:
# author: "authors"
tag: "tags"
imaging:
quality: 80
resampleFilter: "gaussian"
DefaultContentLanguage: en
pygmentsCodeFences: true
pygmentsUseClasses: true
pygmentsCodefencesGuessSyntax: true
# Create 301 redirects rather than html meta taxonomies[outputs]
disableAliases: true
outputs:
home:
- HTML
- RSS
- REDIR
section:
- HTML
- JSON
- RSS
mediaTypes:
"text/platform":
delimiter: ''
outputFormats:
REDIR:
mediatype: "text/platform"
baseName: "redirects.php"
isPlainText: true
notAlternative: true
paginate: 30
paginatePath: "page"
markup:
goldmark:
renderer:
unsafe: true
highlight:
lineNos: false
lineNumbersInTable: false
minify:
disableCSS: true
disableHTML: true
disableJS: true
minifyOutput: true
build:
writeStats: true
related:
threshold: 90
includeNewer: true
toLower: true
indices:
- name: tags
weight: 100
- name: date
weight: 40
In the site configuration file that you posted above, the first line is indented. If that’s what your site configuration actually looks like, it’s broken.
jmooring, worked it out, needed to remove the colon as it’s nested inside the if statement
{{- $related := where (.Site.RegularPages.Related .) "Params.lang" nil | first 3 -}}
Should be:
{{- $related = where (.Site.RegularPages.Related .) "Params.lang" nil | first 3 -}}
Thanks for your help
system
Closed
December 5, 2020, 8:17pm
18
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.