# About if else problem

**URL:** <https://discourse.gohugo.io/t/about-if-else-problem/29028>\
**Category:** support\
**Created:** [October 25, 2020, 1:08pm UTC](https://discourse.gohugo.io/t/about-if-else-problem/29028 "2020-10-25T13:08:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rhinoceros](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/rhinoceros/32/6115_2.png) [@Rhinoceros](https://discourse.gohugo.io/u/Rhinoceros)\
**Post date:** [October 25, 2020, 1:08pm UTC](https://discourse.gohugo.io/t/about-if-else-problem/29028/1 "2020-10-25T13:08:09Z")

</div>

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

```auto
  {{ 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

---

<div class="post-metadata">

**Author:** ![pointyfar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/pointyfar/32/5797_2.png) [@pointyfar](https://discourse.gohugo.io/u/pointyfar)\
**Post date:** [October 25, 2020, 1:18pm UTC](https://discourse.gohugo.io/t/about-if-else-problem/29028/2 "2020-10-25T13:18:32Z")

</div>

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](https://gohugo.io/templates/introduction/#variables)

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

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

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

```

Notice the difference: `:=` vs `=`.

---

<div class="post-metadata">

**Author:** ![Rhinoceros](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/rhinoceros/32/6115_2.png) [@Rhinoceros](https://discourse.gohugo.io/u/Rhinoceros)\
**Post date:** [October 25, 2020, 2:04pm UTC](https://discourse.gohugo.io/t/about-if-else-problem/29028/3 "2020-10-25T14:04:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pointyfar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/pointyfar/32/5797_2.png) [@pointyfar](https://discourse.gohugo.io/u/pointyfar)\
**Post date:** [October 25, 2020, 2:09pm UTC](https://discourse.gohugo.io/t/about-if-else-problem/29028/4 "2020-10-25T14:09:14Z")

</div>

> [@Rhinoceros](#):
>
> `{{ if "alert info" }}`

I thought this was just your pseudo code. Your `if` statement is not doing any condition checking. Read here: [Templating | Hugo](https://gohugo.io/templates/introduction/#example-4-if--else)

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.

---

<div class="post-metadata">

**Author:** ![Rhinoceros](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/rhinoceros/32/6115_2.png) [@Rhinoceros](https://discourse.gohugo.io/u/Rhinoceros)\
**Post date:** [October 25, 2020, 2:14pm UTC](https://discourse.gohugo.io/t/about-if-else-problem/29028/5 "2020-10-25T14:14:36Z")

</div>

in _alert.html_

```auto
<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_

```auto
{{< alert info >}}

...

{{< /alert >}}

```

```auto
{{< alert success >}}

...

{{< /alert >}}

```

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

---

<div class="post-metadata">

**Author:** ![divinerites](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/divinerites/32/12625_2.png) [@divinerites](https://discourse.gohugo.io/u/divinerites)\
**Post date:** [October 25, 2020, 6:11pm UTC](https://discourse.gohugo.io/t/about-if-else-problem/29028/6 "2020-10-25T18:11:07Z")

</div>

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

See [`Get`](https://gohugo.io/functions/get/#readout)

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