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 · 10 comments

Comments

Projects
None yet
5 participants
@benzwu
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

This comment has been minimized.

Show comment
Hide comment
@bep

bep Aug 6, 2015

Member

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?

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 from sort is broken with list to sort should work with nested map keys Aug 6, 2015

@benzwu

This comment has been minimized.

Show comment
Hide comment
@benzwu

benzwu Aug 6, 2015

Contributor

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
Contributor

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 from sort should work with nested map keys to sort template func fails for Pages Aug 6, 2015

@bep bep added the Bug label Aug 6, 2015

@bep

This comment has been minimized.

Show comment
Hide comment
@bep

bep Aug 6, 2015

Member

@derekperkins doesn't the sort func support pointers?

Member

bep commented Aug 6, 2015

@derekperkins doesn't the sort func support pointers?

@tatsushid

This comment has been minimized.

Show comment
Hide comment
@tatsushid

tatsushid Aug 6, 2015

Contributor

@bep it doesn't dereference pointer value.

Contributor

tatsushid commented Aug 6, 2015

@bep it doesn't dereference pointer value.

@tatsushid

This comment has been minimized.

Show comment
Hide comment
@tatsushid

tatsushid Aug 6, 2015

Contributor

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

Contributor

tatsushid commented Aug 6, 2015

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

@derekperkins

This comment has been minimized.

Show comment
Hide comment
@derekperkins

derekperkins Aug 6, 2015

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.

Contributor

derekperkins commented Aug 6, 2015

@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

Make sort tpl func accept field/key chaining arg
'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 #1330
@tatsushid

This comment has been minimized.

Show comment
Hide comment
@tatsushid

tatsushid Aug 7, 2015

Contributor

I sent a PR. Please see #1334

Contributor

tatsushid commented Aug 7, 2015

I sent a PR. Please see #1334

@AmrAbdulrahman

This comment has been minimized.

Show comment
Hide comment
@AmrAbdulrahman

AmrAbdulrahman Feb 11, 2016

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 commented Feb 11, 2016

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

This comment has been minimized.

Show comment
Hide comment
@AmrAbdulrahman

AmrAbdulrahman Feb 14, 2016

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.

AmrAbdulrahman commented Feb 14, 2016

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

This comment has been minimized.

Show comment
Hide comment
@bep

bep Feb 14, 2016

Member

This should be the same:

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

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

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 added a commit to tychoish/hugo that referenced this issue Aug 13, 2017

Make sort tpl func accept field/key chaining arg
'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 #1330
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment