Hugo modifying Site.Params.phone value

Hi there,

I am unable to understand why Hugo change these numbers so I have

#config/_default/params.yaml
phone: 0101010101
#layouts/page/directory.html
<a href="tel:{{ .Site.Params.phone }}">{{ .Site.Params.phone }}</a>
# the output html
<a href="tel:17043521">17043521</a>

cannot understand why it change the random given number in params.yaml file to something else

0000000000 = 0
0101010101 = 17043521

it only change few numbers not all which made me really confused in some places where I am giving example to the users like use the phone format 000 000 0000 etc to show only 0

if I add the | safeHTML in layouts it still show the same weird number

thanks for taking the time

If you want it to be a string:

phone: "0101010101"

if you want it to be a decimal (base 10) integer:

phone: 101010101

The way you have it right now, it’s an octal (base 8) number, which when converted to decimal is 17043521.

Read the YAML spec.

2 Likes

thanks :pray: @jmooring

it does make sense to me now I thought Hugo simply copy paste what ever I add in params as text/number.

Very much related :laughing: : Golang Quirk: Number-strings starting with "0" are Octals ❚ A Scripter's Notes

Never prefix number values with 0.

1 Like

I cannot avoid using 0 in the beginning as some places have zip code starting with 0 etc or local phone numbers have a phone format starting with 0

Does this apply even when I put the value like this “0” instead of my old mistake without the “” thanks

If you use the numbers in string format with double quote wrapping, you should be fine… but then don’t try to convert that string to int in the template using int otherwise it will once again be converted to Octal.

1 Like

thanks @kaushalmodi for sharing the int issue I was trying to understand how to make use of {{ int ("0987" | strings.TrimLeft "0") }} to remove 0 when showing international format

the problem I am trying to solve here is the Australian phone number format local numbers usually start with 04xx xxx xxx but then for international format that 0 disappear and become something like +61 4 xxxx xxxx

but now knowing it might create issues I simply added 2 additional lines in params.yaml

phone: "04"
phoneInt: "4"
countrycode: "61"

And in layouts I format those numbers like this

#local version 04xx xxx xxx for Australian users with language en Australian 
<a href="tel:{{ .Site.Params.phone }}">{{ .Site.Params.phone }}</a>

# international version +61 4 xxxx xxxx with en or en-us
<a href="tel:+{{ .Site.Params.countrycode }}{{ .Site.Params.phoneInt }}">+{{ .Site.Params.countrycode }} {{ .Site.Params.phoneInt }}</a>

seems like workarounds and patches but it seems to be working. Also I started adding all numbers in “” limited to my knowledge of Hugo this is the best I comeup with :slight_smile:

Not tested

Why not do just:

phone: "4"
countrycode: "61"

And in layouts I format those numbers like this

#local version 04xx xxx xxx for Australian users with language en Australian 
{{ (printf `<a href="tel:0%s">0%s</a>` site.Params.phone site.Params.phone) | safeHTML }}

# international version +61 4 xxxx xxxx with en or en-us
{{ (printf `<a href="tel:+%s%s">+%s %s</a>` site.Params.countrycode Params.phone site.Params.countrycode site.Params.phone) | safeHTML }}
1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.