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

Feature: Iterate over json array with objects #756

Closed
thomasmodeneis opened this issue Dec 30, 2014 · 3 comments
Closed

Feature: Iterate over json array with objects #756

thomasmodeneis opened this issue Dec 30, 2014 · 3 comments
Labels

Comments

@thomasmodeneis
Copy link

Hi,

I'm not sure if this is a feature Hugo does not support, or if I'm doing it wrong. I can't find another way to ask this so I had to open this ticket.

hellohugo.md -->

{
    "title": "Test iterate over array of objects",
    "date": "2014-12-26T21:49:12Z",
    "author": "test",
    "listOfStuff": [
        {
            "test" : "test",
            "anothertest":"anothertest"
        },
        {
            "test" : "test",
            "anothertest":"anothertest"
        },
        {
            "test" : "test",
            "anothertest":"anothertest"
        },
        {
            "nick" : "test",
            "anothertest":"anothertest"
        }
    ]
}
## test

With this HTML it only prints me the key but I can't manage to get inside the json object:

                            <div class="tags">
                                {{ range $key, $value := .Params.listOfStuff }}
                                    <span  class="tag"><a href="{{ $key}}"> {{ $key }} </a> {{ $value}} </span>
                                {{ end }}
                            </div>

output:

<div class="tags">

                                    <span class="tag"><a href="0"> 0 </a>  </span>

                                    <span class="tag"><a href="1"> 1 </a>  </span>

                                    <span class="tag"><a href="2"> 2 </a>  </span>

                                    <span class="tag"><a href="3"> 3 </a>  </span>

                            </div>

If I try to get deeper into the object with {{ $value.test }}

                            <div class="tags">
                                {{ range $key, $value := .Params.listOfStuff }}
                                    <span  class="tag"><a href="{{ $key}}"> {{ $key }} </a> {{ $value.test }} </span>
                                {{ end }}
                            </div>

Then Hugo throws:

ERROR: 2014/12/30 Error while rendering page test/test1.md: template: test/single.html:79:101: executing "test/single.html" at <$value.test>: can't evaluate field test in type string

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: Command failed: 
    at ChildProcess.exithandler (child_process.js:637:15)
    at ChildProcess.EventEmitter.emit (events.js:98:17)
    at maybeClose (child_process.js:743:16)
    at Socket.<anonymous> (child_process.js:956:11)
    at Socket.EventEmitter.emit (events.js:117:20)
    at Pipe.close (net.js:465:12)
@bep
Copy link
Member

bep commented Dec 30, 2014

What you try to do isn't supported by Hugo.

Once #748 gets into play, you can do similar things.

In general, http://discuss.gohugo.io/ is a better place for questions.

@thomasmodeneis
Copy link
Author

Thanks bjornerik.
That was really helpful!

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

No branches or pull requests

2 participants