statamic / ideas

đź’ˇDiscussions on ideas and feature requests for Statamic
https://statamic.dev
31 stars 1 forks source link

Antlers - Allow creation of new array items using dynamic key #957

Open dadaxr opened 1 year ago

dadaxr commented 1 year ago

Bug description

thanks to this pr statamic/cms#7594 we can do that :

{{ the_array = ['One', 'Two']; }}

{{ the_array.2 = 'Three'; }}

{{ the_array }}
    {{ value }}
{{ /the_array }}

ie : we can create item by using the nested path syntax.

But we can't use the square bracket syntax to do that:

{{ the_array[2] = 'Three'; }}

And by extend we cant use dynamic key either :

{{ the_array = [] }}
{{ loop from="0" to="2" }}
    {{ key = index}}
    {{ value = count }}
    {{ ("the_array["+key+"] = "+value)|dump }}
    {{ the_array[key] = value }}
{{ /loop }}
{{ the_array|dump }} {{# the_array is still empty #}}

output : image

a kind of (limited) workaround is to use the array union operator (+ or +=) ) :

{{ the_array = [] }}
{{ loop from="0" to="2" }}
    {{ key = index}}
    {{ value = count }}
    {{ the_array += value }}
{{ /loop }}
{{ the_array|dump }} {{# the_array is now filled #}}

image

I know the original PR mentions : image But apparently it's not only the empty square bracket syntax [] (= add a new item at the end) but any square bracket syntax [x] which looks not supported

How to reproduce

please check bug description section

Logs

No response

Environment

Environment
Application Name: Statamic leporc
Laravel Version: 9.52.4
PHP Version: 8.1.14
Composer Version: 2.5.1
Environment: local
Debug Mode: ENABLED
URL: leporc.localhost
Maintenance Mode: OFF

Cache
Config: NOT CACHED
Events: NOT CACHED
Routes: NOT CACHED
Views: CACHED

Drivers
Broadcasting: log
Cache: statamic
Database: mysql
Logs: stack / single
Mail: smtp
Queue: sync
Session: file

Statamic
Addons: 1
Antlers: runtime
Stache Watcher: Disabled
Static Caching: Disabled
Version: 3.4.5 PRO

Statamic Addons
statamic/seo-pro: 4.0.1

Installation

Fresh statamic/statamic site via CLI

Antlers Parser

None

Additional details

No response