简体   繁体   中英

Match field with id and insert data - meteor

I have a collection with the following data.

{
    containerId: "content-left",
    content: "<p>Some text for the left side</p>"
},
{
    containerId: "content-right",
    content: "<p>Some text for the right side</p>"
}

I want to loop through every document and take the containerId then match it up with an id and insert only the content from the containerId that matched the id.

// TEMPLATE

<template name="mainLeft">

    <section id="content-left" class="content">
        {{#each copy}}
            {{{content}}}
        {{/each}}
    </section>

</template>

<template name="mainRight">

    <section id="content-right" class="content">
        {{#each copy}}
            {{{content}}}
        {{/each}}
    </section>

</template>

At the moment I'm just getting all the data from the collection and inserting it in every section so it looks like this

<section id="content-left" class="content">
    <p>Some text for the left side</p>
    <p>Some text for the right side</p>
</section>

<section id="content-right" class="content">
    <p>Some text for the left side</p>
    <p>Some text for the right side</p>
</section>

// HELPER

  UI.registerHelper('copy', function() {
    return ContentCollection.find();
  });

So it should end up looking like this

<section id="content-left" class="content">
    <p>Some text for the left side</p>
</section>

<section id="content-right" class="content">
    <p>Some text for the right side</p>
</section>

You should register two seperate template helpers instead.

UI.registerHelper('copyLeft', function() {
    return ContentCollection.find({containerId:'content-left'});
});

UI.registerHelper('copyRight', function() {
    return ContentCollection.find({containerId:'content-right'});
});

Then you can replace the copy in the template with copyLeft and copyRight

<section id="content-left" class="content">
    {{#each copyLeft}}
        {{{content}}}
    {{/each}}
</section>

<section id="content-right" class="content">
    {{#each copyRight}}
        {{{content}}}
    {{/each}}
</section>

If you want more flexibility you can also pass arguments to the helper

UI.registerHelper('copy', function(container) {
    return ContentCollection.find({containerId:container});
});

Then you can use it like this:

<section id="content-left" class="content">
    {{#each copy 'content-left'}}
        {{{content}}}
    {{/each}}
</section>

<section id="content-right" class="content">
    {{#each copy 'content-right'}}
        {{{content}}}
    {{/each}}
</section>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM