Frontmatter: only strings?

Can I define int type in YAML frontmatter param? Or how to cast string into int?
I’m trying to use mul function - and get the error message: executing “main” at <int .Params.price1>: error calling int: unable to cast “” of type string to int
Or just keep all ints in data.yml?

If you write something along the lines of int = 14 it should be parsed as integer (see? no " around the value). It could be that this is a newer feature.

If all else fails, you can typecaste the value into an integer with int:

looks like there is a page without params price1 set.

better wrap it with with

{{ with .Params.price1 }}
  {{ mul 2 . }}
{{ end }}

Yes, YAML supports strings, integer, float. dates …

… or even add a | default 123 to it, depending on the use case.

@pamubay: it produces error message: executing “main” at <mul 2 .>: error calling mul: can’t apply the operator to the values.
How do I define int in YAML frontmatter? At the moment it is
price1: 242

The YAML tutorial syas “YAML recognizes unquoted numerals as integers or floating point.” Not im my case.

How are you trying to “use the mul function”?

Provide sample code and sample frontmatter.

Also (but that might just be me): What is mul?

mul is Multiply Function.
I’m sorry - it was my bad. I’ve corrected it.
I have a single .html template and multiple .md files - in some of which the price definition was quoted, like price1: “242:00”
To make YAML define it as int it should be an unquoted number! Like
price1: 242
And now it works! The math part is
<h3 class="product-price">{{ with .Params.price1 }}{{ mul 2 . }}{{ end }}</h3>

But if I want to multiply not by 2, but by a value stored in .Params or in /data/prices.yml?

Thank you!

1 Like

But if I want to multiply not by 2, but by a value stored in .Params or in /data/prices.yml?

Then we are back to typecasting:

{{ $somevariable := "123" | int }}
{{ mul 2 $somevariable }}

{{ $somevariable := "123" }}
{{ mul 2 (int $somevariable) }}

Note: In my opinion it’s always better to force type casting when you are working with a value that “someone” can add in the config. You never have 100% influence on them doing their job properly. So better cast the integer. It will probably create a int(0) if anything like “two” is in the variable.

But if I want to multiply not by 2, but by a value stored in .Params or in /data/prices.yml?
<h3 class="product-price">{{ with .Params.price1 }}{{ mul .Params.margin . }}{{ end }}</h3>
but this does not work

Accessing current global Page context inside {{with}} block, you can add $ to the variable.

<h3 class="product-price">
{{ with .Params.price1 }}
  {{ mul $.Params.margin . }}
{{ end }}

Or, you can store it inside $variable first.

{{ $margin := .Params.margin }}
<h3 class="product-price">
{{ with .Params.price1 }}
  {{ mul $margin . }}
{{ end }}

if you want access the .Site.Data inside {{with}} block, then use site function.

{{ site.Data.prices.YOUR_KEY }}

Great, this works! Thank you!
The last thing - to format it using .toFixed(2)

1 Like

Will it be the last thing really? :wink:

Have a look at printf and this docs:

Probably something like:

{{ printf "%.2f" $thenumber }}

Thank you! It should be like this
{{ printf “%.2f” {{ with .Params.price1 }}{{ mul $.Params.margin . }}{{ end }} }}
but this nesting is invalid.

Now I need to format the mul result to .toFixed(2) or

  • but can’t make it work.
    Alternatively, I wanted to store the result to a variable, but the definition does not accept that expression as argument. May be you know how to do that.

you can do it like this.

{{ $result := 0 }}

<h3 class="product-price">
{{ with .Params.price1 }}
  {{ $result =  printf "%.2f" (mul $.Params.margin . ) }}
{{ end }}

{{ $result }}

That’s great, thank you very much!!!
So, the result is stored into $result variable!
This is displayed on the product page. But I also want to print the $result value on the index page.
The index page shows an error message: undefined variable "$result"

I’ve finally decided to output something else on the index page.
Those who still want to output price to the index page - take a look at .Scratch here:

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