# Join two variables together

**URL:** <https://discourse.gohugo.io/t/join-two-variables-together/39402>\
**Category:** support\
**Created:** [July 7, 2022, 10:23pm UTC](https://discourse.gohugo.io/t/join-two-variables-together/39402 "2022-07-07T22:23:37Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![tut](https://avatars.discourse-cdn.com/v4/letter/t/e95f7d/32.png) [@tut](https://discourse.gohugo.io/u/tut)\
**Post date:** [July 7, 2022, 10:23pm UTC](https://discourse.gohugo.io/t/join-two-variables-together/39402/1 "2022-07-07T22:23:37Z")

</div>

How do I join multiple custom variables into one? I want both to generate a `$pages` variable to use in RSS feed template. (Where "load regular pages in section blog, and include [pages](https://discourse.gohugo.io/t/splitting-an-article-into-multiple-pages/29161/3) which have a layout of multipage.

```auto
{{ $p1 := where site.RegularPages "Section" "blog" }}
{{ $p2 := where site.Pages "Params.layout" "=" "multipage" }}
{{ $pages := <!-- join both --> }}

```

---

<div class="post-metadata">

**Author:** ![kaushalmodi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/kaushalmodi/32/2567_2.png) [@kaushalmodi](https://discourse.gohugo.io/u/kaushalmodi)\
**Post date:** [July 7, 2022, 10:28pm UTC](https://discourse.gohugo.io/t/join-two-variables-together/39402/2 "2022-07-07T22:28:16Z")

</div>

See [union](https://gohugo.io/functions/union/).

---

<div class="post-metadata">

**Author:** ![tut](https://avatars.discourse-cdn.com/v4/letter/t/e95f7d/32.png) [@tut](https://discourse.gohugo.io/u/tut)\
**Post date:** [July 7, 2022, 10:46pm UTC](https://discourse.gohugo.io/t/join-two-variables-together/39402/3 "2022-07-07T22:46:35Z")

</div>

I tried this

```auto
{{ $pages := where .Site.RegularPages "Section" "blog" }}
{{ $pages := $pages | union (where .Site.Pages "Params.layout" "=" "multipage") }}

```

But received this error

> **[Screenshot](https://prnt.sc/jVOcgv2_WX6y)**
>
> Captured with Lightshot

Edit: trimming newlines (`{{- -}}`) solved it.

---

<div class="post-metadata">

**Author:** ![kaushalmodi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/kaushalmodi/32/2567_2.png) [@kaushalmodi](https://discourse.gohugo.io/u/kaushalmodi)\
**Post date:** [July 7, 2022, 11:14pm UTC](https://discourse.gohugo.io/t/join-two-variables-together/39402/4 "2022-07-07T23:14:33Z")

</div>

> [@tut](#):
>
> `{{ $pages := $pages | union `

It should be `=` instead of `:=` in this second line.

---

<div class="post-metadata">

**Author:** ![tut](https://avatars.discourse-cdn.com/v4/letter/t/e95f7d/32.png) [@tut](https://discourse.gohugo.io/u/tut)\
**Post date:** [July 7, 2022, 11:16pm UTC](https://discourse.gohugo.io/t/join-two-variables-together/39402/5 "2022-07-07T23:16:42Z")

</div>

The union page you linked to says otherwise?

---

<div class="post-metadata">

**Author:** ![kaushalmodi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/kaushalmodi/32/2567_2.png) [@kaushalmodi](https://discourse.gohugo.io/u/kaushalmodi)\
**Post date:** [July 7, 2022, 11:31pm UTC](https://discourse.gohugo.io/t/join-two-variables-together/39402/6 "2022-07-07T23:31:09Z")

</div>

That’s a typo in the doc. ~~It should be fixed.~~ [fix: Use `=` instead of `:=` for variable reassignments by kaushalmodi · Pull Request #1771 · gohugoio/hugoDocs · GitHub](https://github.com/gohugoio/hugoDocs/pull/1771)

Only the first assignment uses `:=`. Think of it as a variable declaration. Re-assignments use `=`.

---

<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:** [July 7, 2022, 11:52pm UTC](https://discourse.gohugo.io/t/join-two-variables-together/39402/7 "2022-07-07T23:52:23Z")

</div>

You can do it either way:

```auto
{{ $x := 1 }}
{{ $x := add $x 1 }}
{{ $x }} --> 2

{{ $x := 1 }}
{{ $x = add $x 1 }}
{{ $x }} --> 2

```

I like the first way because of the alignment.

---

<div class="post-metadata">

**Author:** ![tut](https://avatars.discourse-cdn.com/v4/letter/t/e95f7d/32.png) [@tut](https://discourse.gohugo.io/u/tut)\
**Post date:** [July 7, 2022, 11:57pm UTC](https://discourse.gohugo.io/t/join-two-variables-together/39402/8 "2022-07-07T23:57:10Z")

</div>

I did mine this way to be consistent with how I have learnt to use Hugo variables

```auto
{{- $p1 := where site.RegularPages "Section" "blog" }}
{{- $p2 := where site.Pages "Params.layout" "multipage" }}
{{- $pages := $p1 | union ($p2) }}

```

---

<div class="post-metadata">

**Author:** ![kaushalmodi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/kaushalmodi/32/2567_2.png) [@kaushalmodi](https://discourse.gohugo.io/u/kaushalmodi)\
**Post date:** [July 8, 2022, 12:09am UTC](https://discourse.gohugo.io/t/join-two-variables-together/39402/9 "2022-07-08T00:09:00Z")

</div>

> [@jmooring](#):
>
> You can do it either way:

~~I didn’t know that!~~

~~So the `=` is useless? I wonder why it was introduced then? Or did `:=` get allowed for reassignments some time later?~~

I really like the consistency in definition:

- `:=` for declarations or first assignments
- `=` for reassignments

* * *

**Update** : OK, I get it. Using `:=` for both initial and later assignments works in the case of `$pages` in this thread because the redefinition is happening in the same scope as the initial assignment.

---

<div class="post-metadata">

**Author:** ![kaushalmodi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/kaushalmodi/32/2567_2.png) [@kaushalmodi](https://discourse.gohugo.io/u/kaushalmodi)\
**Post date:** [July 8, 2022, 12:14am UTC](https://discourse.gohugo.io/t/join-two-variables-together/39402/10 "2022-07-08T00:14:39Z")

</div>

@jmooring About variable redefinition: [Introduction to Hugo Templating | Hugo](https://gohugo.io/templates/introduction/#variables)

In the documentation, I think that that convention should be followed (use `=` for redefinition) for clarity. If the user does `:=` instead of `=` in that variable redefinition example in the above linked doc, they might be surprised by the outcome.

---

<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:** [July 10, 2022, 12:15am UTC](https://discourse.gohugo.io/t/join-two-variables-together/39402/11 "2022-07-10T00:15:05Z")

</div>

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