# Scope issue inside conditional block (I think)

**URL:** <https://discourse.gohugo.io/t/scope-issue-inside-conditional-block-i-think/29273>\
**Category:** support\
**Created:** [November 6, 2020, 9:39pm UTC](https://discourse.gohugo.io/t/scope-issue-inside-conditional-block-i-think/29273 "2020-11-06T21:39:50Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexander\_Hobday](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/alexander_hobday/32/8422_2.png) [@Alexander\_Hobday](https://discourse.gohugo.io/u/Alexander_Hobday)\
**Post date:** [November 6, 2020, 9:39pm UTC](https://discourse.gohugo.io/t/scope-issue-inside-conditional-block-i-think/29273/1 "2020-11-06T21:39:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![orange\_tang](https://avatars.discourse-cdn.com/v4/letter/o/ed8c4c/32.png) [@orange\_tang](https://discourse.gohugo.io/u/orange_tang)\
**Post date:** [November 6, 2020, 10:48pm UTC](https://discourse.gohugo.io/t/scope-issue-inside-conditional-block-i-think/29273/2 "2020-11-06T22:48:12Z")

</div>

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:

```auto
{{ $.related = whatever }}

```

The ‘.’ tells Hugo to look just outside the current scope.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [November 7, 2020, 2:09am UTC](https://discourse.gohugo.io/t/scope-issue-inside-conditional-block-i-think/29273/3 "2020-11-07T02:09:27Z")

</div>

> [@orange\_tang](#):
>
> The ‘.’ tells Hugo to look just outside the current scope.

No, it does not.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [November 7, 2020, 2:11am UTC](https://discourse.gohugo.io/t/scope-issue-inside-conditional-block-i-think/29273/4 "2020-11-07T02:11:56Z")

</div>

```auto
{{ $foo := 1}}
{{ if true }}
  {{ $foo := 2 }}
{{ end }}
{{ $foo }}

```

> 1

```auto
{{ $foo := 1}}
{{ if true }}
  {{ $foo = 2 }}
{{ end }}
{{ $foo }}

```

> 2

See [https://golang.org/pkg/text/template/#hdr-Variables](https://golang.org/pkg/text/template/#hdr-Variables)

---

<div class="post-metadata">

**Author:** ![Alexander\_Hobday](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/alexander_hobday/32/8422_2.png) [@Alexander\_Hobday](https://discourse.gohugo.io/u/Alexander_Hobday)\
**Post date:** [November 12, 2020, 5:14pm UTC](https://discourse.gohugo.io/t/scope-issue-inside-conditional-block-i-think/29273/5 "2020-11-12T17:14:25Z")

</div>

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 .) }}
```

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [November 14, 2020, 10:53am UTC](https://discourse.gohugo.io/t/scope-issue-inside-conditional-block-i-think/29273/6 "2020-11-14T10:53:47Z")

</div>

Assuming your frontmatter contains something like this…

```auto
lang = "en"

```

Then…

```auto
{{- $langs := slice "de" "fr" -}}
{{- $related := where (.Site.RegularPages.Related .) "Params.lang" "not in" $langs | first 3 -}}

```

---

<div class="post-metadata">

**Author:** ![Alexander\_Hobday](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/alexander_hobday/32/8422_2.png) [@Alexander\_Hobday](https://discourse.gohugo.io/u/Alexander_Hobday)\
**Post date:** [November 16, 2020, 4:56pm UTC](https://discourse.gohugo.io/t/scope-issue-inside-conditional-block-i-think/29273/7 "2020-11-16T16:56:29Z")

</div>

> [@jmooring](#):
>
> ```auto
> {{- $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.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [November 16, 2020, 8:30pm UTC](https://discourse.gohugo.io/t/scope-issue-inside-conditional-block-i-think/29273/8 "2020-11-16T20:30:14Z")

</div>

```auto
{{- $related := where (.Site.RegularPages.Related .) "Params.lang" nil | first 3 -}}

```

---

<div class="post-metadata">

**Author:** ![Alexander\_Hobday](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/alexander_hobday/32/8422_2.png) [@Alexander\_Hobday](https://discourse.gohugo.io/u/Alexander_Hobday)\
**Post date:** [November 16, 2020, 9:34pm UTC](https://discourse.gohugo.io/t/scope-issue-inside-conditional-block-i-think/29273/9 "2020-11-16T21:34:10Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [November 16, 2020, 9:38pm UTC](https://discourse.gohugo.io/t/scope-issue-inside-conditional-block-i-think/29273/10 "2020-11-16T21:38:34Z")

</div>

This will return pages where `lang` is not set in frontmatter. It will not return pages where `lang = ""` in frontmatter.

---

<div class="post-metadata">

**Author:** ![Alexander\_Hobday](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/alexander_hobday/32/8422_2.png) [@Alexander\_Hobday](https://discourse.gohugo.io/u/Alexander_Hobday)\
**Post date:** [November 17, 2020, 4:43pm UTC](https://discourse.gohugo.io/t/scope-issue-inside-conditional-block-i-think/29273/11 "2020-11-17T16:43:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [November 17, 2020, 4:46pm UTC](https://discourse.gohugo.io/t/scope-issue-inside-conditional-block-i-think/29273/12 "2020-11-17T16:46:11Z")

</div>

I’d be happy to troubleshoot if you provide a link to the public repository for your project. Otherwise, I’d just be guessing…

---

<div class="post-metadata">

**Author:** ![Alexander\_Hobday](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/alexander_hobday/32/8422_2.png) [@Alexander\_Hobday](https://discourse.gohugo.io/u/Alexander_Hobday)\
**Post date:** [November 17, 2020, 4:48pm UTC](https://discourse.gohugo.io/t/scope-issue-inside-conditional-block-i-think/29273/13 "2020-11-17T16:48:12Z")

</div>

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
---
```

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [November 17, 2020, 5:06pm UTC](https://discourse.gohugo.io/t/scope-issue-inside-conditional-block-i-think/29273/14 "2020-11-17T17:06:46Z")

</div>

Please share your site configuration file.

---

<div class="post-metadata">

**Author:** ![Alexander\_Hobday](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/alexander_hobday/32/8422_2.png) [@Alexander\_Hobday](https://discourse.gohugo.io/u/Alexander_Hobday)\
**Post date:** [November 17, 2020, 6:38pm UTC](https://discourse.gohugo.io/t/scope-issue-inside-conditional-block-i-think/29273/15 "2020-11-17T18:38:06Z")

</div>

```
    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
```

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [November 18, 2020, 11:58am UTC](https://discourse.gohugo.io/t/scope-issue-inside-conditional-block-i-think/29273/16 "2020-11-18T11:58:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Alexander\_Hobday](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/alexander_hobday/32/8422_2.png) [@Alexander\_Hobday](https://discourse.gohugo.io/u/Alexander_Hobday)\
**Post date:** [December 3, 2020, 8:17pm UTC](https://discourse.gohugo.io/t/scope-issue-inside-conditional-block-i-think/29273/17 "2020-12-03T20:17:35Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [December 5, 2020, 8:17pm UTC](https://discourse.gohugo.io/t/scope-issue-inside-conditional-block-i-think/29273/18 "2020-12-05T20:17:37Z")

</div>

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.
