TangibleInc / template-system

A template system for WordPress with content type loops and conditions
https://docs.loopsandlogic.com/reference/template-system/
8 stars 3 forks source link

Loop tag: `map_keys` loop type sorts alphabetically by key #86

Closed BenTangible closed 7 months ago

BenTangible commented 8 months ago

It seems that the map_keys loop type doesn't default to looping through key/value pairs in the order they're specified, but instead sorts them into alphabetical order by key. Here's a simple example:

<Map name=animals>
  <Key cat>🐈</Key>
  <Key dog>🐶</Key>
  <Key ball>⚽</Key>
  <Key apple>🍎</Key>
</Map>

<ul>
  <Loop map_keys=animals>
    <li><Field key />: <Field value /></li>
  </Loop>
</ul>

This outputs:

eliot-akira commented 7 months ago

For this, it was indeed sorting the map keys alphabetically. I've removed that default, and added support for optionally sorting keys with sort_field=key.

However, in PHP and JavaScript, the order of the map keys is not guaranteed by the language specification. In practice they're always in the order that they were defined, but it might change, for example, if a language runtime decides to do some performance optimization.

eliot-akira commented 7 months ago

By the way, the template with emojis was a fun example, I copied it to create tests and it made the work more enjoyable.