hansaplast
hansaplast

Reputation: 11573

assemble/handlebars: define data context for a template

In assemble I want to define one data hierarchy and then work on a subset of the data in one template, how can this be achieved?

Example

stages.yaml

stages:
    stage1:
        goodies:
          - some
          - data
    stage2:
        goodies:
          - more
          - data

and then define the data subset like this:

index.hbs

{{#withStage stage1}}
    {{#each goodies}}
        <p>{{this}}</p>
    {{/each}}
{{/withStage}}

I tried registering the following helper:

helpers.js

Handlebars.registerHelper('withStage', function(context, options){
    return options.fn(this.stages[context]);
});

but although there was no error, no <p> was shown.

For completeness, here are my assemble options:

Gruntfile.js

    assemble: {
        options: {
            layout: "src/layouts/default.hbs",
            flatten: true,
            data: 'src/data/*.yaml',
            helpers: ['./helpers.js'],
        },

Upvotes: 1

Views: 418

Answers (1)

hansaplast
hansaplast

Reputation: 11573

I run into the concept of partials which seem to be the way to solve my 'design issue':

added options.partials to
Gruntfile.js:

    assemble: {
        options: {
            ...
            data: 'src/data/*.yaml',
            partials: ['src/partials/*.hbs' ],
            ...
        },

and and added goodies.hbs into src/partials and put this into
index.hbs:

{{#each stages.stage1}}
  {{> goodies}}
{{/each}}

Still I leave the question open since I want to know what I did wrong with the helper.

Upvotes: 2

Related Questions