Skip to content

this array functionalities not working within #each helper #65

@omnilinguist

Description

@omnilinguist

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:

  • in several other unit tests (eg. test_each, test_each_this, test_context_with_attrs) it seems to successfully test other complex functionalities of #each, where the iterator behaves as an dictionary;
  • test_GH_158__Using_array_index_twice_breaks_the_template successfully tests the raw arr.[0] functionality of arrays;
  • in fact, even {{this}} inside the block will render the stringified sublist, eg. ['John', 'Smith'].

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions