1

Relatively new to handlebars.js, but not templating. I am using two loops to output markup in specific order. Both collections.intro and collections.elements are created by https://github.com/segmentio/metalsmith-collections.

Javascript

var order = ['intro', 'elements'];

collections.intro = [
 { title: 'One' },
 { title: 'Two' }
];

collections.elements = [
 { title: 'One' },
 { title: 'Two' }
];

Handlebars

{{#each order}}

  {{this}} /* intro, elements */

  {{#each ../collections.[this]}}
    {{this.title}}
  {{/each}}

{{/each}}

Is there anyway to use this from the order loop to access the correct collections. Both collections[intro] and collections[elements] work when hard coded.

1 Answer 1

1

Managed to resolve this with a custom helper:

Javascript

Handlebars.registerHelper( 'collection', function ( slug, options ) {
  return options.data.root.collections[slug];
});

Handlebars

{{#each order}}

  {{#each (collection this)}}
    {{this.title}}
  {{/each}}

{{/each}}
Sign up to request clarification or add additional context in comments.

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.