# Conditionally checking for non existent variable

**URL:** <https://discourse.gohugo.io/t/conditionally-checking-for-non-existent-variable/4004>\
**Category:** support\
**Created:** [August 31, 2016, 12:44am UTC](https://discourse.gohugo.io/t/conditionally-checking-for-non-existent-variable/4004 "2016-08-31T00:44:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![robsonsobral](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/robsonsobral/32/1628_2.png) [@robsonsobral](https://discourse.gohugo.io/u/robsonsobral)\
**Post date:** [August 31, 2016, 12:44am UTC](https://discourse.gohugo.io/t/conditionally-checking-for-non-existent-variable/4004/1 "2016-08-31T00:44:52Z")

</div>

Hi!

I’m really lost in here. How can I check for a variable that isn’t always available?

`{{ if or (.IsHome) (and .IsPage (eq .File.BaseFileName "running")) }}`

I tried a lot of different codes, but… `at <.File.BaseFileName>: File is not a field of struct type *hugolib.Node in partials/footer.html`

I’m sorry. I still can’t find my way on GoLang.

Thank you.

---

<div class="post-metadata">

**Author:** ![michael\_henderson](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/michael_henderson/32/10490_2.png) [@michael\_henderson](https://discourse.gohugo.io/u/michael_henderson)\
**Post date:** [August 31, 2016, 2:19am UTC](https://discourse.gohugo.io/t/conditionally-checking-for-non-existent-variable/4004/2 "2016-08-31T02:19:43Z")

</div>

There is a function called “with” that you can use. If the variable is set, then the code in the block will run. If it isn’t, then it will be skipped.

```auto
{{ with .File }}{{ with .File.BaseFileName }}
...something...
{{ end }}{{ end }}

```

There are some more examples in [http://gohugo.io/templates/functions/](http://gohugo.io/templates/functions/)

---

<div class="post-metadata">

**Author:** ![robsonsobral](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/robsonsobral/32/1628_2.png) [@robsonsobral](https://discourse.gohugo.io/u/robsonsobral)\
**Post date:** [August 31, 2016, 11:37am UTC](https://discourse.gohugo.io/t/conditionally-checking-for-non-existent-variable/4004/3 "2016-08-31T11:37:02Z")

</div>

Thank you.

I can’t use `with` because I have two conditions: be the homepage or be the `running` page.

I saw that you edited your answer. My first try was using `isset .File "BaseFileName"` and it didn’t work too.

Thank you.

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [August 31, 2016, 12:11pm UTC](https://discourse.gohugo.io/t/conditionally-checking-for-non-existent-variable/4004/4 "2016-08-31T12:11:40Z")

</div>

If .IsPage

---

<div class="post-metadata">

**Author:** ![robsonsobral](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/robsonsobral/32/1628_2.png) [@robsonsobral](https://discourse.gohugo.io/u/robsonsobral)\
**Post date:** [August 31, 2016, 12:48pm UTC](https://discourse.gohugo.io/t/conditionally-checking-for-non-existent-variable/4004/5 "2016-08-31T12:48:38Z")

</div>

Thank you for your attention.

I tried `.IsPage` together with `.File.BaseFileName`, but… No luck. The last condition is always evaluated and it crashes the code.

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [August 31, 2016, 3:57pm UTC](https://discourse.gohugo.io/t/conditionally-checking-for-non-existent-variable/4004/6 "2016-08-31T15:57:36Z")

</div>

Yes, that is a curious effect of how Go templates and/or operator, I think. You must map the entire conditional in an if:

```auto
{{ if .IsPage }}
{{ if eq .File... }}
{{ end }}
{{ end }}

```

---

<div class="post-metadata">

**Author:** ![robsonsobral](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/robsonsobral/32/1628_2.png) [@robsonsobral](https://discourse.gohugo.io/u/robsonsobral)\
**Post date:** [August 31, 2016, 4:30pm UTC](https://discourse.gohugo.io/t/conditionally-checking-for-non-existent-variable/4004/7 "2016-08-31T16:30:43Z")

</div>

Thank you again.

However, your code emulates an `AND`, while I need an `OR`.

I tried to use a… Something like a ternary attribution, but it doesn’t work either.

```auto
{{ $fileName := .File.BaseFileName | default "" }}
{{ if or (.IsHome) (eq $fileName "running") }}

```

---

<div class="post-metadata">

**Author:** ![robsonsobral](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/robsonsobral/32/1628_2.png) [@robsonsobral](https://discourse.gohugo.io/u/robsonsobral)\
**Post date:** [August 31, 2016, 4:42pm UTC](https://discourse.gohugo.io/t/conditionally-checking-for-non-existent-variable/4004/8 "2016-08-31T16:42:15Z")

</div>

Ok! I found a way! If somebody has a better idea, please tell me.

```auto
{{ $fileName := "" }}

{{ if .IsPage }}
    {{ $fileName := .File.BaseFileName }}
{{ end }}

{{ if or (.IsHome) (eq $fileName "running") }}
[...]
{{ end }}

```

I don’t know why, but the attribution has to be this way. An `{{ else }}` gives the error `undefined variable "$fileName"`:

```auto
{{ if .IsPage }}
    {{ $fileName := .File.BaseFileName }}
{{ else }}
    {{ $fileName := "" }}
{{ end }}

```

Thank you all!
