gohugoio/hugo · error

type %T is not a WeightedPage

Error message

type %T is not a WeightedPage

What it means

Returned by `WeightedPage.Slice` when it is asked to convert a `[]any` whose elements are not all `WeightedPage` values. Slice is the internal adapter used by the `collections.Slice` template function and by `apply`-style template plumbing. The element-level type check at weighted.go:75-78 fires when a heterogeneous slice is passed in.

Solutions

  1. Filter the input slice so every element is a `WeightedPage` before calling the slice helper.
  2. Use `.Pages` or `.Data.Terms` directly instead of routing through `collections.Slice` for weighted pages.
  3. Build the WeightedPages slice in Go/template via `index .Site.Taxonomies` rather than assembling it element-by-element.

Example fix

// template -- before
{{ $mixed := slice .Page (site.Taxonomies.tags.first) }}
{{ $wp := apply $mixed "slice" }}

// after
{{ $wp := .Site.Taxonomies.tags }}
Defensive patterns

Strategy: type-guard

Type guard

// In templates, narrow before passing to weighted-page helpers.
// Go-side:
func isWeightedPage(v any) bool {
	_, ok := v.(page.WeightedPage)
	return ok
}

Try / catch

// In Go template code, filter first:
{{ $wp := slice }}
{{ range where $input "weightedPage" true }}{{ $wp = $wp | append . }}{{ end }}

Prevention

When it happens

Trigger: A template calls a slice/apply helper over a list that mixes `WeightedPage` with other types (Pages, ints, strings), e.g. `apply (seq 5) "weightedPageSlice"`. Most commonly hit via `collections.Slice` invoked with mixed element types where a WeightedPages result was expected.

Common situations: Custom taxonomy templates that pipe a blend of `.Pages` and `.Data.Terms` into a weighted-page construct; partial templates reused across contexts where the param is sometimes a Page; upgrading Hugo where `apply` semantics tightened.

Related errors


AI-assisted analysis of gohugoio/hugo@52c9bd7908 (2026-08-09). Data as JSON: /api/errors/4f612ad86f4fd301. Report an issue: GitHub.

Appendix: source

Thrown at resources/page/weighted.go:77

	return WeightedPage{Weight: weight, Page: p, owner: owner}
}

func (w WeightedPage) String() string {
	return fmt.Sprintf("WeightedPage(%d,%q)", w.Weight, w.Page.Title())
}

// Slice is for internal use.
// for the template functions. See collections.Slice.
func (p WeightedPage) Slice(in any) (any, error) {
	switch items := in.(type) {
	case WeightedPages:
		return items, nil
	case []any:
		weighted := make(WeightedPages, len(items))
		for i, v := range items {
			g, ok := v.(WeightedPage)
			if !ok {
				return nil, fmt.Errorf("type %T is not a WeightedPage", v)
			}
			weighted[i] = g
		}
		return weighted, nil
	default:
		return nil, fmt.Errorf("invalid slice type %T", items)
	}
}

// Pages returns the Pages in this weighted page set.
func (wp WeightedPages) Pages() Pages {
	pages := make(Pages, len(wp))
	for i := range wp {
		pages[i] = wp[i].Page
	}
	return pages
}

View on GitHub (pinned to 52c9bd7908)