The variable doesn't change

sorry for the newbie question.

how can I make the test2 turns out to be 1?

    {{$test := 0}}
    {{ if gt 1 0 }}
        {{$test := add $test 1}}
        <div>test1 = {{ $test }}</div>   --> 1
    <div>test2 = {{ $test }}</div>   --> 0


{{$test = add $test 1}}`

:= assigns to a new variable.

but I got this error on version 0.45

unexpected "=" in operand
  1. You must first use the := to initialize the variable, then use = to re-assign to a new value.
  2. Hugo 0.45 is rather old. The re-assignment syntax was introduced in a Go version not too long ago, not sure when, but you should consider a Hugo upgrade.
1 Like

thanks very much.
after upgrading to the latest version, it works. :grinning:

1 Like

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