gohugoio / hugo

The world’s fastest framework for building websites.
https://gohugo.io
Apache License 2.0
76.28k stars 7.55k forks source link

Improve .Page.Fragments.ToHTML #13107

Closed jmooring closed 5 hours ago

jmooring commented 8 hours ago

The Fragments.ToHTML method on Page takes three arguments:

func (toc *Fragments) ToHTML(startLevel, stopLevel int, ordered bool) template.HTML {
  ...
}

It would be convenient to pass site and/or page parameters to this method, something like:

{{ .Fragments.ToHTML (.Param "toc.startLevel") (.Param "toc.endLevel") true }}

The above throws an error with JSON and TOML site/page parameters because the values are unmarshaled to float64 (JSON) or int64 (TOML). You can work around this with:

{{ .Fragments.ToHTML (.Param "toc.startLevel" | int) (.Param "toc.endLevel" | int) true }}

But it would easier if the method signature were...

func (toc *Fragments) ToHTML(startLevel, stopLevel any, ordered bool) template.HTML {
  ...
}

... with cast.ToIntE, etc.

bep commented 7 hours ago

Yea, we could do this (I wish Go generics could help us with this, but I don't think so). Note that should be possible to do:

{{ .Fragments.ToHTML (.Param "toc.startLevel" | int) (.Param "toc.endLevel" | int) true }}