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

Use integer variables from front matter .Params #551

Closed
galaktor opened this issue Oct 9, 2014 · 8 comments
Closed

Use integer variables from front matter .Params #551

galaktor opened this issue Oct 9, 2014 · 8 comments
Labels

Comments

@galaktor
Copy link
Contributor

galaktor commented Oct 9, 2014

I'm relatively new to Hugo, so I could be doing something wrong...but here goes.

It seems like I cannot use numerical (i.e. integer) values from my front matter params. I wanted to set a number in there and use it as an argument for the "first" function i.e.

+++
foo = 10

range first .Params.foo .Data.Pages

I couldn't debug this properly but from what I saw using some "printf debugging" via the template, the foo variable was a string and first didn't like that.

Any way to cast or use number variables?

Thanks

@tatsushid
Copy link
Contributor

Did you use toml as your front matter, didn't you? I tested toml decoding behavior with a following small code

package main

import (
        "fmt"
        "os"

        "github.com/BurntSushi/toml"
)

func main() {
        tomlStr := `foo = 10`
        var data map[string]interface{}
        if _, err := toml.Decode(tomlStr, &data); err != nil {
                fmt.Fprintln(os.Stderr, err)
                os.Exit(1)
        }
        fmt.Printf("%#v\n", data)
        if data["foo"] != 10 {
                fmt.Println("foo isn't int(10)")
        }
        if data["foo"] != int64(10) {
                fmt.Println("foo isn't int64(10)")
        }
}

It shows 10 is int64 type, not int type. Hugo does as same as above so I think .Param.foo would be int64(10) and it causes the problem you mentioned because first function expects int as its first parameter.

If this is true, one of the workarounds is using YAML as the front matter. YAML library used in Hugo converts 10 to int(10), not to int64(10) and to solve the problem fundamentally, it is needed to change that first function accepts any int family value as its first parameter.

@galaktor
Copy link
Contributor Author

galaktor commented Oct 9, 2014

Thanks for that. I can confirm that it works when I use YAML front matter format. So would be great to support that consistently in the others as well!

@galaktor
Copy link
Contributor Author

galaktor commented Oct 9, 2014

btw same issue seems to exist for .Site.Params defined within config.toml

bep added a commit to bep/hugo that referenced this issue Oct 9, 2014
TOML and YAML handles integers differently, creating issues when using integer values from configuration or front matter in the First template function.

This currently works in YAML (parses into int), but not in TOML (parses into int64).

This commit modifies First so it accepts any int.

Fixes gohugoio#551
@bep
Copy link
Member

bep commented Oct 10, 2014

I cooked a patch fro the First-function, but as there may be more cases (now and in the future), this should maybe be solved by getting the sources in line, either by changing the libraries (not realistic) or create an adapter of kind (should be possible).

The template API have a limited usage scope, but the interface{} type API for simple integer types looks kind of ... not so pretty.

@galaktor
Copy link
Contributor Author

I see the same issue when using the result of "len" as an arg in "gt"

 {{ if gt (len .Params.somelist) 0 }}

where somelist is defined in a TOML front matter

somelist = [ "foo" ]

it seems to work output-wise, but I see this error from hugo

ERROR: 2014/10/12 Rendering error: reflect: call of reflect.Value.Type on zero Value

@bep
Copy link
Member

bep commented Oct 12, 2014

@galaktor My guess your last sample is something different. As it works "output-wise" my best guess is that you have somelist defined in some of the pages, but not all. It's likely the len-function that fails for nil-arguments. In Hugo-world len(nil) should probably be 0 and not return an error.

@galaktor
Copy link
Contributor Author

Cheers @bjornerik that was it. Hard to figure these out sometimes :-)

@spf13 spf13 closed this as completed in da5d98e Oct 15, 2014
tychoish pushed a commit to tychoish/hugo that referenced this issue Aug 13, 2017
TOML and YAML handles integers differently, creating issues when using integer values from configuration or front matter in the First template function.

This currently works in YAML (parses into int), but not in TOML (parses into int64).

This commit modifies First so it accepts any int.

Fixes gohugoio#551
@github-actions
Copy link

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 Apr 20, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants