Get Integer from Shortcode template. Cast from String -> Int?

I have a shortcode setup called score, which has a attribute called rating.

{{< score rating="2">}}

What I’m trying to do is show a certain image 2 times and a different image 5-2 times.

{{ range $i, $sequence := (seq 5) }} {{ if lt .Get "rating" 4 }} <img src="/img/icons/full.svg"/> {{ else }} <img src="/img/icons/empty.svg"/> {{ end }} {{ end }}

I think this is failing because .Get Rating is returning a string and not an int.

Is there a workaround for this? Go does have an int method, but I cant understand how to use it? This is what I’ve tried:

` {{ range $i, $sequence := (seq 5) }}
{{ if lt 1 (int .Get “rating”)}}
it’s equal

{{ else }}

{{ end }}

`

Yes, you should be able to use int.

{{ range $i, $sequence := (seq 5) }}
{{   if lt (int (.Get "rating")) 4 }} 
<img src="/img/icons/full.svg"/>
{{   else }}
<img src="/img/icons/empty.svg"/>
{{   end }}
{{ end }}

Thanks for the reply, unfortunately, when I use that approach, the page doesn’t show any output.

The below evaluates to true, so I know the format is correct, however when the “3” is replaced with .Get "rating" or even .Get 0, I see no result in the HTML.

{{ if lt (int ("3")) 4 }}

Any idea what it could be? I’m unable to see any error in the web server log or in the actual page, so it’s difficult to debug.

.Get "rating" returns 5 in the HTML.

I just tested the sample code I posted earlier, and it works for me. Try running Hugo with --verbose and see if you are getting any errors in that shortcode.

As a bonus, here’s another (untested) approach:

{{ $ratings := dict "1" "one" "2" "two" "3" "three" "4" "four" "5" "full" }}

{{ if isset $ratings (.Get "rating") }}
<img src="/img/icons/{{ .Get "rating" }}.svg"/>
{{ else }}
<img src="/img/icons/empty.svg"/>
{{ end }}

Your second untested example works perfectly well (thanks!) but since I need to print out the image x times, I’m unable to use it.

I really can’t understand why the first example doesn’t work, the error I get in the console is:

ERR: template: theme/shortcodes/froth.html:9:17: executing "theme/shortcodes/froth.html" at <.Get>: can't evaluate field Get in type int

To confirm, the code I’m using is:

{{ range $i, $sequence := (seq 5) }} {{ if lt (int (.Get "rating")) 4 }} <img src="/img/icons/full.svg"/> {{ else }} <img src="/img/icons/empty.svg"/> {{ end }} {{ end }}

If it helps, my Hugo version is:
Hugo Static Site Generator v0.16 BuildDate: 2016-06-06T13:29:38+01:00

And I’m running it with the following command:

hugo server --buildDrafts --theme=latte --renderToDisk --verbose

UPDATE
After debugging it some more, I’ve managed to find out some information that might help.

If I print out the .Get outside of the range loop, the value gets printed, but if I define it inside the loop, I get an error. For example, the following results in the value of the rating printed before the image.

  {{ range $i, $sequence :=  (seq 5) }}
    <img src="/img/icons/rating.svg/>   
  {{ end }}```

The following does **not** work, because the .Get is inside the range, the error I get is ```'can't evaluate field Get in type int'```:

{{ range $i, $sequence := (seq 5) }}
{{ .Get “rating” }}
<img src="/img/icons/rating.svg/>
{{ end }}```

Duh. I forgot that the context changes inside the loop. Use a dollar sign to reference the initial template context.

{{ range $i, $sequence := (seq 5) }}
{{   if lt (int ($.Get "rating")) 4 }}
<img src="/img/icons/full.svg"/>
{{   else }}
<img src="/img/icons/empty.svg"/>
{{   end }}
{{ end }}

PS - to show a code block in the discussion forums, use three backticks to fence the block:

{{ .Get 0 }}

1 Like

That worked! Thanks for the assistance, I’ve learnt about the scoping and also loops! :raised_hands: