Giter Site home page Giter Site logo

Ordering menu about hugo-theme-learn HOT 12 CLOSED

matcornic avatar matcornic commented on August 25, 2024 2
Ordering menu

from hugo-theme-learn.

Comments (12)

Xipas avatar Xipas commented on August 25, 2024 4

Hi, i had the same problem, and ended up changing the menu creation to .Site.Menus
Hugo Docs

it changes a bit on the page, but i like it better this way.

Menu changed to this (line 16)

  <ul class="topics">
          {{ $page := . }}  
      {{ range .Site.Menus.main }}
      <li class="dd-item  {{ if eq $page.RelPermalink .URL }}active{{ end }} {{if in $page.RelPermalink .URL }}parent{{ end }}"    
      data-nav-id="{{.URL}}">
        <a href="{{.URL}}">
          <span>
             {{ .Pre  }}
            {{ .Name }} 
              {{ if .Site.Params.showVisitedLinks}}  <i class="fa fa-check read-icon"> {{ end }} </i>
           </span>
        </a>
        {{ if .HasChildren }} 
        <ul>
          {{ range .Children}}
          <li class="dd-item {{ if eq $page.RelPermalink .URL }}active{{ end }}"  data-nav-id="{{.URL}}">
            <a href="{{.URL}}">
              <span>{{ .Name  }}    {{ if .Site.Params.showVisitedLinks}} <i class="fa fa-check read-icon"></i> {{ end }} </span>
            </a>
          </li>
          {{ end }}
        </ul> 
        {{ end }} 
      </li> 
      {{ end }}
    </ul>

since i do not like having the menu defined on config.toml
my content page header changed to this

--- 
title : "Title to show"
next: /sobre/intro
chapter: true 
menu:
  main:
    identifier : "Unique_ID_OF_PAGE"
    weight: 5
    name : "Title to show"
    pre : '<b>1.</b>'
---

and the child's (we can have unlimited levels, using the identifier and the parent, however the menu design only show 2 levels for now)

--- 
title: What is this Hugo theme ?
prev: /sobre
next: /sobre/requesitos 
menu:
  main:
    identifier : "Unique_ID_OF_PAGE"
    parent: 'Unique_ID_OF_PARENT'
    weight: 5
    name : "What is this Hugo theme"
---

and a bug fix on header.html (on the breadcrumb creator) line 51

    {{ $type := .Params.menu.main.parent }}
                {{ $relLink := .RelPermalink }}
                {{ range $name , $value := .Site.Menus.main }}

                 {{ if eq $value.Identifier $type }} 
                    {{ if ne $relLink $value.URL }}
                <a href="{{  $value.URL }}" itemprop="url"><span itemprop="title"> <b>{{  $value.Name }}</b></span></a> <i class="fa fa-angle-right"></i>
                    {{ end }}
                  {{ end }}
                {{ end }}

and now i have the same menu, and the weight working.

I still working to reduce the duplication on top of content pages, since we have Titleand menu name with the same values.

i think i just need replace all uses of .Title with the menu value .Params.menu.main.name, however havent done that yet, so now sure about that.

from hugo-theme-learn.

TimNN avatar TimNN commented on August 25, 2024 1

Yeah, that was why I was trying something like First.Weight (so order the sections by the weight of the first page in the section).

I don't know where I got the First from - I believe I found it in some go documentation but I can't seem to find it again.

from hugo-theme-learn.

matcornic avatar matcornic commented on August 25, 2024 1

@TimNN @eddie Yes, it's a map of list of pages, you can't sort it like a list (https://gohugo.io/templates/list/). The key is the folder name, the value is the list of the pages inside the folder.

My thought was to create a menu automatically from the folder structure of the content, instead of using the Hugo menu functionality (in order to avoid boilerplate in config.toml). As Hugo does not provide (yet) a functionality like this, I based everything on the .Site.Sections.

Based on that principle, and my little knowledge about Go Templates, I can't define right know a nice and clean solution (but I'm sure there is one 😄 ).

from hugo-theme-learn.

matcornic avatar matcornic commented on August 25, 2024

Hello Xaf,

I'll look into that. Since you used weigth and it does not work, it may be a bug.
Meanwhile, one simple solution is prefixing your folders with a number. Then, if you don't want the numbers to be in the path, create aliases.

Note: weight sure works for subpages, as you can see in the demo (repo). I'll check for chapter pages.

from hugo-theme-learn.

TimNN avatar TimNN commented on August 25, 2024

I'm also encountering this issue (weight does not work with chapters).

from hugo-theme-learn.

TimNN avatar TimNN commented on August 25, 2024

It looks like the problem is that .Site.Sections is an map of pageName to WeightedPages objects and not a WeightedPages object.

from hugo-theme-learn.

TimNN avatar TimNN commented on August 25, 2024

I tried to use {{ range $key , $value := sort .Site.Sections "First.Weight" }} in menu.html which I believe should work, but fails because "error calling sort: First is neither a struct field, a method nor a map element of type hugolib.WeightedPages".

from hugo-theme-learn.

eddie avatar eddie commented on August 25, 2024

@TimNN The sections are just directories right? So they won't have the weight attribute.

from hugo-theme-learn.

eddie avatar eddie commented on August 25, 2024

@TimNN Printing out the sections map gives me this

map[faq:[{0 }] changelog:[{0 }] intro:[{5 }]]

Not sure how to apply sort with this however :)

from hugo-theme-learn.

Xipas avatar Xipas commented on August 25, 2024

I created a pr #5 with this changes and keep compatibility to folder way, and with 3rd level menu, however, still have a decision to make if we wanna use go this way.

from hugo-theme-learn.

tandibar avatar tandibar commented on August 25, 2024

I think it would be much nicer to go with the default hugo way. Would love to see this PR merged.

from hugo-theme-learn.

kamilchm avatar kamilchm commented on August 25, 2024

I posted an alternative solution in #8

from hugo-theme-learn.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.