Search code examples
javascriptarrayshandlebars.jspartial

grunt-assemble with handlebars: how to get an array into a partial?


i want to generate with grunt-assemble static sites, everything works fine but i have a problem to get an array into a partial

this is my partial(image-slider.hbs) which should recive the array

<div class="swiper-container {{className}}">
    <div class="swiper-wrapper">
        {{#each images}}
            <img  class="swiper-slide" src="{{ ../path }}/{{image}}" alt="">
        {{/each}}
    </div>
</div>

and from this partial(office-slider.hbs) i want to send the data

... 
<h1>images from the office</h1>
{{> image-slider 
    className='office-slider' 
    path='..img' 
    images=[
        "image": "t_1.jpg",
        "image": "t_2.jpg",
        "image": "t_3.jpg",
        "image": "t_1.jpg",
        "image": "t_2.jpg",
        "image": "t_3.jpg",
    ]
}}
...

office-slider.hbs is included office.hbs

...
{{>office-slider}}
...

"className" and "path" works fine, but if i try to put an array as a data i get only an error

Warning: Unexpected token ILLEGAL Use --force to continue. Aborted due to warnings.

what im doing wrong?

gregor


Solution

  • On advice from @dandavis i seperated my data out of the template and this is how it works

    my gruntfile

    assemble: {
        options: {
            partials: ['partials/**/*.hbs'],
            layoutdir: 'layouts',
            layout: ['default.hbs'],
            flatten: true,
            data: ['pages/**/*.json'],  <--load all jsons (contains the data)
            helpers: ['js/helper-*.js']
        },
        site: {
            src: ['pages/**/*.hbs'],
            dest: 'build/pages/'
        }
    }
    

    create home.json (in my case it's located in the directory "pages/home/" next to home.hbs)

    {
        "title": "pagetitle",
        "gallery1": ["item1.jpg", "item2.jpg","item3.jpg"] 
    }
    

    inside my home.hbs i load the partial with the data from home.json

    {{home.title}}
    {{>image_bar data=home.gallery1}} 
    

    and now i have full access to the data in my partial (image_bar.hbs)

    <ul>
        {{#each data}} 
            <li><img  src="../img/{{this}}"></li > 
        {{/each}} 
    </ul>