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 · 7 comments

Comments

Projects
None yet
3 participants
@digitalcraftsman
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

This comment has been minimized.

Show comment
Hide comment
@bep

bep Mar 12, 2017

Member

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.

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

This comment has been minimized.

Show comment
Hide comment
@digitalcraftsman

digitalcraftsman Mar 12, 2017

Member

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.

Member

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 added the Enhancement label Mar 13, 2017

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

@bep

This comment has been minimized.

Show comment
Hide comment
@bep

bep May 18, 2017

Member

@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.

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

This comment has been minimized.

Show comment
Hide comment
@digitalcraftsman

digitalcraftsman May 18, 2017

Member

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

Member

digitalcraftsman commented May 18, 2017

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

This comment has been minimized.

Show comment
Hide comment
@bep

bep May 18, 2017

Member

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

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

This comment has been minimized.

Show comment
Hide comment
@moorereason

moorereason May 18, 2017

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.

Contributor

moorereason commented May 18, 2017

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

This comment has been minimized.

Show comment
Hide comment
@bep

bep May 18, 2017

Member

@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.

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 bep modified the milestones: v0.22, v0.21 May 20, 2017

@bep bep modified the milestones: v0.22, v0.23 Jun 7, 2017

@bep bep modified the milestones: v0.23, v0.24, v0.25 Jun 16, 2017

@bep bep self-assigned this Jul 3, 2017

bep added a commit to bep/hugo that referenced this issue Jul 3, 2017

tpl/collections: Add Pages support to Intersect and Union
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

@bep bep closed this in #3673 Jul 3, 2017

bep added a commit that referenced this issue Jul 3, 2017

tpl/collections: Add Pages support to Intersect and Union
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment