Hugo: Add "dotted" map access as second try in the index func

Created on 6 Apr 2017  路  11Comments  路  Source: gohugoio/hugo

My proposal to fix the index func in the Go std lib was rejected, so we are stuck with maintaining a patched version.

I'm working on some Hugo docs, and have a shortcode that creates HTML tables from a JSON data file:

{{< datatable "media" "types" "Type" "Suffix" >}}

I could probably have done the splitting in the template, but right now I have accepted that I have two levels, and my data list is available at .Site.Data.media.types. This logic would break if it suddenly was .Site.Data.hugo.media.types.

It would be nice if I could do:

{{ $mylist := index .Site.Data "media.types" }}

I.e. if the index func

  1. First did a map lookup with "media.types"
  2. If none found and the string contains a ".", split it and do a nested lookup

/cc @moorereason

Enhancement Keep

All 11 comments

@bep I'd like to work on this, and I've got a decent amount of Go experience but I'm not entirely familiar with the codebase. Where should I start with this?

@bep #3974
reminder pull request

This issue has been automatically marked as stale because it has not had recent activity. The resources of the Hugo team are limited, and so we are asking for your help.
If this is a bug and you can still reproduce this error on the master branch, please reply with all of the information you have about it in order to keep the issue open.
If this is a feature request, and you feel that it is still relevant and valuable, please tell us why.
This issue will automatically be closed in the near future if no further activity occurs. Thank you for all your contributions.

I'd like to work on this one, is it still open for a contribution? I have noticed that there was a PR already, but it is not clear to me why it wasn't merged.

@mknycha looks like I never signed the Contributor License Agreement, oops. Not sure if there was any other reason

@mknycha I'm sorry that I missed out on this, but where is this PR? I tried to search for it now.

OK, looking at this issue now, and the test cases in the PR from @blackmichael, I realize I didn't fully understand the API of the index func when I created this issue:

See the test cases in #6473 -- which passes with the current implementation.

The thing I've missed was that the index func takes multiple indices (which can be a combination of map an slice indices). So in my example above you can do:

{{ $mylist := index .Site.Data "media" "types" }}

Or even:

{{ $mylist := index .Site.Data "media" "types" 0 }}

/cc @blackmichael @regisphilibert @moorereason with the above I'm hesitant to introduce a new "dotted syntax" which would be potentially ambiguous.

Damn, always a discovery...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

digitalcraftsman picture digitalcraftsman  路  3Comments

sigma picture sigma  路  3Comments

moorereason picture moorereason  路  3Comments

geddski picture geddski  路  3Comments

nikolas picture nikolas  路  3Comments