noahmorrison / chevron

A Python implementation of mustache
MIT License
486 stars 52 forks source link

Looping through nested array #39

Closed dvdvnl closed 5 years ago

dvdvnl commented 5 years ago

Hi,

I encountered a bug in chevron (Python 3.7.0, chevron 0.12.2) where looping through nested arrays leads to an erroneous output. It appears as if the closing tag ({{/.}}) of an inner loop gets mixed up with the closing tag of an outer loop.

Running the code

import chevron

with open('test.txt', 'w') as file:
    with open('test.mustache', 'r') as template:
        file.write(chevron.render(template, {
            'loop': [
                [
                    [
                        'Lorem',
                        'ipsum',
                        'dolor',
                        'sit'
                    ],
                    [
                        'amet',
                        'consectetur',
                        'adipiscing',
                        'elit'
                    ]
                ],
                [
                    [
                        'sed',
                        'do',
                        'eiusmod'
                    ],
                    [
                        'tempor',
                        'incididunt',
                        'ut'
                    ]
                ],
                [
                    [
                        'labore',
                        'et',
                        'dolore'
                    ], [
                        'magna',
                        'aliqua'
                    ]
                ]
            ]
        }))

with the template

--- level 0
{{#loop}}
    --- level 1
    {{#.}}
        --- level 2
        {{#.}}
            --- level 3
            {{.}}
            --- /level 3
        {{/.}}
        --- /level 2 (I get lost sometimes)
    {{/.}}
    --- /level 1
{{/loop}}
--- /level 0

outputs

--- level 0
    --- level 1
        --- level 2
            --- level 3
            Lorem
            --- /level 3
            --- level 3
            ipsum
            --- /level 3
            --- level 3
            dolor
            --- /level 3
            --- level 3
            sit
            --- /level 3
        --- level 2
            --- level 3
            amet
            --- /level 3
            --- level 3
            consectetur
            --- /level 3
            --- level 3
            adipiscing
            --- /level 3
            --- level 3
            elit
            --- /level 3
        --- /level 2 (I get lost sometimes)
    --- /level 1
[...]

where the line --- /level 2 (I get lost sometimes) does not show up after the inner loop ended as long as the outer one is not finished. Whereas in the same example written in JS on JSFiddle it works as I would expect it to:


--- level 0
    --- level 1
        --- level 2
            --- level 3
            Lorem
            --- /level 3
            --- level 3
            ipsum
            --- /level 3
            --- level 3
            dolor
            --- /level 3
            --- level 3
            sit
            --- /level 3
        --- /level 2 (I get lost sometimes)
        --- level 2
            --- level 3
            amet
            --- /level 3
            --- level 3
            consectetur
            --- /level 3
            --- level 3
            adipiscing
            --- /level 3
            --- level 3
            elit
            --- /level 3
        --- /level 2 (I get lost sometimes)
    --- /level 1
[...]
noahmorrison commented 5 years ago

Fixed in 076fbfaf34e4373ec1474b6c115c74664f7380b1