Some broad advice needed

OK, I am currently creating the news section on the website and I am trying to do two different things:

  1. Set a primary category for inclusion in the breadcrumbs

  2. Create and show a specific sidemenu based on that primary category. (i.e. if I have categories for different companies, then I want to show links related to that company in a side menu).

Here are my thoughts:

My knowledge of Tags \ Categories is limited (at the moment - still learning)

  1. It is easy to manually set a primary category and primary category url but is there a way to designate a primary category, and to look up whether that is a category and automate the url part.

  2. I am not sure the best way to go about this. Should I use data, or conditional blocks. I am having a massive brain fart on the best way to approach this. I think I just need some ideas here on the best way, so I can then look it up and implement.

OK, this is my proposed solution for 2:

In the single template I have the following:

{{ define "sidemenu" }}
{{ partial "menus/sidebar-menu.html" . }}
{{ if eq .Params.primarycategory "companyname" }}
{{ partial "menus/companymenu.html" . }}
{{ end }}
{{ end }}

And the relevant partial code (obviously finetuning menu names etc):

<h4 id="bottom-nav">Sections menu</h4>
<ul class="vertical menu" data-accordion-menu>
    {{ $currentNode := . }}
    {{ range .Site.Menus.main }}
    {{ if .HasChildren }}
    <li>
        <a href="javascript:;" class=""><span>{{ .Name }}</span></a>
        <ul class="menu vertical nested {{if $currentNode.HasMenuCurrent "main" . }} is-active{{end}}">


        {{ range .Children }}
    <li {{if $currentNode.IsMenuCurrent "main" . }} class="active"{{end}}><a href="{{.URL}}">{{ .Name }}</a>

    </li>
    {{ end }}
</ul>
{{else}}
<li><a href="{{.URL}}"><span>{{ .Name }}</span></a>{{end}}</li>
{{end}}
</ul>
<br>

This way, I can add items to the relevant menu items via the front matter, plus add active styling etc.

It does mean that every time I create a category (that requires a menu) I need to add a file to the partials, and an entry in the single.html template.

Is there a better way?

Edit: I suppose a better way would be to pass the primary category variable into the sections menu partial. Hmmmm. This is getting complex.
Edit2: Looks like printf may help here. Need to learn some things.

Edit3: Looks like I figured out point 2:

{{ $primarycat := $.Params.primarycategory }}
{{ $primarycatt := printf "%q" (print "$.Site.Menus." $primarycat)}}

<h4 id="bottom-nav">{{humanize $primarycat }} News</h4>
<ul class="vertical menu" data-accordion-menu>
    {{ $currentNode := . }}
    {{ range (index $.Site.Menus $primarycat) }}
    {{ if .HasChildren }}
    <li>
        <a href="javascript:;" class=""><span>{{ .Name }}</span></a>
        <ul class="menu vertical nested {{if $currentNode.HasMenuCurrent $primarycatt . }} is-active{{end}}">


            {{ range .Children }}
            <li {{if $currentNode.IsMenuCurrent $primarycatt . }} class="active"{{end}}><a href="{{.URL}}">{{ .Name }}</a>

            </li>
            {{ end }}
        </ul>
        {{else}}
    <li><a href="{{.URL}}"><span>{{ .Name }}</span></a>{{end}}</li>
    {{end}}
</ul>
<br>

Is your goal to creat categories as taxonomies or is “primary category” just a single key-value in front matter. You might not even need to use menus, but I’m not grocking the full scope of what you’re trying to accomplish here…

This thread provides a very interesting dictation if your stream of consciousness, haha. That said, I empathize with the frustrations of getting started, but I think you’ll get more responses with this type of query on the forums if

  1. You pare down the scope of your questions
  2. You provide a link to source to keep your post shorter and give support a better idea of scope

Looks like you’re well on your way to figuring this out :smile:

1 Like

@rdwatters Yeh, I think I have everything covered now.

The intent was to designate one of several categories as the primary category, and use that to initiate other information (such as menus, additional breadcrumb entry) showing.

I pretty much have everything working as intended now (although I have a fair bit of code tidying to do).

My coding knowledge consists of mainly front end, and the Udacity introduction to programming (python) course. So, it is a steep learning curve. Pleased with everything though. Hugo is really easy to use, compared to things like Django. I have quite a large complex website I am trying to migrate across.

1 Like