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

[5.x] Dictionary tag #10885

Merged
merged 4 commits into from
Oct 4, 2024
Merged

Conversation

ryanmitchell
Copy link
Contributor

@ryanmitchell ryanmitchell commented Oct 2, 2024

This PR adds a {{ dictionary }} tag to Antlers, allowing you to use dictionaries outside the field type.

Usage:

{{ dictionary:countries }}{{ label }} {{ value }}{{ /dictionary:countries }}

or

{{ dictionary handle="countries" }}{{ label }} {{ value }}{{ /dictionary}}

It also gets any ‘extra’ data:

{{ dictionary handle="countries" }}{{ emoji }} {{ value }}{{ /dictionary}}

Searching:

{{ dictionary:countries search="Aus" }}{{ label }} {{ value }}{{ /dictionary:countries }}

Passing config:

Any params passed that arent handle or search will be passed as config... eg

{{ dictionary:countries emojis="false" }}{{ label }} {{ value }}{{ /dictionary:countries }}

conditions, pagination, chunking and limiting:

It supports conditions, pagination, chunking and limiting eg

{{ dictionary:countries iso2:is="AUS" paginate="4" }}{{ label }} {{ value }}{{ /dictionary:countries }}

query scopes

You can also use query scopes:
{{ dictionary:countries query_scope="my_scope" }}{{ label }} {{ value }}{{ /dictionary:countries }}

Files:

Where this gets really powerful is pulling in arbitrary files and being able to filter, paginate and output them:
{{ dictionary:file filename="products.json" label="Name" value="Code" paginate="4" }}

Note:
Under the hood its using an Item query builder to allow filtering, ordering etc.

Closes statamic/ideas#1214.

Copy link
Member

@jasonvarga jasonvarga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to get all the data you can add supplement_data similar to search results

Do we think this would be a performance concern? I don't think so. Might as well just include the data all the time. It's already just sitting there.

The reason we do it in search is because we need to actually do separate lookups etc.

@ryanmitchell
Copy link
Contributor Author

Sure, easily changed.

This method gets an array of Item instances that would be used for each option.

To make it backwards compatible, a method is added to the abstract class. It's not performant, but it works.

The BasicDictionary class overrides it to make it performant.

If someone is making a dictionary that extends the base class, they don't have to override it, but they can if they want to improve the performance.

This is all done because ->options() would get the options, then calling ->get() is happening for each option, which would end up getting all the options for each one. In the case of the file dictionary for example, this means the file is read and parsed for each line in the file.
@jasonvarga
Copy link
Member

This was a bit out of scope for the tag, but I added a new method to the dictionary class to get the options.

The way you had it:

  • You would get all the options() which under the hood would get all the items.
  • Then you would get() each option, which would get all the items again each time.

i.e. When listing the countries, it would re-read the array 250 times, one for each country.
Or if you had a json file with 1000 options it would re-read and re-parse the file 1000 times.

This wasn't your fault as there wasn't an alternative for you.

Now, it'll get the options just once.

@ryanmitchell
Copy link
Contributor Author

Yeah I had noticed that and had considered adding an all() or something to dictionary but didn’t want to push the PR too far!

@jasonvarga jasonvarga merged commit 3956ab1 into statamic:5.x Oct 4, 2024
16 checks passed
@ryanmitchell ryanmitchell deleted the feature/dictionary-tag branch October 4, 2024 19:23
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

Successfully merging this pull request may close these issues.

Dictionary Tag to loop over all its items (like taxonomy tags)
2 participants