gohugoio / hugo

The world’s fastest framework for building websites.
https://gohugo.io
Apache License 2.0
75.81k stars 7.53k forks source link

Order by type/section? #412

Closed unor closed 10 years ago

unor commented 10 years ago

Is it possible to order lists by type/section?

On http://hugo.spf13.com/templates/list it gives examples for

Are these currently the only fields you can use for ordering?

(I tried ByType and BySection, but it didn’t work.)

spf13 commented 10 years ago

How would ByType or BySection work? Would they group the posts?

unor commented 10 years ago

I assumed in the same way as ByTitle: alphabetically. It’s not really important, though.

Grouping (with the possibility of adding subheadings) would of course be even better; that would also be nice for ordering by date (so you could group by year, for example). I don’t know Go/template well, so I’m not sure if this isn’t possible even now.

tatsushid commented 10 years ago

The same as @unor, I'm thinking about grouping like jekyll's 'Group By' described in http://jekyllrb.com/docs/templates/

To implement such a grouping functionality, I think something 'Group' structure which has a key and its array is needed to be able to be ordered by the key because golang map isn't ordered by its key.

On date grouping, hugo node and page structures have its date as golang time so to order by year, month etc, I think it should be converted by something date format function before sorted

How about these template function calls below?

Simple grouping

{{group_by .Data.Pages 'Section' 'asc'}}

returns

interface{}[
    struct Group {
        Key: 'book',
        Data: interface{}[
            book section pages
        ]
    },
    struct Group {
        Key: 'post',
        Data: interface{}[
            post section pages
        ]
    },
    ...
]

Date grouping

{{group_by_date .Data.Pages '2006' 'desc'}}

returns ('2006' is the format string to get year part of golang time)

interface{}[
    struct Group {
        Key: "2014",
        Data: interface{}[
            pages written in 2014
        ]
    },
    struct Group {
        Key: "2013",
        Data: interface{}[
            pages written in 2013
        ]
    },
    ...
]

If those look good, I'll try to implement

spf13 commented 10 years ago

That looks really good to me. Looking forward to using it. Thanks.

spf13 commented 10 years ago

I would do things slightly differently for the types. I would make the base type a "PageGroup", the containing slice a slice of PageGroup, not interface{} and the data a slice of Pages. It's going to be a lot easier that way.

Does that make sense?

For me I can't think of using this for anything except pages, so I would avoid the extra complexity and overhead that comes from using an empty interface.

natefinch commented 10 years ago

Note that maps in templates are actually iterated over in order sorted by key if the key is a simple type (string, int etc).

Check out the actions section here: http://golang.org/pkg/text/template/ under range in the block comment. On Aug 19, 2014 8:37 AM, "Steve Francia" notifications@github.com wrote:

I would do things slightly differently for the types. I would make the base type a "PageGroup", the containing slice a slice of PageGroup, not interface{} and the data a slice of Pages. It's going to be a lot easier that way.

Does that make sense?

For me I can't think of using this for anything except pages, so I would avoid the extra complexity and overhead that comes from using an empty interface.

— Reply to this email directly or view it on GitHub https://github.com/spf13/hugo/issues/412#issuecomment-52626739.

tatsushid commented 10 years ago

Thanks @spf13 and @natefinch. I read the document and also read the golang text/template implementation in text/template/exec.go. In walkRange and sortKeys funcs, all map keys and elements are sorted as the document says

Which is good, using map for grouping and implementing reverse order template function (now doesn't exist such a common function, only Pages structure method is available) or using structure for grouping and returning sorted structure array?

tatsushid commented 10 years ago

Ah, sorry, using reverse template function with range like

{{ range k, v := (reverse .Pages) }}

doesn't make sense. Only useful when reverse returns array but in this case, the group key is inaccessible

github-actions[bot] commented 2 years ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.