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

Allow dict to create nested structures #6497

Closed
bep opened this issue Nov 11, 2019 · 13 comments · Fixed by #6500
Closed

Allow dict to create nested structures #6497

bep opened this issue Nov 11, 2019 · 13 comments · Fixed by #6500
Assignees
Milestone

Comments

@bep
Copy link
Member

bep commented Nov 11, 2019

I'm working on documentation for the revised "markdown etc." and thought I would save some time by code generation. Which would be exellent had it not been for Hugo's limited map support.

What I would want is something ala:

{{ $configSection := index $.Site.Data.docs.config $sections }}
{{ $config := dict $sections $configSection }}

The $sections above is a string slice and would make sure that I could present the docs on the correct level in a "copy pastable" friendly version.

@bep bep added this to the v0.60 milestone Nov 11, 2019
@bep bep self-assigned this Nov 11, 2019
@bep
Copy link
Member Author

bep commented Nov 11, 2019

/cc @regisphilibert

@regisphilibert
Copy link
Member

The $sections above is a string slice

Not sure to understand but it seems we're using parsed data as key. Could we also do:

{{ $config := dict "greeting.french" "bonjour" }}

@bep
Copy link
Member Author

bep commented Nov 11, 2019

Your construct is very ambiguous. Assuming that "." is always used as a separator will not work.

I may have another idea, though. Will be back in a minute ...

@bep
Copy link
Member Author

bep commented Nov 11, 2019

If we allow a map to be passed in as the (optional) first argument, we could do something ala:

{{ $config := dict "config" dict }}
{{ $config = $config | dict "markup" dict }}

@regisphilibert
Copy link
Member

Not sure to follow all those dicts... What would be the data structure of$config in the end? Also pipe argument order always confuses me, but wouldn't$config end up being the last argument of your piped dict on the second line, or am I unpiping it wrong?

{{ $config := dict "config" dict }}
{{ $config = dict "markup" dict $config }}

@bep
Copy link
Member Author

bep commented Nov 11, 2019

My example was slightly wrong:

{{  $config := dict }}
{{ $root := dict "config" $config }}
{{ $config = $config | dict "markup" dict }}

Which I guess I with the current Hugo could write as:

{{  $config := dict "markup" dict }}
{{ $root := dict "config" $config }}

But that is not possible to do in a loop.

I guess it boils down to this line:

{{ $config = $config | dict "markup" "value" }}

Which, when thinking about it, could be rewritten as:

{{ $config = $config | merge (dict "markup" "value" )}}

OK, I'll try to implement the docs changes with the above and see if that works.

@bep
Copy link
Member Author

bep commented Nov 11, 2019

OK; so I managed to get my code toggle shortcode change working with this construct:

DELETED

Not particularly simple ... I suspect that my initial suggestion (first comment) was the most sensible one, but I will let this one linger ....

@regisphilibert
Copy link
Member

I use merge a lot for building dynamic maps within loops, but I found it takes a toll on build time. Scratch.SetInMap is way faster.

As an aside: If you're interested in improving overall Hugo handling of data, I'd love to dig up this old thing: #5604

@bep
Copy link
Member Author

bep commented Nov 11, 2019

If you're interested in improving overall Hugo handling of data

Interested maybe, time, no.

@bep bep modified the milestones: v0.60, v0.61 Nov 11, 2019
@bep
Copy link
Member Author

bep commented Nov 11, 2019

So, I deleted my "suggested approach" above -- I just couldn't get it right. I'm a little bit tired, but it shows that it's much harder than it should.

I have a patch ready that allows string slices as keys in dict, which makes the above construct much simpler:

{{ $sections := (split . ".") }}
{{ $configSection := index $.Site.Data.docs.config $sections }}
{{ $code = dict $sections $configSection  }}

@regisphilibert
Copy link
Member

I'm guessing here because not sure what the value of . or $.Site.Data.docs.config is, but first arg is a slice of strings representing keys and second is a slice of whatever representing values.

Pending they both have an equal numbers of item, key/value pairs are created by "paralelling" the two slices?

@regisphilibert
Copy link
Member

regisphilibert commented Nov 11, 2019

Scratch the above, I understood by reading #6500

bep added a commit to bep/hugo that referenced this issue Nov 11, 2019
@bep bep closed this as completed in #6500 Nov 11, 2019
@github-actions
Copy link

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 Feb 12, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants