wbond / pybars3

Handlebars.js template support for Python 3 and 2
GNU Lesser General Public License v3.0
179 stars 46 forks source link

`this` array functionalities not working within `#each` helper #65

Open omnilinguist opened 5 years ago

omnilinguist commented 5 years ago

Perhaps a unit test is worth a thousand words here. This is the test I expected to pass, which works perfectly fine in JS Handlebars:

    def test_each_this_array(self):
        template = u"{{#each name}}{{this.[0]}} {{this.[1]}} {{/each}}"
        context = {
            'name': [
                ['John', 'Smith'],
                ['James', 'Joyce']
            ]
        }
        result = u"John Smith James Joyce "

        self.assertRender(template, context, result)

But instead, it gets rendered as such:

tests/test_acceptance.py:51: in assertRender
    self.assertEqual(result, render(template, context, helpers=helpers, partials=partials, **kwargs))
E   AssertionError: 'John Smith James Joyce ' != '    '
E   - John Smith James Joyce 
E   +

This is interesting because:

But for some reason the engine is having trouble combining these components together to successfully render the above test.

Because of this, I'm having to do a workaround of having the iterator be a dictionary and entering field names to make it work, eg.

    def test_each_this_array(self):
        template = u"{{#each name}}{{this.first_name}} {{this.last_name}} {{/each}}"
        context = {
            'name': [
                {'first_name': 'John', 'last_name': 'Smith'},
                {'first_name': 'James', 'last_name': 'Joyce'}
            ]
        }
        result = u"John Smith James Joyce "

        self.assertRender(template, context, result)

which is similar to the behaviour of the test_list_context unit test. Any ideas?