How to sort on inner field?

#1

This is in somefile.md and many other files.md

---
properties:
  ogc_fid: 4
  name: some_name
  num: 7
---

I want to sort pages by ogc_fid
And this is in layout.

{{ range sort .Data.Pages ".Params.properties.ogc_fid" }}

Sorting on ogc_fid doesn’t work.

Server show an error:

ERROR 2018/12/13 22:09:18 Failed to render pages: render of "section" failed: "d
.....\test\layouts\pass\pass.html:9:11": execute of template fail
ed: template: pass/pass.html:9:11: executing "pass/pass.html" at <sort .Data.Pag
es ".P...>: error calling sort: can't evaluate an invalid value
#2

This should work fine as you have it

{{ range sort .Data.Pages ".Params.properties.ogc_fid" }}

This error is likely because one of your pages doesn’t have a value for ogc_fid

#3

No it is not. Every page has ogc_fid

#4

Would need to see your code to help anymore. Else I’m just shooting in the dark

#5

You were right. Pags without ogc_fid break everything
The workaround is

{{$tmp := where .Pages ".Params.properties" "!=" nil}}
	  {{ range sort $tmp ".Params.properties.ogc_fid"}}
#6

Thanks for helping people!

1 Like
#7

Next time, please follow the advice at Requesting Help and share your project. if you can not, please recreate a project that reproduces your issue that can be easily shared.