Floating point value in weight?

I am testing if i canb use hugo as a readonly front-end for an inventory database where each item has floating point weight in the frontmatter.

It seems it is converted to an int by Hugo. Is there any way i can get to the original floating point weight value in the theme?

So, weight is one of the built-in front matter keywords in Hugo, and is int.

I would recommend you put custom data below params, e.g.:

--
title: My Product
weight: 100 # is converted to int, controls page sort, but can also be accessed with .Weight on the Page.
params:
    weight: 3.14
---

And access weight in template with .Params.weight.

Thank you. Unfortunately - I dont think i have the flexibility to define the frontmatter in this project thus. I think i have to assume that my “custom” parameters are in the root of the yaml structure. E.g. like this:

title: a test title
weight: 130.2
packaged by: test person

So far hugo works great even if that is not strictly how it is recommended in the docs. My one issue is the integer weights.

Here’s my wishful thinking.

  • {{ .Weight }} -this returns the integer weight used by hugo sorting.
  • {{ .Params.Weight }} -this could return the raw floating point value.
    currently both returns 130.

Alternatively it could be something like:

  • {{ .Params.Weight.RawValue }}
  • {{ .RawParams.Weight }}
  • …or hugo could ‘just’ decide to start using a float for weight.

I expect none of these are small changes/ feasible - But maybe other people who would like to use hugo for a webshop or similar would also like to see this change.

Q: Does it make sense as a feature request on github?


I am also considering if i can hack something together using:
unmarshal and resources.Get on the page file to somehow get access to the raw frontmatter.
i dont want to do this. But if it works - it works.

Yea, looking back, that’s how I guess it should have been. Not sure if we can/should break that now, but that is the closest thing I can see to a acceptable proposal of the options you listed. But … I suggest you create a GitHub issue.

My take on this… if you need a custom parameter in front matter, place it under the params key. This is consistent with the way we handle custom parameters in the site configuration. Any field not under the params key is reserved.

So why does this work?

+++
title = 'Example'
foo = 'bar'
+++
{{ .Page.Params.foo }} --> "bar"

Because we made the addition of the front matter params key in v0.123.0 backwards compatible, but I hope at some point we’ll be able to deprecate that.

Yea, and in any case; if you store custom data in front matter (e.g. products), I would say that it’s always a good thing to somehow name space it (e.g. item), which would not have the weight issue described here wherever you put it.

1 Like