Search code examples
javascripthttphandlebars.js

Handlebar #each with some other attributes


I have a special problem with Handlebar's #each loop.

I have a hbs file, called: list.hbs. In the inside, looks like this:

{{#each subjects}}
    <tr>
        <td>{{subject_name}}</td>
        <td>{{subject_code}}</td>
        <td>{{subject_size}}</td>
        <td>{{subject_location}}</td>
        <td>{{subject_teacher}}</td>
        <td>
        {{testString}}
        </td>
    </tr>
    {{/each}}
{{testString}}

when i reach my endpoint, i use this line in my javascript file:

 res.render('subjects/list', {
                subjects: __subjects ,
                testString: "some text for example",
            });   

The problem is this: when the Handlebar is doing the for_each function, it ignoring the testString attribute. When it's done, and quit from each, it can print in the HTML the test string. I know i could write this testString in my subject object, but this is just a representation of my problem, i have more complicated structure than this. Some idea, please?


Solution

  • I got help finally, the answer is this:

        {{#each subjects}}
        <tr>
            <td>{{subject_name}}</td>
            <td>{{subject_code}}</td>
            <td>{{subject_size}}</td>
            <td>{{subject_location}}</td>
            <td>{{subject_teacher}}</td>
            <td>
            {{../testString}}
            </td>
        </tr>
        {{/each}}
    {{testString}}