# Hugo Variable Help -- too verbose? \[Solved\]

**URL:** <https://discourse.gohugo.io/t/hugo-variable-help-too-verbose-solved/11734>\
**Category:** support\
**Created:** [April 27, 2018, 3:12am UTC](https://discourse.gohugo.io/t/hugo-variable-help-too-verbose-solved/11734 "2018-04-27T03:12:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jswtch](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jswtch/32/8984_2.png) [@jswtch](https://discourse.gohugo.io/u/jswtch)\
**Post date:** [April 27, 2018, 3:12am UTC](https://discourse.gohugo.io/t/hugo-variable-help-too-verbose-solved/11734/1 "2018-04-27T03:12:28Z")

</div>

Hello,

I am new to templating with Hugo and would love some input.  
I wrote this logic here to display page names in a nav, adding a ‘strong’ tag to the active page, and changing index name to “Home” for users. Was there a cleaner way to go about this? \* I know the titleInRange is unnecessary - that was just for me to remember scope. I mean was there a way to avoid the nested ‘if’?

Also can someone help me with getting the “Home” nav link to show up first? It has a weight of zero, but it appears last. Also anyone have advice for complex whole page templating? Just practice?

```auto

{{ $title := .Title }}
{{ range .Site.Pages.ByWeight }}
  {{ $titleInRange := .Title }}
  {{ if eq $titleInRange .Site.Title }}

    {{ $titleInRange := "Home" }}
    {{ if eq .Title $title }}
      <a href="{{ .URL }}"><strong>{{ $titleInRange }}</strong></a>
    {{ else }}
      <a href="{{ .URL }}">{{ $titleInRange }}</a>
    {{ end }}
  {{ else }}  
    {{ if eq .Title $title }}
      <a href="{{ .URL }}"><strong>{{ $titleInRange }}</strong></a>
    {{ else }}
     <a href="{{ .URL }}">{{ $titleInRange }}</a>
    {{ end }}

  {{ end }}
{{ end }}

```

---

<div class="post-metadata">

**Author:** ![parsiya](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/parsiya/32/1218_2.png) [@parsiya](https://discourse.gohugo.io/u/parsiya)\
**Post date:** [April 27, 2018, 4:02am UTC](https://discourse.gohugo.io/t/hugo-variable-help-too-verbose-solved/11734/2 "2018-04-27T04:02:24Z")

</div>

Instead of `{{ if eq $titleInRange .Site.Title }}` you can use `.isHome`: [https://gohugo.io/variables/page/#page-variables](https://gohugo.io/variables/page/#page-variables)

If you want to give a title to your index, you can create an `_index.md` besides `index.html` and add frontmatter to it like `title` or anything else, I am not sure how it can replace the value of `.Title`.

---

<div class="post-metadata">

**Author:** ![jswtch](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jswtch/32/8984_2.png) [@jswtch](https://discourse.gohugo.io/u/jswtch)\
**Post date:** [April 27, 2018, 6:26am UTC](https://discourse.gohugo.io/t/hugo-variable-help-too-verbose-solved/11734/3 "2018-04-27T06:26:20Z")

</div>

Thanks for the response. I created the \_index.md in the content directory & it worked great.

---

<div class="post-metadata">

**Author:** ![cmal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/cmal/32/5324_2.png) [@cmal](https://discourse.gohugo.io/u/cmal)\
**Post date:** [April 27, 2018, 10:24am UTC](https://discourse.gohugo.io/t/hugo-variable-help-too-verbose-solved/11734/4 "2018-04-27T10:24:52Z")

</div>

Hi, i think you may be trying to build a menu, for which hugo has a [dedicated system](https://gohugo.io/content-management/menus/) that will give you access to the `IsMenuCurrent` function in the [menu templates](https://gohugo.io/templates/menu-templates/) to check whether the menu entry is your current page or not.

Moreover, if i’m reading correctly, your first `if` is only used to change the value of `$titleInRange`. So maybe you could reduce your code by using the [cond](https://gohugo.io/functions/cond/) function:

```auto
{{ $titleInRange := cond .IsHome "Home" .Title }}

```

So now you’re down to just one if. Hope that helps 🙂

---

<div class="post-metadata">

**Author:** ![regis](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/regis/32/4958_2.png) [@regis](https://discourse.gohugo.io/u/regis)\
**Post date:** [April 27, 2018, 11:08am UTC](https://discourse.gohugo.io/t/hugo-variable-help-too-verbose-solved/11734/5 "2018-04-27T11:08:40Z")

</div>

> [@jswtch](#):
>
> {{ $title := .Title }}

You don’t need to store the parent page’s title in a variable, it will always be available in the `$` from within `range` or `with` so:

```auto
{{ if eq .Title $.Title }}

```

Also, you should checkout [.Scratch](https://gohugo.io/functions/scratch), using it I was able to come up with the following.  
You should try and improve it. Have fun discovering Hugo!

```auto
{{ range .Site.Pages.ByWeight }}
  {{ .Scratch.Set "title" (cond .IsHome "Home" .Title) }}
  {{ if or (and .IsHome $.IsHome) (eq .Title $.Title) }}
    {{ .Scratch.Set "title" (printf "<strong>%s</strong>" (.Scratch.Get "title") ) }}
  {{ end }}
  <a href="{{ .URL }}">{{ .Scratch.Get "title" | safeHTML}}</a>
{{ end }}

```

---

<div class="post-metadata">

**Author:** ![jswtch](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jswtch/32/8984_2.png) [@jswtch](https://discourse.gohugo.io/u/jswtch)\
**Post date:** [April 28, 2018, 11:17pm UTC](https://discourse.gohugo.io/t/hugo-variable-help-too-verbose-solved/11734/6 "2018-04-28T23:17:17Z")

</div>

Thanks everyone. I am marking this as resolved.
