Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

this array functionalities not working within #each helper #65

Open
omnilinguist opened this issue Oct 8, 2019 · 0 comments
Open

this array functionalities not working within #each helper #65

omnilinguist opened this issue Oct 8, 2019 · 0 comments

Comments

@omnilinguist
Copy link

omnilinguist commented Oct 8, 2019

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant