Skip to content

Commit cf02bbc

Browse files
committed
Document dictionary tag
Closes #1474.
1 parent d33aebb commit cf02bbc

File tree

1 file changed

+164
-0
lines changed

1 file changed

+164
-0
lines changed
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
---
2+
title: Dictionary
3+
intro: Allows you to loop through options from a Dictionary.
4+
description: Allows you to loop through options from a Dictionary.
5+
id: 603bb573-5ebf-4851-935e-713f1caaa431
6+
parameters:
7+
-
8+
name: handle
9+
type: string
10+
description: 'The handle of the dictionary you wish to get options from.'
11+
required: true
12+
-
13+
name: sort
14+
type: string
15+
description: 'Sort options by field name (or `random`). You may pipe-separate multiple fields for sub-sorting and specify sort direction of each field using a colon. For example, `sort="name"` or `sort="date:asc|name:desc"` to sort by date then by name.'
16+
required: false
17+
-
18+
name: limit
19+
type: integer
20+
description: 'Limit the total results returned.'
21+
required: false
22+
-
23+
name: filter|query_scope
24+
type: string
25+
description: "Apply a custom [query scope](https://statamic.dev/extending/query-scopes-and-filters) You should specify the query scope's handle, which is usually the name of the class in snake case. For example: `MyAwesomeScope` would be `my_awesome_scope`."
26+
required: false
27+
-
28+
name: offset
29+
type: integer
30+
description: 'Skip a specified number of results.'
31+
required: false
32+
-
33+
name: paginate
34+
type: 'boolean|int *false*'
35+
description: 'Specify whether your options should be paginated. You can pass `true` and also use the `limit` param, or just pass the limit directly in here.'
36+
required: false
37+
-
38+
name: page_name
39+
type: 'string *page*'
40+
description: 'The query string variable used to store the page number (ie. `?page=`).'
41+
required: false
42+
-
43+
name: on_each_side
44+
type: 'int *3*'
45+
description: When using pagination, this specifies the max number of links each side of the current page. The minimum value is `1`.
46+
-
47+
name: as
48+
type: string
49+
description: 'Alias your options into a new variable loop.'
50+
required: false
51+
-
52+
name: scope
53+
type: string
54+
description: 'Scope your options with a variable prefix.'
55+
required: false
56+
-
57+
name: '*'
58+
type: string
59+
description: 'Any additional parameters will be set as config options on the Dictionary.'
60+
---
61+
62+
This tag allows you to loop through options from a [Dictionary](/fieldtypes/dictionary).
63+
64+
```
65+
{{ dictionary handle="countries" }}
66+
{{ label }} {{ value }}
67+
{{ /dictionary }}
68+
```
69+
70+
You can also use the shorthand syntax for this:
71+
72+
```
73+
{{ dictionary:countries }}
74+
{{ label }} {{ value }}
75+
{{ /dictionary:countries }}
76+
```
77+
78+
You can also output any additional data provided by the Dictionary, like `emoji` or `region` in the case of the Countries dictionary:
79+
80+
```
81+
{{ dictionary:countries }}
82+
{{ emoji }} {{ name }} in {{ region }}
83+
{{ /dictionary:countries }}
84+
```
85+
86+
## Searching
87+
88+
```antlers
89+
{{ dictionary:countries search="Aus" }}
90+
{{ label }} {{ value }}
91+
{{ /dictionary:countries }}
92+
```
93+
94+
```html
95+
🇦🇺 Australia AUS
96+
🇦🇹 Austria AUT
97+
```
98+
99+
## Conditions
100+
101+
```
102+
{{ dictionary:countries region:is="Europe" }}
103+
{{ label }} {{ value }}
104+
{{ /dictionary:countries }}
105+
```
106+
107+
There are a bunch of conditions available to you, like `:is`, `:isnt`, `:contains`, `:starts_with`, and `:is_before`. There are many more than that. In fact, there's a whole page dedicated to [conditions - check them out](/conditions).
108+
109+
## Paginating
110+
111+
To enable pagination mode, add the `paginate` parameter with the number of options in each page.
112+
113+
```
114+
{{ dictionary:countries paginate="10" as="countries" }}
115+
{{ countries }}
116+
{{ label }}<br>
117+
{{ /countries }}
118+
119+
{{ paginate }}
120+
<a href="{{ prev_page }}">⬅ Previous</a>
121+
122+
{{ current_page }} of {{ total_pages }} pages
123+
(There are {{ total_items }} posts)
124+
125+
<a href="{{ next_page }}">Next ➡</a>
126+
{{ /paginate }}
127+
{{ /dictionary:countries }}
128+
```
129+
130+
In pagination mode, your options will be scoped (in the example, we're scoping them into the `countries` tag pair). Use that tag pair to loop over the options in that page.
131+
132+
The `paginate` variable will become available to you. This is an array containing data about the paginated set.
133+
134+
| Variable | Description |
135+
|----------|-------------|
136+
| `next_page` | The URL to the next paginated page.
137+
| `prev_page` | The URL to the previous paginated page.
138+
| `total_items` | The total number of options.
139+
| `total_pages` | The number of paginated pages.
140+
| `current_page` | The current paginated page. (ie. the x in the ?page=x param)
141+
| `auto_links` | Outputs an HTML list of paginated links.
142+
| `links` | Contains data for you to construct a custom list of links.
143+
| `links:all` | An array of all the pages. You can loop over this and output {{ url }} and {{ page }}.
144+
| `links:segments` | An array of data for you to create a segmented list of links.
145+
146+
<br>
147+
148+
## Query Scopes
149+
150+
Doing something custom or complicated? You can create [query scopes](/extending/query-scopes-and-filters) to narrow down those results with the `query_scope` or `filter` parameter:
151+
152+
```
153+
{{ dictionary:countries query_scope="your_query_scope" }}
154+
```
155+
156+
You should reference the query scope by its handle, which is usually the name of the class in snake case. For example: `YourQueryScope` would be `your_query_scope`.
157+
158+
## Files
159+
160+
One of the powerful things you can do with the Files dictionary is pull in options from a JSON, YAML or CSV file.
161+
162+
```
163+
{{ dictionary:file filename="products.json" label="Name" value="Code" paginate="5" }}
164+
```

0 commit comments

Comments
 (0)