Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for pages in the union/intersect template function #3174

Closed
digitalcraftsman opened this issue Mar 12, 2017 · 8 comments · Fixed by #3673
Closed

Add support for pages in the union/intersect template function #3174

digitalcraftsman opened this issue Mar 12, 2017 · 8 comments · Fixed by #3673
Assignees
Milestone

Comments

@digitalcraftsman
Copy link
Member

digitalcraftsman commented Mar 12, 2017

Complex where-nestings could be simplified by filtering pages with union and intersect:

  • union corresponds to the logical OR operator (x is an element of set A OR set B)
  • intersect corresponds to the logical AND operator (x is an element of set A AND set B)

Example: find all pages where .Params.a equals "v1" AND .Params.b equals "v2"

{{ $v1 := where .Site.Pages "Params.a" "v1" }}
{{ $v2 := where .Site.Pages "Params.b" "v2" }}

{{ $filtered := $v1 | intersect $v2 }}
@bep
Copy link
Member

bep commented Mar 12, 2017

Note that instead of adding support for one and one type, it should support any type that can be compared by == and !=.

And I think you switched AND and OR in the bullet points above.

@bep bep added this to the v0.20 milestone Mar 12, 2017
@digitalcraftsman
Copy link
Member Author

digitalcraftsman commented Mar 12, 2017

Note that instead of adding support for one and one type, it should support any type that can be compared by == and !=.

Makes sense

And I think you switched AND and OR in the bullet points above.

The typical copy-and-paste dilemma. It's fixed now.

@bep bep modified the milestones: v0.21, v0.20 Mar 31, 2017
@bep
Copy link
Member

bep commented May 18, 2017

@moorereason @digitalcraftsman The intersect func should be fine now, right? It would be cool if we could also get the union up to speed? That would give us a good story on page queries.

@digitalcraftsman
Copy link
Member Author

Great! We should also add some nice examples to the docs. We should link from the docs for the where template func to examples for union and intersection.

/cc @rdwatters

@bep
Copy link
Member

bep commented May 18, 2017

@digitalcraftsman my post was riddled with question marks, which may be good to address before we start worrying about examples.

@moorereason
Copy link
Contributor

The union enhancements are tracked in #3411. I hope to get to those in the v0.21 time frame (feel free to claim that issue if you want to do it!). They should look much like the intersect updates in #3328.

I still don't think we support Page comparisons. We are doing comparisons of basic types. We don't compare structs. I looked at comparing Pages a few days ago and ran into an issue with cyclical imports since tpl/collections would need to import hugolib.

@bep
Copy link
Member

bep commented May 18, 2017

@moorereason re the cyclic, I had a Git stash laying around, pushed it in #3484 -- I probably will not complete this, so take what you want ...

You don't really need to test real pages, just make up some structs and slices of struct pointers.

bep added a commit to bep/hugo that referenced this issue Jul 3, 2017
This enables `AND` (`intersect`)  and `OR` (`union`)  filters when combined with `where`.

Example:

```go
{{ $pages := where .Site.RegularPages "Type" "not in" (slice "page" "about") }}
{{ $pages := $pages | union (where .Site.RegularPages "Params.pinned" true) }}
{{ $pages := $pages | intersect (where .Site.RegularPages "Params.images" "!=" nil) }}
```

The above fetches regular pages not of `page` or `about` type unless they are pinned. And finally, we exclude all pages with no `images` set in Page params.

Fixes gohugoio#3174
@bep bep closed this as completed in #3673 Jul 3, 2017
bep added a commit that referenced this issue Jul 3, 2017
This enables `AND` (`intersect`)  and `OR` (`union`)  filters when combined with `where`.

Example:

```go
{{ $pages := where .Site.RegularPages "Type" "not in" (slice "page" "about") }}
{{ $pages := $pages | union (where .Site.RegularPages "Params.pinned" true) }}
{{ $pages := $pages | intersect (where .Site.RegularPages "Params.images" "!=" nil) }}
```

The above fetches regular pages not of `page` or `about` type unless they are pinned. And finally, we exclude all pages with no `images` set in Page params.

Fixes #3174
@Anastasiia-XFR
Copy link

Anastasiia-XFR commented Sep 21, 2021

If there two range, for example:
{{ $arr1 := slice "AU" "DE" "GB" "IT" "FR" }}
{{ $arr2 := slice "RU" "DE" "FR" }}
How can I find different element, for this case "GB" and "IT"?

Oh, I find it
{{ $res := $arr1 | symdiff $arr2 }}
{{ $res }} or {{ $res = uniq $res | sort }}

@gohugoio gohugoio locked as resolved and limited conversation to collaborators Sep 22, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants