Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sort template func fails for Pages #1330

Closed
benzwu opened this issue Aug 6, 2015 · 11 comments
Closed

sort template func fails for Pages #1330

benzwu opened this issue Aug 6, 2015 · 11 comments

Comments

@benzwu
Copy link
Contributor

benzwu commented Aug 6, 2015

sorting list like this:

{{ range sort .Site.Pages "Params.somefield" }}

causing error message:

ERROR: 2015/07/01 Error while rendering homepage: reflect: call of reflect.Value.FieldByName on ptr Value
@bep
Copy link
Member

bep commented Aug 6, 2015

OK, reading the doc, this isn't supported. "Params.somefield" isn't a field on Page. Sorting by nested map keys could maybe be implemented, but that is not on my todo-list.

Can you confirm that constructs like

{{ range sort .Site.Pages "Title" }}

Work as expected?

@bep bep changed the title sort is broken with list sort should work with nested map keys Aug 6, 2015
@benzwu
Copy link
Contributor Author

benzwu commented Aug 6, 2015

confirm, same result

{{ range sort .Site.Pages "Title" }}
    {{ .Title }}
{{ end }}

ERROR: 2015/08/06 Error while rendering homepage: reflect: call of reflect.Value.FieldByName on ptr Value

Hugo Static Site Generator v0.14 BuildDate: 2015-06-17T03:41:12+07:00

@bep bep changed the title sort should work with nested map keys sort template func fails for Pages Aug 6, 2015
@bep bep added the Bug label Aug 6, 2015
@bep
Copy link
Member

bep commented Aug 6, 2015

@derekperkins doesn't the sort func support pointers?

@tatsushid
Copy link
Contributor

@bep it doesn't dereference pointer value.

@tatsushid
Copy link
Contributor

Fixed in my local repository but I reconsider it's really enough to solve this

@derekperkins
Copy link
Contributor

@bep I would have thought so, but I guess not. :) I haven't touched Hugo in 7-8 months, so I don't really remember. Should be easy enough to add an Indirect to support them.

@tatsushid Go ahead and submit your PR with the fix.

tatsushid added a commit to tatsushid/hugo that referenced this issue Aug 7, 2015
'sort' template function used to accept only each element's struct field
name, method name and map key name as its second argument. This extends
it to accept a field/method/key chaining key string like
'Params.foo.bar' as the argument. It evaluates sub elements of each
array or map elements and sorts by them.

Typical use case would be sorting pages by user defined front matter
value. For example, sorting pages by 'Params.foo.bar' is possible by
writing the following template code

    {{ range sort .Data.Pages "Params.foo.bar" }}
        {{ .Content }}
    {{ end }}

It ignores all leading and trailing dots so "Params.foo.bar" can be
written in ".Params.foo.bar"

This also fixes the issue that 'sort' cannot evaluate a pointer value.

Fix gohugoio#1330
@tatsushid
Copy link
Contributor

I sent a PR. Please see #1334

@AmrAbdulrahman
Copy link

Um trying to make a sort on user defined property as following:

in my pages:

+++
title= "bla bla bla"
parent = "parent"
index = 0 # each page is assigned a unique index
+++

and i try to apply sorting as following:

{{ range sort .Site.Pages ".Params.index" }}
    <a href="{{.RelPermalink}}">
          {{.Title}} {{.Params.index}}
    </a>
{{end}}

this doesn't fails at compilation, but the list is rendered empty.
I'm using hugo#0.15 which contains this PR merged: #1334

Am i missing something?

@AmrAbdulrahman
Copy link

Well, i was walking in the wrong direction.
I discovered that there's a default parameter is there just to be used for sorting, "weight"

So, the fix is:

use "weight" instead of "index"

and use weight

{{ range sort .Site.Pages ".Weight" }}

this fixed my issue, but there's still a problem using user defined properties for sorting.

@bep
Copy link
Member

bep commented Feb 14, 2016

This should be the same:

{{ range sort .Site.Pages.Sort }}

Sorts by Weigt (and then by date and then by title).

tychoish pushed a commit to tychoish/hugo that referenced this issue Aug 13, 2017
'sort' template function used to accept only each element's struct field
name, method name and map key name as its second argument. This extends
it to accept a field/method/key chaining key string like
'Params.foo.bar' as the argument. It evaluates sub elements of each
array or map elements and sorts by them.

Typical use case would be sorting pages by user defined front matter
value. For example, sorting pages by 'Params.foo.bar' is possible by
writing the following template code

    {{ range sort .Data.Pages "Params.foo.bar" }}
        {{ .Content }}
    {{ end }}

It ignores all leading and trailing dots so "Params.foo.bar" can be
written in ".Params.foo.bar"

This also fixes the issue that 'sort' cannot evaluate a pointer value.

Fix gohugoio#1330
@github-actions
Copy link

github-actions bot commented Apr 9, 2022

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 9, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants