About if else problem

I wanna use if else in shortcode, when if something, that’s OK, but when

  {{ if "alert info" }}
    {{ $color := "blue" }} {{ $alerttitle := "Info" }}
    {{ else if "alert success" }}
    {{ $color := "green" }} {{ $alerttitle := "Success" }}
    {{ else if "alert warning" }}
    {{ $color := "orange" }} {{$alerttitle := "Warning" }}
    {{ else if "alert danger" }}
    {{ $color := "red" }} {{$alerttitle := "Danger" }}
something
 {{end}}

It doesn’t work, but it doesn’t report errors.
I don’t know why this is happening, is there another way to do it?
Thanks

I’m assuming you are trying to use the value of $alerttitle and $color outside of the if block. The reason it is not working is because of scope. See: https://gohugo.io/templates/introduction/#variables

So you need to initialize (:=) the variables outside of the if block, and update the values inside (=).

{{ $color := "foo" }}
{{ $alerttitle := "bar" }}

  {{ if "alert info" }}
    {{ $color = "blue" }} {{ $alerttitle = "Info" }}
    {{ else if "alert success" }}
    ...
 {{end}}
{{ $color }}
{{ $alerttitle }}

Notice the difference: := vs =.

According to your answer, has been able to output the results, but the results are incorrect, only the contents of the output info, can not achieve if the judgment, such as success, warning, danger are still output info content

I thought this was just your pseudo code. Your if statement is not doing any condition checking. Read here: Templating | Hugo

Do you have your code somewhere we can have a look at? It may be easier to help you if we can see what your code looks like.

in alert.html

<div role="alert">
    {{ $color := "foo" }}
    {{ $alerttitle := "bar" }}
    {{ if "alert info" }}
    {{ $color = "blue" }} {{ $alerttitle = "INFO" }}
    {{ else if "alert success" }}
    {{ $color = "green" }} {{ $alerttitle = "Success" }}
    {{ else if "alert warning" }}
    {{ $color = "orange" }} {{$alerttitle = "Warning" }}
    {{ else if "alert danger" }}
    {{ $color = "red" }} {{$alerttitle = "Danger" }}
    {{end}}
    <div class="alert {{ range .Params }}{{ . }} {{ end }}bg-{{ $color }}-500 text-white font-bold rounded-t px-4 py-2">
        {{ $alerttitle }}
    </div>

    <div class="border border-t-0 border-{{ $color }}-400 rounded-b bg-blue-100 px-4 py-3 text-{{ $color }}-700">
        <p>{{ .Inner | markdownify }}</p>
    </div>


</div>

in post.md

{{< alert info >}}

...

{{< /alert >}}
{{< alert success >}}

...

{{< /alert >}}

I don’t know which condition to judge in this case.

You have to test the parameter you give to your shortcode.

See Get

Your code {{ if "alert info" }} is not doing anything helpfull.