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 Page method/field to taxonomy object #12316

Closed
jmooring opened this issue Mar 29, 2024 · 3 comments · Fixed by #12363
Closed

Add Page method/field to taxonomy object #12316

jmooring opened this issue Mar 29, 2024 · 3 comments · Fixed by #12363

Comments

@jmooring
Copy link
Member

We can get a reference to a term page with:

{{ $termPage := site.Taxonomies.tags.foo.Page }}

It would be convenient if we could get a reference to a taxonomy page with:

{{ $taxonomyPage := site.Taxonomies.tags.Page }}

That may not be possible, despite the fact we lowercase terms internally (e.g., there shouldn't be a collision between tags.page (a term) and tags.Page (a method/field).

The alternative has always seemed less elegant compared to how you can get a reference to a term page:

{{ site.GetPage "/tags" }}
@bep
Copy link
Member

bep commented Apr 3, 2024

This is what site.Taxonomies.tags gets you:

type Taxonomy map[string]WeightedPages
  • We cannot change the type (or: not until we get some better "struct iterator" support in Go templates)
  • We cannot add state other than WeightedPages (e.g. tags)

What we in theory could do, but that would not work for empty taxonomies:

func (t Taxonomy) Page() page.Page {
   var first WeightedPages
   range _, v := t {
        first = v
        break
    }
    if first == nil {
         return nil
     }
     return first.Page().Parent()
}

@bep bep removed the NeedsTriage label Apr 3, 2024
@bep bep added this to the v0.125.0 milestone Apr 3, 2024
@jmooring
Copy link
Member Author

jmooring commented Apr 3, 2024

I tested with this:

// Page returns the taxonomy page or nil if the taxonomy has no terms.
func (i Taxonomy) Page() Page {
	var first WeightedPages
	for _, v := range i {
		first = v
		break
	}
	if first == nil {
		return nil
	}
	return first.Page().Parent()
}

and this:

{{ range $taxonomy, $terms := site.Taxonomies }}
  {{ with $terms }}
    <h2><a href="{{ .Page.RelPermalink }}">{{ .Page.LinkTitle }}</a></h2>
    {{ range $term, $weightedPages := . }}
      <h3><a href="{{ .Page.RelPermalink }}">{{ .Page.LinkTitle }}</a></h3>
      {{ range $weightedPages }}
        <a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
      {{ end }}
    {{ end }}
  {{ end }}
{{ end }}

Works great.

I think this is OK as long as we document the "returns nil if no term pages" part.

And it's kind of sneaky, so I like it.

@bep bep added Enhancement and removed Proposal labels Apr 4, 2024
jmooring added a commit to jmooring/hugo that referenced this issue Apr 11, 2024
jmooring added a commit to jmooring/hugo that referenced this issue Apr 12, 2024
bep pushed a commit that referenced this issue Apr 12, 2024
Copy link

github-actions bot commented May 4, 2024

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 4, 2024
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.

2 participants