0

I want to be able to use the each helper on this json but I cant get the children arrays to display. I've tried everything but nothing works. I'm new to this but its just frustrating to be stuck on this part

html

<head>
  <title>tally</title>
</head>

<body>
  <h1>Welcome to Meteor!</h1>

  {{> hello}}

    {{#each artist }}
        <h2>{{ name }}</h2>
        <h3>{{ proDate }}</h3>

    <b>Songs</b>: {{#each video}}
                    {{songs}}

                    {{/each}}



    {{/each}}


</body>

javascript

    if (Meteor.isClient) {
    Template.body.helpers({
        artist: [
            {
                "name": "El Alfa",
                "proDate": 2008,
                "albums": 0,
                video: [{
                    "song": "Muevete Jevi",
                    "youtube": "https://www.youtube.com/embed/bnlW-42_Sfs"

                }]
            }]
    });
}

3 Answers 3

0

In meteor helper is associated with a template. In your question there is no template in the html. So, i was a bit confused about that. You can refer to official documentation of meteor. I am also new in meteor technology. So far i used helper in two ways:

For better understanding:

html

<template name="myTemplate">
    {{#each artist}}
        <h3>Name : {{name}}</h3>
        <h4>proDate{{proDate}}</h4>
        <h4>{{albums}}</h4>
        Songs:
        <ul>
            {{#each video}}
                <li>{{song}} - {{youtube}}</li>
            {{/each}}
        </ul>
        <hr>
    {{/each}}
</template>

js (heplers for specific template)

Template.myTemplate.helpers({
    artist : function(){
        return [
        {
            "name": "El Alfa 1",
            "proDate": 2007,
            "albums": 0,
            video: [{
                    "song": "Muevete Jevi 11",
                    "youtube": "https://www.youtube.com/embed/bnlW-42_Sfs/11"
                },
                {
                    "song": "Muevete Jevi 12",
                    "youtube": "https://www.youtube.com/embed/bnlW-42_Sfs/12"

                }
            ]
        },
        {
            "name": "El Alfa 2",
            "proDate": 2008,
            "albums": 1,
            video: [{
                "song": "Muevete Jevi 21",
                "youtube": "https://www.youtube.com/embed/bnlW-42_Sfs/21"

            },
            {
                 "song": "Muevete Jevi 22",
                 "youtube": "https://www.youtube.com/embed/bnlW-42_Sfs/22"

            }]
        }]
     }
});

OR we can write helpers for all the template. i.e.

Template.registerHelper("artist", function () {
    return [...] // same JSON as above
});

Please feel free ask any doubt in comments.

Cheers. :)

Sign up to request clarification or add additional context in comments.

1 Comment

Now my second question is if I wanted to target the first "el alfa 1" and i just wanted to display song #2. how would i do this? {{ artist.video[2] }}
0

For the JSON structure you defined it should look like this:

{{#each artist }}
    <h2>{{name}}</h2>
    <h3>{{proDate}}</h3>

    <b>Songs</b>: 
    {{#each video}}
        <p>
            Song: {{song}}
            Link: {{youtube}}
        </p>
    {{/each}}

{{/each}}

Comments

0

You're calling songs directly and Blaze doesn't know what to do with it. Here's how you would call the songs referenced in your example:

{{#each artist}}
  <h2>{{name}}</h2>
  <h3>{{proDate}}</h3>

  <b>Songs</b>: 
  {{#each video}}
    <p>{{song}}</p>
    <p>{{youtube}}</p>
  {{/each}}
{{/each}}

If you want to use a helper, which it looks like it. You need to reference it with a > symbol.

{{> song}}

There's an awesome concise example on the official Meteor page.

if (Meteor.isClient) {
  // This code only runs on the client
  Template.body.helpers({
    tasks: [
      { text: "This is task 1" },
      { text: "This is task 2" },
      { text: "This is task 3" }
    ]
  });
}

<body>
  <div class="container">
    <header>
      <h1>Todo List</h1>
    </header>

    <ul>
      {{#each tasks}}
        {{> task}}
      {{/each}}
    </ul>
  </div>
</body>

<template name="task">
  <li>{{text}}</li>
</template>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.