Open willrowe opened 8 years ago
Hi,
I'm one of the few idiots that use Maps ;D
After some frustrating days at work I used some weekend time to add basic Map support to the library.
Supports:
For my use case at work that seems to be enough for now. ;)
What I didn't figure out:
Probably needs some more work in other filters and functions.
Maybe someone has some insight if I missed something obvious?
fork: https://github.com/NochEinKamel/twig.js/tree/Map-Object-Support-%23427 diff: https://github.com/twigjs/twig.js/compare/master...NochEinKamel:twig.js:Map-Object-Support-%23427?expand=1
I have always used plain objects in Twig.js as an equivalent to PHP associative arrays, and until a recent situation I ran into, this has worked just fine.
In PHP, an associative array the values will always always be kept the same order as they were inserted. In most cases the order may not matter, but I had a recent project where the order of elements on a page was dependent on the insertion order and the keys were representing the ID for that element. The elements could then be re-arranged, meaning the ID's would not be sequential.
Here is where the issue came in. In JavaScript plain objects does not keep the order of the values in the same order they are inserted, in the case I was testing the values were being ordered alphabetically by their key. This meant that the values I was getting from PHP, which were in a specific order, would be re-arranged when saved into a JavaScript order, representing an inconsistency between how it would be displayed in Twig for PHP and Twig.js. After looking into this a little further I was able to use Map objects to keep the data in the correct order, but, as far as I know, Twig.js does not support Map objects as a data structure.
Although this isn't something that comes up that often, I do think that it is important as far as being able to match the functionality of how a template would appear on the server side when dependent upon the order. I haven't taken a look at what would need to be done yet, but wanted to open this to see if anyone had any thoughts.