I’m trying to add some schema.org data to a page. Specifically, I am trying to add a baseSalary value which is required to be the format 100000.00
(as a float with 2-decimal precision).
In the front matter I’m storing this information as an int, i.e. 100000
. I can’t figure out how to get this to render as 100000.00
in the template.
My attempts so far:
{{ .Params.base_salary }}
--> 100000
(int)
{{ lang.NumFmt 2 .Params.base_salary "- ." }}
--> "100000.00"
(string)
{{ float (lang.NumFmt 2 .Params.base_salary "- .")}}
--> 100000
(int)
{{ (printf "%.2f" .Params.base_salary) }}
--> "%!f(int64=100000)"
(wat?)
I’m sure Hugo can do this, I’m just missing something obvious.
ju52
December 10, 2020, 4:20pm
2
{{ .Params.base_salary }}.00
{{ .Params.base_salary }}.00
--> 100000 .00
(note the weird space)
{{- .Params.base_salary -}}.00
--> 100000 .00
(same thing)
In full context:
"value": {{- .Params.base_salary -}}.00,
--> "value": 100000 .00,
Because this is invalid JSON, the site won’t build:
Error building site: failed to render pages: JSON parse error: expected comma character or an array or object ending on line 33 and column 25
33: "value": 100000 .00,
ju52
December 10, 2020, 4:36pm
4
{{print .Params.base_salary “.00”}}
should do the trick
{{ print .Params.base_salary ".00" }}
--> "100000.00"
(string)
{{ float (print .Params.base_salary ".00") }}
--> 100000
(int)
bep
December 10, 2020, 5:02pm
6
{{ printf 1234.5678 “%.2f” }}
@bep did you mean {{ printf "%.2f" 1234.5678 }}
? if so, that prints the number as a string.
As I mentioned above, {{ (printf "%.2f" .Params.base_salary) }}
leads to "%!f(int64=100000)"
which I cannot understand.
markgoho:
.Params.base_salary
Well, how does your frontmatter look like?
blafasel = “123.12”
blafasel = 123.12
blafasel = 123
the first one is a string, the second one a float, the third one an integer. The problem seems to be in the way you define the values.
Please see original post.
I saw, but not the original frontmatter. Just what came out of it.
The space in {{ .Params.base_salary -}}.00
doesn’t make much sense in a templating way.
You should be able to use the lang.NumFmt
string result. Can you share the context of where you’re trying to use this in the template?
@moorereason I’m trying to create a JSON object in the template
Front Matter:
+++
base_salary = 100000
+++
What I’m trying to achieve:
"value": {
"@type": "QuantitativeValue",
"value": 100000.000
}
What all previous attempts have led to:
"value": {
"@type": "QuantitativeValue",
"value": 100000 , // {{- .Params.base_salary -}}
"value":"100000.00", // {{- lang.NumFmt 2 .Params.base_salary "- ." }}
"value": 100000 , // {{- float (lang.NumFmt 2 .Params.base_salary "- .") -}}
"value": 100000 .00, // {{- .Params.base_salary -}}.00
"value": "100000.00", // {{ print .Params.base_salary ".00" }}
"value": 100000 , // {{ float (print .Params.base_salary ".00") }},
"value": "%!f(int64=100000)", // {{ printf "%.2f" .Params.base_salary }}
}
Some are very close, but the format has to be precise: a float with two-decimal precision, i.e. 100000.00
.
bep
December 10, 2020, 8:26pm
13
OK, sorry, f
expects a float, so:
printf "%.2f" (float .Params.base_salary)
Or use @moorereason 's suggestion.
@bep printf
returns a string, I need a float
Use safeJS
to prevent the template engine from quoting the string:
{{ lang.NumFmt 2 10000 | safeJS }}
2 Likes
@moorereason thank you so much for this! I knew it had to be simple. Yay!
{{ lang.NumFmt 2 .Params.base_salary "- ." | safeJS }}
--> 100000.00
also:
{{ print .Params.base_salary ".00" | safeJS }}
works, but feels less right since we’re hardcoding that extra bit
ju52
December 11, 2020, 8:41am
17
You should use **jsonify ** instead of safeJS , if you output a JSON file
like this
"size": {{ $feedsize | jsonify }}
you can use the float function to convert a string to a float value
{{ print .Params.base_salary ".00" | float | jsonify }}
This is incorrect.
jsonify
returns a template.HTML
type . The string would still be quoted by the template engine, which wants a template.JS
type in a JSON context.
Additionally, jsonify
calls the JSON marshaller, which is considerably heavier than what safeJS
is doing. safeJS
is essentially a type cast, which is really all you need here to satisfy the template engine’s security model.
1 Like
ju52
December 11, 2020, 5:07pm
19
I’m not so deep inside Hugo / Go.
jsonify returns a value for float input without quotes. Tried it
OK, only my 2c, don’t like to extend this thread to infinity
system
Closed
December 13, 2020, 5:08pm
20
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.