luoma 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/CHANGELOG.md +5 -0
  4. data/README.md +2 -2
  5. data/lib/luoma/context.rb +2 -1
  6. data/lib/luoma/drop.rb +2 -3
  7. data/lib/luoma/drops/range.rb +1 -1
  8. data/lib/luoma/drops/undefined.rb +1 -1
  9. data/lib/luoma/environment.rb +13 -14
  10. data/lib/luoma/escape.rb +53 -0
  11. data/lib/luoma/expression.rb +8 -8
  12. data/lib/luoma/filter.rb +3 -3
  13. data/lib/luoma/filters/escape_js.rb +12 -0
  14. data/lib/luoma/filters/slice.rb +1 -1
  15. data/lib/luoma/filters/sort.rb +0 -25
  16. data/lib/luoma/filters/string.rb +2 -2
  17. data/lib/luoma/loaders/file_system_loader.rb +10 -9
  18. data/lib/luoma/parser_unified.rb +2 -1
  19. data/lib/luoma/tags/assign.rb +4 -0
  20. data/lib/luoma/tags/case.rb +1 -1
  21. data/lib/luoma/template.rb +0 -2
  22. data/lib/luoma/version.rb +1 -1
  23. data/lib/luoma.rb +2 -0
  24. data/sig/luoma/drop.rbs +2 -2
  25. data/sig/luoma/environment.rbs +8 -8
  26. data/sig/luoma/escape.rbs +7 -0
  27. data/sig/luoma/filters/escape_js.rbs +6 -0
  28. data/sig/luoma/filters/sort.rbs +0 -4
  29. data/sig/luoma/loaders/file_system_loader.rbs +4 -0
  30. data.tar.gz.sig +1 -2
  31. metadata +4 -26
  32. metadata.gz.sig +0 -0
  33. data/docs/configuration.md +0 -180
  34. data/docs/custom_filters.md +0 -3
  35. data/docs/custom_tags.md +0 -3
  36. data/docs/expressions.md +0 -3
  37. data/docs/extension_types.md +0 -41
  38. data/docs/filter_reference.md +0 -1884
  39. data/docs/index.md +0 -77
  40. data/docs/luoma_for_template_authors.md +0 -3
  41. data/docs/markdown.md +0 -111
  42. data/docs/predicate_reference.md +0 -3
  43. data/docs/static_analysis.md +0 -3
  44. data/docs/tag_reference.md +0 -352
  45. data/docs/template_loaders.md +0 -177
  46. data/docs/undefined_variables.md +0 -3
  47. data/docs-requirements.txt +0 -11
  48. data/docs_/cycle.md +0 -17
  49. data/docs_/font_rendering_example.md +0 -157
  50. data/docs_/header_block_example.md +0 -51
  51. data/docs_/increment_and_decrement.md +0 -49
  52. data/docs_/logo_style_example.md +0 -40
  53. data/docs_/migration.md +0 -11
  54. data/docs_/notes.md +0 -19
  55. data/zensical.toml +0 -363
@@ -1,1884 +0,0 @@
1
- # Built-in filters
2
-
3
- !!! warning
4
-
5
- This page is a work in progress. Some of the information here is not accurate.
6
-
7
- ## abs
8
-
9
- ```
10
- <number> | abs
11
- ```
12
-
13
- Return the absolute value of a number. Works on integers, floats and string representations of integers or floats.
14
-
15
- ```liquid2
16
- {{ -42 | abs }}
17
- {{ 7.5 | abs }}
18
- {{ '42.0' | abs }}
19
- ```
20
-
21
- ```plain title="output"
22
- 42
23
- 7.5
24
- 42.0
25
- ```
26
-
27
- Given a value that can't be cast to an integer or float, the special value `Nothing` will be returned.
28
-
29
- ```liquid2
30
- {{ 'hello' | abs }}
31
- {{ 'hello' | abs or 0 }}
32
- {{ ('hello' | abs) or 99 }}
33
- ```
34
-
35
- ```plain title="output"
36
-
37
- 0
38
- ```
39
-
40
- ## all
41
-
42
- ```
43
- <array> | all
44
- <array> | all: <string> [, <value>]
45
- <array> | all: <lambda>
46
- ```
47
-
48
- Return `true` if all items in the input array are truthy, or `false` otherwise.
49
-
50
- ```liquid2
51
- {{ [true, true, true] | all }}
52
- {{ [true, false, true] | all }}
53
- ```
54
-
55
- ```title="Output"
56
- true
57
- false
58
- ```
59
-
60
- If a string argument is given, array items should be objects and the string is used as a property name to test for truthiness.
61
-
62
- ```liquid2
63
- {% assign
64
- items = [
65
- {"title": "foo", "active": true},
66
- {"title": "bar", "active": false},
67
- {"title": "baz", "active": true},
68
- ]
69
- %}
70
-
71
- {{ items | all: "active" }}
72
- ```
73
-
74
- ```title="Output"
75
- false
76
- ```
77
-
78
- If the optional second argument is given, the value at the given property will be compared to the argument value instead of testing for truthiness.
79
-
80
- ```liquid2
81
- {% assign
82
- items = [
83
- {"title": "foo", "state": 2},
84
- {"title": "bar", "state": 2},
85
- {"title": "baz", "state": 2},
86
- ]
87
- %}
88
-
89
- {{ items | all: "state", 2 }}
90
- ```
91
-
92
- ```title="Output"
93
- true
94
- ```
95
-
96
- Given a lambda expression as the first argument, the expression will be evaluated for each item in the input array and the result tested for truthiness.
97
-
98
- ```liquid2
99
- {% assign
100
- items = [
101
- {"title": "foo", "state": 2},
102
- {"title": "bar", "state": 3},
103
- {"title": "baz", "state": 1},
104
- ]
105
- %}
106
-
107
- {{ items | all: (x) -> x.state < 5 }}
108
- ```
109
-
110
- ```title="Output"
111
- true
112
- ```
113
-
114
- ## any
115
-
116
- ```
117
- <array> | any
118
- <array> | any: <string> [, <value>]
119
- <array> | any: <lambda>
120
- ```
121
-
122
- Return `true` if any of the items in the input array are truthy, or `false` if they are all falsy.
123
-
124
- ```liquid2
125
- {{ [true, true, true] | any }}
126
- {{ [true, false, true] | any }}
127
- {{ [false, false, false] | any }}
128
- ```
129
-
130
- ```title="Output"
131
- true
132
- true
133
- false
134
- ```
135
-
136
- If a string argument is given, array items should be objects and the string is used as a property name to test for truthiness.
137
-
138
- ```liquid2
139
- {% assign
140
- items = [
141
- {"title": "foo", "active": true},
142
- {"title": "bar", "active": false},
143
- {"title": "baz", "active": true},
144
- ]
145
- %}
146
-
147
- {{ items | any: "active" }}
148
- ```
149
-
150
- ```title="Output"
151
- true
152
- ```
153
-
154
- If the optional second argument is given, the value at the given property will be compared to the argument value instead of testing for truthiness.
155
-
156
- ```liquid2
157
- {% assign
158
- items = [
159
- {"title": "foo", "state": 2},
160
- {"title": "bar", "state": 2},
161
- {"title": "baz", "state": 2},
162
- ]
163
- %}
164
-
165
- {{ items | any: "state", 2 }}
166
- ```
167
-
168
- ```title="Output"
169
- true
170
- ```
171
-
172
- Given a lambda expression as the first argument, the expression will be evaluated for each item in the input array and the result tested for truthiness.
173
-
174
- ```liquid2
175
- {% assign
176
- items = [
177
- {"title": "foo", "state": 2},
178
- {"title": "bar", "state": 3},
179
- {"title": "baz", "state": 1},
180
- ]
181
- %}
182
-
183
- {{ items | any: (x) -> x.state < 2 }}
184
- ```
185
-
186
- ```title="Output"
187
- true
188
- ```
189
-
190
- ## append
191
-
192
- ```
193
- <string> | append: <string>
194
- ```
195
-
196
- Return the input value concatenated with the argument value.
197
-
198
- ```liquid2
199
- {{ 'Hello, ' | append: 'World!' }}
200
- ```
201
-
202
- ```plain title="output"
203
- Hello, World!
204
- ```
205
-
206
- If either the input value or argument are not a string, they will be coerced to a string before concatenation.
207
-
208
- ```liquid2
209
- {% assign a_number = 7.5 -%}
210
- {{ 42 | append: a_number }}
211
- {{ nosuchthing | append: 'World!' }}
212
- ```
213
-
214
- ```plain title="output"
215
- 427.5
216
- World!
217
- ```
218
-
219
- ## at_least
220
-
221
- ```
222
- <number> | at_least: <number>
223
- ```
224
-
225
- Return the maximum of the filter's input value and its argument. If either input value or argument are string representations of an integer or float, they will be cast to an integer or float prior to comparison.
226
-
227
- ```liquid2
228
- {{ -5.1 | at_least: 8 }}
229
- {{ 8 | at_least: '5' }}
230
- ```
231
-
232
- ```plain title="output"
233
- 8
234
- 8
235
- ```
236
-
237
- If both input value and argument can not be cast to an integer or float, the special value `Nothing` will be returned instead.
238
-
239
- ```liquid2
240
- {{ "hello" | at_least: 2 }}
241
- {{ "hello" | at_least: -2 }}
242
- {{ -1 | at_least: "abc" }}
243
- {{ ('foo' | at_least: "bar") or 42 }}
244
- ```
245
-
246
- ```plain title="output"
247
- 2
248
- -2
249
- -1
250
- 42
251
- ```
252
-
253
- ## at_most
254
-
255
- ```
256
- <number> | at_most: <number>
257
- ```
258
-
259
- Return the minimum of the filter's input value and its argument. If either input value or argument are string representations of an integer or float, they will be cast to an integer or float prior to comparison.
260
-
261
- ```liquid2
262
- {{ 5 | at_most: 8 }}
263
- {{ '8' | at_most: 5 }}
264
- ```
265
-
266
- ```plain title="output"
267
- 5
268
- 5
269
- ```
270
-
271
- If both input value and argument can not be cast to an integer or float, the special value `Nothing` will be returned instead.
272
-
273
- ```liquid2
274
- {{ "hello" | at_most: 2 }}
275
- {{ "hello" | at_most: -2 }}
276
- {{ -1 | at_most: "abc" }}
277
- {{ ('foo' | at_most: "bar") or 42 }}
278
- ```
279
-
280
- ```plain title="output"
281
- 2
282
- 2
283
- -1
284
- 42
285
- ```
286
-
287
- ## capitalize
288
-
289
- ```
290
- <string> | capitalize
291
- ```
292
-
293
- Return the input string with the first character in upper case and the rest lowercase.
294
-
295
- ```liquid2
296
- {{ 'heLLO, World!' | capitalize }}
297
- ```
298
-
299
- ```plain title="output"
300
- Hello, world!
301
- ```
302
-
303
- If the input value is not a string, it will be converted to a string.
304
-
305
- ```liquid2
306
- {{ 42 | capitalize }}
307
- ```
308
-
309
- ```plain title="output"
310
- 42
311
- ```
312
-
313
- ## ceil
314
-
315
- ```
316
- <number> | ceil
317
- ```
318
-
319
- Round the input value up to the nearest whole number. The input value will be converted to a number if it is not an integer or float.
320
-
321
- ```liquid2
322
- {{ 5.1 | ceil }}
323
- {{ 5.0 | ceil }}
324
- {{ 5 | ceil }}
325
- {{ '5.4' | ceil }}
326
- ```
327
-
328
- ```plain title="output"
329
- 6
330
- 5
331
- 5
332
- 5
333
- ```
334
-
335
- If the input is undefined or can't be converted to a number, the special value `Nothing` is returned.
336
-
337
- ```liquid2
338
- {{ 'hello' | ceil }}
339
- {{ ('hello' | ceil) or 1 }}
340
- ```
341
-
342
- ```plain title="output"
343
-
344
- 1
345
- ```
346
-
347
- ## compact
348
-
349
- ```
350
- <array> | compact[: <key>]
351
- ```
352
-
353
- Return a new array containing items from the input array excluding `null` and `Nothing` values.
354
-
355
- ```liquid2
356
- {%- assign a = [1, 2, null, nosuchthing ] -%}
357
- {{ a | compact }}
358
- ```
359
-
360
- ```title="Output"
361
- [1,2]
362
- ```
363
-
364
- If `key` is given and it is a string, array items should be objects and the key is used to lookup a property of each object.
365
-
366
- ```liquid2
367
- {%- assign
368
- items = [
369
- { "title": "foo", "id": 1 },
370
- { "title": null, "id": 2 },
371
- { "title": "baz", "id": 3 },
372
- ]
373
- -%}
374
-
375
- {{ items | compact: "title" }}
376
- ```
377
-
378
- ```title="Output"
379
- [{"title":"foo","id":1},{"title":"baz","id":3}]
380
- ```
381
-
382
- If `key` is a lambda expression, the expression is evaluated for each item in the input array. If the expression evaluates to `nil` or `Nothing`, the item is excluded from the result.
383
-
384
- ```liquid2
385
- {%- assign
386
- items = [
387
- { "title": "foo", "id": 1 },
388
- { "id": null },
389
- { "title": null, "id": 3 },
390
- ]
391
- -%}
392
-
393
- {{ items | compact: x -> (x.title or x.id) }}
394
- ```
395
-
396
- ```title="Output"
397
- [{"title":"foo","id":1},{"title":null,"id":3}]
398
- ```
399
-
400
- ## concat
401
-
402
- ```
403
- <array> | concat: <array>
404
- ```
405
-
406
- Create a new array by joining one array-like object with another.
407
-
408
- ```liquid2
409
- {% assign fruits = "apples, oranges, peaches" | split: ", " %}
410
- {% assign vegetables = "carrots, turnips, potatoes" | split: ", " %}
411
-
412
- {% assign everything = fruits | concat: vegetables %}
413
-
414
- {% for item in everything %}
415
- - {{ item }}
416
- {% endfor %}
417
- ```
418
-
419
- ```plain title="output"
420
- - apples
421
- - oranges
422
- - peaches
423
- - carrots
424
- - turnips
425
- - potatoes
426
- ```
427
-
428
- If the input value is not array-like, it will be converted to an array. No conversion is attempted for the argument value.
429
-
430
- ```liquid2
431
- {% assign fruits = "apples, oranges, peaches" | split: ", " -%}
432
- {% assign things = "hello" | concat: fruits -%}
433
-
434
- {% for item in things -%}
435
- - {{ item }}
436
- {% endfor %}
437
- ```
438
-
439
- ```plain title="output"
440
- - h
441
- - e
442
- - l
443
- - l
444
- - o
445
- - apples
446
- - oranges
447
- - peaches
448
- ```
449
-
450
- If the input is a nested array, it will be flattened before concatenation. The argument is not flattened.
451
-
452
- ```json title="data"
453
- {
454
- "a": [
455
- ["a", "x"],
456
- ["b", ["y", ["z"]]]
457
- ],
458
- "b": ["c", "d"]
459
- }
460
- ```
461
-
462
- ```liquid2
463
- {{ a | concat: b | join: '#' }}
464
- ```
465
-
466
- ```plain title="output"
467
- a#x#b#y#z#c#d
468
- ```
469
-
470
- ## date
471
-
472
- ```
473
- <datetime> | date: <string>
474
- ```
475
-
476
- Format a date and/or time according the the given format string. The input can be a string, in which case the string will be parsed as a date/time before formatting.
477
-
478
- :::caution
479
-
480
- LiquidScript's `date` filter can parse Unix timestamps, ISO 8601, RFC2822, SQL and HTTP header formatted date/time strings. It does not do fuzzy parsing like Ruby or Python Liquid.
481
-
482
- :::
483
-
484
- ```liquid2
485
- {{ "March 14, 2016" | date: "%b %d, %y" }}
486
- ```
487
-
488
- ```plain title="output"
489
- Mar 14, 16
490
- ```
491
-
492
- The special `'now'` or `'today'` input values can be used to get the current timestamp. `'today'` is an alias for `'now'`. Both include time information.
493
-
494
- ```liquid2
495
- {{ "now" | date: "%Y-%m-%d %H:%M" }}
496
- ```
497
-
498
- ```plain title="output"
499
- 2021-12-02 10:17
500
- ```
501
-
502
- If the input is undefined, an empty string is returned.
503
-
504
- ```liquid2
505
- {{ nosuchthing | date: "%Y-%m-%d %H:%M" }}
506
- ```
507
-
508
- ```plain title="output"
509
-
510
- ```
511
-
512
- ## default
513
-
514
- ```
515
- <expression> | default[: <object>[, allow_false:<bool>]]
516
- ```
517
-
518
- Return a default value if the input is undefined, `nil`/`null`, `false` or empty, or return the input unchanged otherwise.
519
-
520
- ```liquid2
521
- {{ product_price | default: 2.99 }}
522
-
523
- {%- assign product_price = "" %}
524
- {{ product_price | default: 2.99 }}
525
-
526
- {%- assign product_price = 4.99 %}
527
- {{ product_price | default: 2.99 }}
528
- ```
529
-
530
- ```plain title="output"
531
- 2.99
532
- 2.99
533
- 4.99
534
- ```
535
-
536
- If the optional `allow_false` argument is `true`, an input of `false` will not return the default. `allow_false` defaults to `false`.
537
-
538
- ```liquid2
539
- {% assign product_reduced = false -%}
540
- {{ product_reduced | default: true, allow_false: true }}
541
- ```
542
-
543
- ```plain title="output"
544
- false
545
- ```
546
-
547
- If no argument is given, the default value will be an empty string.
548
-
549
- ```liquid2
550
- {{ product_price | default }}
551
- ```
552
-
553
- ```plain title="output"
554
-
555
- ```
556
-
557
- Empty strings, arrays and objects all cause the default value to be returned. `0` does not.
558
-
559
- ```liquid2
560
- {{ "" | default: "hello" }}
561
- {{ 0 | default: 99 }}
562
- ```
563
-
564
- ```plain title="output"
565
- hello
566
- 0
567
- ```
568
-
569
- ## divided_by
570
-
571
- ```
572
- <number> | divided_by: <number>
573
- ```
574
-
575
- Divide a number by another number. The result is rounded down to the nearest integer if the divisor is an integer.
576
-
577
- ```liquid2
578
- {{ 16 | divided_by: 4 }}
579
- {{ 5 | divided_by: 3 }}
580
- ```
581
-
582
- ```plain title="output"
583
- 4
584
- 1
585
- ```
586
-
587
- If you divide by a float, the result will be a float.
588
-
589
- ```liquid2
590
- {{ 20 | divided_by: 7 }}
591
- {{ 20 | divided_by: 7.0 }}
592
- ```
593
-
594
- ```plain title="output"
595
- 2
596
- 2.857142857142857
597
- ```
598
-
599
- If either the input or argument are not an integer or float, Liquid will try to convert them to an integer or float. If the input can't be converted, `0` will be used instead. If the argument can't be converted, an exception is raised.
600
-
601
- ```liquid2
602
- {{ "20" | divided_by: "7" }}
603
- {{ "hello" | divided_by: 2 }}
604
- ```
605
-
606
- ```plain title="output"
607
- 2
608
- 0
609
- ```
610
-
611
- ## downcase
612
-
613
- ```
614
- <string> | downcase
615
- ```
616
-
617
- Return the input string with all characters in lowercase.
618
-
619
- ```liquid2
620
- {{ 'Hello, World!' | downcase }}
621
- ```
622
-
623
- ```plain title="output"
624
- hello, world!
625
- ```
626
-
627
- If the input is not a string, Liquid will convert it to a string before forcing characters to lowercase.
628
-
629
- ```liquid2
630
- {{ 5 | downcase }}
631
- ```
632
-
633
- ```plain title="output"
634
- 5
635
- ```
636
-
637
- If the input is undefined, an empty string is returned.
638
-
639
- ## escape
640
-
641
- ```
642
- <string> | escape
643
- ```
644
-
645
- Escape special characters in a string for safe use in HTML.
646
-
647
- This filter replaces the characters `&`, `<`, `>`, `'`, and `"` with their corresponding HTML-safe sequences:
648
-
649
- - `&` -> `&amp;`
650
- - `<` -> `&lt;`
651
- - `>` -> `&gt;`
652
- - `'` -> `&#39;`
653
- - `"` -> `&#34;`
654
-
655
- This helps prevent HTML injection when rendering untrusted content in HTML element bodies or attributes.
656
-
657
- :::caution
658
-
659
- This filter does **not** make strings safe for use in JavaScript, including in `<script>` blocks, inline event handler attributes (e.g. `onerror`), or other JavaScript contexts. For those cases, use the [`escapejs`](#escapejs) filter instead.
660
-
661
- :::
662
-
663
- ```liquid2
664
- {{ "Have you read 'James & the Giant Peach'?" | escape }}
665
- ```
666
-
667
- ```plain title="output"
668
- Have you read &#39;James &amp; the Giant Peach&#39;?
669
- ```
670
-
671
- ## escapejs
672
-
673
- ```
674
- <string> | escapejs
675
- ```
676
-
677
- Escape characters for safe use in JavaScript string literals.
678
-
679
- This filter escapes a string for embedding inside **JavaScript string literals**, using either single or double quotes (e.g. `'...'` or `"..."`). It replaces control characters and potentially dangerous symbols with their corresponding Unicode escape sequences.
680
-
681
- Escaped characters include:
682
-
683
- - ASCII control characters (U+0000 to U+001F)
684
- - Characters like quotes, angle brackets, ampersands, equals signs - Line/paragraph separators (U+2028, U+2029)
685
-
686
- :::caution
687
-
688
- This filter does **not** make strings safe for use in JavaScript template literals (backtick strings), or in raw JavaScript expressions. Use it only when placing data inside quoted JS strings within inline `<script>` blocks or event handlers.
689
-
690
- **Recommended alternatives:**
691
-
692
- - Pass data using HTML `data-*` attributes and read them in JS via `element.dataset`.
693
- - For structured data, prefer a JSON-serialization approach using a JSON filter.
694
-
695
- :::
696
-
697
- ```liquid2
698
- {% assign some_string = "<script>alert('x')</script>" %}
699
- <img src="" onerror="{{ some_string | escapejs }}" />
700
- ```
701
-
702
- ```plain title="output"
703
- <img src="" onerror="\u003Cscript\u003Ealert(\u0027x\u0027)\u003C/script\u003E" />
704
- ```
705
-
706
- ## escape_once
707
-
708
- ```
709
- <string> | escape_once
710
- ```
711
-
712
- Escape a string for HTML, but avoid double-escaping existing entities.
713
-
714
- Converts characters like `&`, `<`, and `>` to their HTML-safe sequences, but leaves existing HTML entities untouched (e.g., `&amp;` stays `&amp;`).
715
-
716
- This is useful when escaping content that may already be partially escaped.
717
-
718
- See the [`escape`](#escape) filter for details and limitations.
719
-
720
- ```liquid2
721
- {{ "Have you read 'James &amp; the Giant Peach'?" | escape_once }}
722
- ```
723
-
724
- ```plain title="output"
725
- Have you read &#39;James &amp; the Giant Peach&#39;?
726
- ```
727
-
728
- ## find
729
-
730
- ```
731
- <array> | find: <string>[, <object>]
732
- ```
733
-
734
- Return the first item in the input array that contains a property, given as the first argument, equal to the value given as the second argument. If no such item exists, `null` is returned.
735
-
736
- In this example we select the first page in the "Programming" category.
737
-
738
- ```json title="data"
739
- {
740
- "pages": [
741
- {
742
- "id": 1,
743
- "title": "Introduction to Cooking",
744
- "category": "Cooking",
745
- "tags": ["recipes", "beginner", "cooking techniques"]
746
- },
747
- {
748
- "id": 2,
749
- "title": "Top 10 Travel Destinations in Europe",
750
- "category": "Travel",
751
- "tags": ["Europe", "destinations", "travel tips"]
752
- },
753
- {
754
- "id": 3,
755
- "title": "Mastering JavaScript",
756
- "category": "Programming",
757
- "tags": ["JavaScript", "web development", "coding"]
758
- }
759
- ]
760
- }
761
- ```
762
-
763
- ```liquid2
764
- {% assign page = pages | find: 'category', 'Programming' %}
765
- {{ page.title }}
766
- ```
767
-
768
- ```plain title="output"
769
- Mastering JavaScript
770
- ```
771
-
772
- ## find_index
773
-
774
- Return the index of the first item in the input array that contains a property, given as the first argument, equal to the value given as the second argument. If no such item exists, `null` is returned.
775
-
776
- In this example we find the index for the first page in the "Programming" category.
777
-
778
- ```json title="data"
779
- {
780
- "pages": [
781
- {
782
- "id": 1,
783
- "title": "Introduction to Cooking",
784
- "category": "Cooking",
785
- "tags": ["recipes", "beginner", "cooking techniques"]
786
- },
787
- {
788
- "id": 2,
789
- "title": "Top 10 Travel Destinations in Europe",
790
- "category": "Travel",
791
- "tags": ["Europe", "destinations", "travel tips"]
792
- },
793
- {
794
- "id": 3,
795
- "title": "Mastering JavaScript",
796
- "category": "Programming",
797
- "tags": ["JavaScript", "web development", "coding"]
798
- }
799
- ]
800
- }
801
- ```
802
-
803
- ```liquid2
804
- {% assign index = pages | find_index: 'category', 'Programming' %}
805
- {{ pages[index].title }}
806
- ```
807
-
808
- ```plain title="output"
809
- Mastering JavaScript
810
- ```
811
-
812
- ## first
813
-
814
- ```
815
- <sequence> | first
816
- ```
817
-
818
- Return the first item of the input sequence. The input could be array-like or a mapping, but not a string.
819
-
820
- ```liquid2
821
- {{ "Ground control to Major Tom." | split: " " | first }}
822
- ```
823
-
824
- ```plain title="output"
825
- Ground
826
- ```
827
-
828
- If the input sequence is undefined, empty or not a sequence, `nil` is returned.
829
-
830
- ## flatten
831
-
832
- TODO
833
-
834
- ## flat_map
835
-
836
- TODO
837
-
838
- ## floor
839
-
840
- ```
841
- <number> | floor
842
- ```
843
-
844
- Return the input down to the nearest whole number. Liquid tries to convert the input to a number before the filter is applied.
845
-
846
- ```liquid2
847
- {{ 1.2 | floor }}
848
- {{ 2.0 | floor }}
849
- {{ 183.357 | floor }}
850
- {{ -5.4 | floor }}
851
- {{ "3.5" | floor }}
852
- ```
853
-
854
- ```plain title="output"
855
- 1
856
- 2
857
- 183
858
- -6
859
- 3
860
- ```
861
-
862
- If the input can't be converted to a number, `0` is returned.
863
-
864
- ## has
865
-
866
- ```
867
- <array> | has: <string>[, <object>]
868
- ```
869
-
870
- Return `true` if the input array contains an object with a property identified by the first argument that is equal to the object given as the second argument. `false` is returned if none of the items in the input array contain such a property/value.
871
-
872
- In this example we test to see if any pages are in the "Programming" category.
873
-
874
- ```json title="data"
875
- {
876
- "pages": [
877
- {
878
- "id": 1,
879
- "title": "Introduction to Cooking",
880
- "category": "Cooking",
881
- "tags": ["recipes", "beginner", "cooking techniques"]
882
- },
883
- {
884
- "id": 2,
885
- "title": "Top 10 Travel Destinations in Europe",
886
- "category": "Travel",
887
- "tags": ["Europe", "destinations", "travel tips"]
888
- },
889
- {
890
- "id": 3,
891
- "title": "Mastering JavaScript",
892
- "category": "Programming",
893
- "tags": ["JavaScript", "web development", "coding"]
894
- }
895
- ]
896
- }
897
- ```
898
-
899
- ```liquid2
900
- {% assign has_programming_page = pages | has: 'category', 'Programming' %}
901
- {{ has_programming_page }}
902
- ```
903
-
904
- ```plain title="output"
905
- true
906
- ```
907
-
908
- ## join
909
-
910
- ```
911
- <array> | join[: <string>]
912
- ```
913
-
914
- Return the items in the input array as a single string, separated by the argument string. If the
915
- input is not an array, Liquid will convert it to one. If input array items are not strings, they
916
- will be converted to strings before joining.
917
-
918
- ```liquid2
919
- {% assign beatles = "John, Paul, George, Ringo" | split: ", " -%}
920
-
921
- {{ beatles | join: " and " }}
922
- ```
923
-
924
- ```plain title="output"
925
- John and Paul and George and Ringo
926
- ```
927
-
928
- If an argument string is not given, it defaults to a single space.
929
-
930
- ```liquid2
931
- {% assign beatles = "John, Paul, George, Ringo" | split: ", " -%}
932
-
933
- {{ beatles | join }}
934
- ```
935
-
936
- ```plain title="output"
937
- John Paul George Ringo
938
- ```
939
-
940
- ## json
941
-
942
- TODO
943
-
944
- ## last
945
-
946
- ```
947
- <array> | last
948
- ```
949
-
950
- Return the last item in the array-like input.
951
-
952
- ```liquid2
953
- {{ "Ground control to Major Tom." | split: " " | last }}
954
- ```
955
-
956
- ```plain title="output"
957
- Tom.
958
- ```
959
-
960
- If the input is undefined, empty, string or a number, `nil` will be returned.
961
-
962
- ## lstrip
963
-
964
- ```
965
- <string> | lstrip
966
- ```
967
-
968
- Return the input string with all leading whitespace removed. If the input is not a string, it will
969
- be converted to a string before stripping whitespace.
970
-
971
- ```liquid2
972
- {{ " So much room for activities " | lstrip }}!
973
- ```
974
-
975
- ```plain title="output"
976
- So much room for activities !
977
- ```
978
-
979
- ## map
980
-
981
- ```
982
- <array> | map: <string | lambda expression>
983
- ```
984
-
985
- Extract properties from an array of objects into a new array.
986
-
987
- For example, if `pages` is an array of objects with a `category` property:
988
-
989
- ```json title="data"
990
- {
991
- "pages": [
992
- { "category": "business" },
993
- { "category": "celebrities" },
994
- { "category": "lifestyle" },
995
- { "category": "sports" },
996
- { "category": "technology" }
997
- ]
998
- }
999
- ```
1000
-
1001
- ```liquid2
1002
- {% assign categories = pages | map: "category" -%}
1003
-
1004
- {% for category in categories -%}
1005
- - {{ category }}
1006
- {%- endfor %}
1007
- ```
1008
-
1009
- ```plain title="output"
1010
- - business
1011
- - celebrities
1012
- - lifestyle
1013
- - sports
1014
- - technology
1015
- ```
1016
-
1017
- ## max
1018
-
1019
- TODO:
1020
-
1021
- ## min
1022
-
1023
- TODO:
1024
-
1025
- ## minus
1026
-
1027
- ```
1028
- <number> | minus: <number>
1029
- ```
1030
-
1031
- Return the result of subtracting one number from another. If either the input or argument are not a number, Liquid will try to convert them to a number. If that conversion fails, `0` is used instead.
1032
-
1033
- ```liquid2
1034
- {{ 4 | minus: 2 }}
1035
- {{ "16" | minus: 4 }}
1036
- {{ 183.357 | minus: 12.2 }}
1037
- {{ "hello" | minus: 10 }}
1038
- ```
1039
-
1040
- ```plain title="output"
1041
- 2
1042
- 12
1043
- 171.157
1044
- -10
1045
- ```
1046
-
1047
- ## modulo
1048
-
1049
- ```
1050
- <number> | modulo: <number>
1051
- ```
1052
-
1053
- Return the remainder from the division of the input by the argument.
1054
-
1055
- ```liquid2
1056
- {{ 3 | modulo: 2 }}
1057
- {{ "24" | modulo: "7" }}
1058
- {{ 183.357 | modulo: 12 }}
1059
- ```
1060
-
1061
- ```plain title="output"
1062
- 1
1063
- 3
1064
- 3.357
1065
- ```
1066
-
1067
- If either the input or argument are not an integer or float, Liquid will try to convert them to an
1068
- integer or float. If the input can't be converted, `0` will be used instead. If the argument can't
1069
- be converted, an exception is raised.
1070
-
1071
- ## newline_to_br
1072
-
1073
- ```
1074
- <string> | newline_to_br
1075
- ```
1076
-
1077
- Return the input string with `\n` and `\r\n` replaced with `<br />\n`.
1078
-
1079
- ```liquid2
1080
- {% capture string_with_newlines %}
1081
- Hello
1082
- there
1083
- {% endcapture %}
1084
-
1085
- {{ string_with_newlines | newline_to_br }}
1086
- ```
1087
-
1088
- ```plain title="output"
1089
-
1090
-
1091
- <br />
1092
- Hello<br />
1093
- there<br />
1094
-
1095
- ```
1096
-
1097
- ## plus
1098
-
1099
- ```
1100
- <number> | plus: <number>
1101
- ```
1102
-
1103
- Return the result of adding one number to another. If either the input or argument are not a number, Liquid will try to convert them to a number. If that conversion fails, `0` is used instead.
1104
-
1105
- ```liquid2
1106
- {{ 4 | plus: 2 }}
1107
- {{ "16" | plus: "4" }}
1108
- {{ 183.357 | plus: 12 }}
1109
- ```
1110
-
1111
- ```plain title="output"
1112
- 6
1113
- 20
1114
- 195.357
1115
- ```
1116
-
1117
- ## prepend
1118
-
1119
- ```
1120
- <string> | prepend: <string>
1121
- ```
1122
-
1123
- Return the argument concatenated with the filter input.
1124
-
1125
- ```liquid2
1126
- {{ "apples, oranges, and bananas" | prepend: "Some fruit: " }}
1127
- ```
1128
-
1129
- ```plain title="output"
1130
- Some fruit: apples, oranges, and bananas
1131
- ```
1132
-
1133
- If either the input value or argument are not a string, they will be coerced to a string before
1134
- concatenation.
1135
-
1136
- ```liquid2
1137
- {% assign a_number = 7.5 -%}
1138
- {{ 42 | prepend: a_number }}
1139
- {{ nosuchthing | prepend: 'World!' }}
1140
- ```
1141
-
1142
- ```plain title="output"
1143
- 7.542
1144
- World!
1145
- ```
1146
-
1147
- ## reject
1148
-
1149
- ```
1150
- <array> | reject: <string>[, <object>]
1151
- ```
1152
-
1153
- Return a copy of the input array including only those objects that have a property, named with the first argument, **that is not equal to** a value, given as the second argument. If a second argument is not given, only elements with the named property that are falsy will be included.
1154
-
1155
- ```json title="data"
1156
- {
1157
- "products": [
1158
- { "title": "Vacuum", "type": "house", "available": true },
1159
- { "title": "Spatula", "type": "kitchen", "available": false },
1160
- { "title": "Television", "type": "lounge", "available": true },
1161
- { "title": "Garlic press", "type": "kitchen", "available": true }
1162
- ]
1163
- }
1164
- ```
1165
-
1166
- ```liquid2
1167
- All products:
1168
- {% for product in products -%}
1169
- - {{ product.title }}
1170
- {% endfor %}
1171
-
1172
- {%- assign kitchen_products = products | reject: "type", "kitchen" -%}
1173
-
1174
- Non kitchen products:
1175
- {% for product in kitchen_products -%}
1176
- - {{ product.title }}
1177
- {% endfor %}
1178
-
1179
- {%- assign unavailable_products = products | reject: "available" -%}
1180
-
1181
- Unavailable products:
1182
- {% for product in unavailable_products -%}
1183
- - {{ product.title }}
1184
- {% endfor %}
1185
- ```
1186
-
1187
- ```plain title="output"
1188
- All products:
1189
- - Vacuum
1190
- - Spatula
1191
- - Television
1192
- - Garlic press
1193
- Non kitchen products:
1194
- - Vacuum
1195
- - Television
1196
- Unavailable products:
1197
- - Spatula
1198
- ```
1199
-
1200
- ## remove
1201
-
1202
- ```
1203
- <string> | remove: <string>
1204
- ```
1205
-
1206
- Return the input with all occurrences of the argument string removed.
1207
-
1208
- ```liquid2
1209
- {{ "I strained to see the train through the rain" | remove: "rain" }}
1210
- ```
1211
-
1212
- ```plain title="output"
1213
- I sted to see the t through the
1214
- ```
1215
-
1216
- If either the filter input or argument are not a string, they will be coerced to a string before
1217
- substring removal.
1218
-
1219
- ## remove_first
1220
-
1221
- ```
1222
- <string> | remove_first: <string>
1223
- ```
1224
-
1225
- Return a copy of the input string with the first occurrence of the argument string removed.
1226
-
1227
- ```liquid2
1228
- {{ "I strained to see the train through the rain" | remove_first: "rain" }}
1229
- ```
1230
-
1231
- ```plain title="output"
1232
- I sted to see the train through the rain
1233
- ```
1234
-
1235
- If either the filter input or argument are not a string, they will be coerced to a string before substring removal.
1236
-
1237
- ## remove_last
1238
-
1239
- ```
1240
- <string> | remove_last: <string>
1241
- ```
1242
-
1243
- Return a copy of the input string with the last occurrence of the argument string removed.
1244
-
1245
- ```liquid2
1246
- {{ "I strained to see the train through the rain" | remove_last: "rain" }}
1247
- ```
1248
-
1249
- ```plain title="output"
1250
- I strained to see the train through the
1251
- ```
1252
-
1253
- If either the filter input or argument are not a string, they will be coerced to a string before substring removal.
1254
-
1255
- ## replace
1256
-
1257
- ```
1258
- <string> | replace: <string>[, <string>]
1259
- ```
1260
-
1261
- Return the input with all occurrences of the first argument replaced with the second argument. If
1262
- the second argument is omitted, it will default to an empty string, making `replace` behave like
1263
- `remove`.
1264
-
1265
- ```liquid2
1266
- {{ "Take my protein pills and put my helmet on" | replace: "my", "your" }}
1267
- ```
1268
-
1269
- ```plain title="output"
1270
- Take your protein pills and put your helmet on
1271
- ```
1272
-
1273
- If either the filter input or argument are not a string, they will be coerced to a string before
1274
- replacement.
1275
-
1276
- ## replace_first
1277
-
1278
- ```
1279
- <string> | replace_first: <string>[, <string>]
1280
- ```
1281
-
1282
- Return a copy of the input string with the first occurrence of the first argument replaced with the second argument. If the second argument is omitted, it will default to an empty string, making `replace_first` behave like `remove_first`.
1283
-
1284
- ```liquid2
1285
- {{ "Take my protein pills and put my helmet on" | replace_first: "my", "your" }}
1286
- ```
1287
-
1288
- ```plain title="output"
1289
- Take your protein pills and put my helmet on
1290
- ```
1291
-
1292
- If either the filter input or argument are not a string, they will be coerced to a string before replacement.
1293
-
1294
- ## replace_last
1295
-
1296
- ```
1297
- <string> | replace_last: <string>, <string>
1298
- ```
1299
-
1300
- Return a copy of the input string with the last occurrence of the first argument replaced with the second argument.
1301
-
1302
- ```liquid2
1303
- {{ "Take my protein pills and put my helmet on" | replace_first: "my", "your" }}
1304
- ```
1305
-
1306
- ```plain title="output"
1307
- Take my protein pills and put your helmet on
1308
- ```
1309
-
1310
- If either the filter input or argument are not a string, they will be coerced to a string before replacement.
1311
-
1312
- ## reverse
1313
-
1314
- ```
1315
- <array> | reverse
1316
- ```
1317
-
1318
- Return a copy of the input array with the items in reverse order. If the filter input is a string, `reverse` will return the string unchanged.
1319
-
1320
- ```liquid2
1321
- {% assign my_array = "apples, oranges, peaches, plums" | split: ", " -%}
1322
-
1323
- {{ my_array | reverse | join: ", " }}
1324
- ```
1325
-
1326
- ```plain title="output"
1327
- plums, peaches, oranges, apples
1328
- ```
1329
-
1330
- ## round
1331
-
1332
- ```
1333
- <number> | round[: <number>]
1334
- ```
1335
-
1336
- Return the input number rounded to the given number of decimal places. The number of digits defaults to `0`.
1337
-
1338
- ```liquid2
1339
- {{ 1.2 | round }}
1340
- {{ 2.7 | round }}
1341
- {{ 183.357 | round: 2 }}
1342
- ```
1343
-
1344
- ```plain title="output"
1345
- 1
1346
- 3
1347
- 183.36
1348
- ```
1349
-
1350
- If either the filter input or its optional argument are not an integer or float, they will be converted to an integer or float before rounding.
1351
-
1352
- ## rstrip
1353
-
1354
- ```
1355
- <string> | rstrip
1356
- ```
1357
-
1358
- Return the input string with all trailing whitespace removed. If the input is not a string, it will be converted to a string before stripping whitespace.
1359
-
1360
- ```liquid2
1361
- {{ " So much room for activities " | rstrip }}!
1362
- ```
1363
-
1364
- ```plain title="output"
1365
- So much room for activities!
1366
- ```
1367
-
1368
- ## safe
1369
-
1370
- ```
1371
- <string> | safe
1372
- ```
1373
-
1374
- Return the input string marked as safe to use in an HTML or XML document. If the filter input is not a string, it will be converted to an HTML-safe string.
1375
-
1376
- With auto-escape enabled and the following global variables:
1377
-
1378
- ```json title="data"
1379
- {
1380
- "username": "Sally",
1381
- "greeting": "</p><script>alert('XSS!');</script>"
1382
- }
1383
- ```
1384
-
1385
- ```liquid2 title="template"
1386
- <p>{{ greeting }}, {{ username }}</p>
1387
- <p>{{ greeting | safe }}, {{ username }}</p>
1388
- ```
1389
-
1390
- ```html title="output"
1391
- <p>&lt;/p&gt;&lt;script&gt;alert(&#34;XSS!&#34;);&lt;/script&gt;, Sally</p>
1392
- <p></p><script>alert('XSS!');</script>, Sally</p>
1393
- ```
1394
-
1395
- ## size
1396
-
1397
- ```
1398
- <object> | size
1399
- ```
1400
-
1401
- Return the size of the input object. Works on strings, arrays and hashes.
1402
-
1403
- ```liquid2
1404
- {{ "Ground control to Major Tom." | size }}
1405
- {{ "apples, oranges, peaches, plums" | split: ", " | size }}
1406
- ```
1407
-
1408
- ```plain title="output"
1409
- 28
1410
- 4
1411
- ```
1412
-
1413
- ## slice
1414
-
1415
- ```
1416
- <sequence> | slice: <int>[, <int>]
1417
- ```
1418
-
1419
- Return a substring or subsequence of the input string or array. The first argument is the zero-based start index. The second, optional argument is the length of the substring or sequence, which defaults to `1`.
1420
-
1421
- ```liquid2
1422
- {{ "Liquid" | slice: 0 }}
1423
- {{ "Liquid" | slice: 2 }}
1424
- {{ "Liquid" | slice: 2, 5 }}
1425
- {% assign beatles = "John, Paul, George, Ringo" | split: ", " -%}
1426
- {{ beatles | slice: 1, 2 | join: " " }}
1427
- ```
1428
-
1429
- ```plain title="output"
1430
- L
1431
- q
1432
- quid
1433
- Paul George
1434
- ```
1435
-
1436
- If the first argument is negative, the start index is counted from the end of the sequence.
1437
-
1438
- ```liquid2
1439
- {{ "Liquid" | slice: -3 }}
1440
- {{ "Liquid" | slice: -3, 2 }}
1441
- {% assign beatles = "John, Paul, George, Ringo" | split: ", " -%}
1442
- {{ beatles | slice: -2, 2 | join: " " }}
1443
- ```
1444
-
1445
- ```plain title="output"
1446
- u
1447
- ui
1448
- George Ringo
1449
- ```
1450
-
1451
- ## sort
1452
-
1453
- ````
1454
- <array> | sort[: <string>]
1455
- ``
1456
-
1457
- Return a copy of the input array with its elements sorted.
1458
-
1459
- ```liquid
1460
- {% assign my_array = "zebra, octopus, giraffe, Sally Snake" | split: ", " -%}
1461
- {{ my_array | sort | join: ", " }}
1462
- ````
1463
-
1464
- ```plain title="output"
1465
- Sally Snake, giraffe, octopus, zebra
1466
- ```
1467
-
1468
- The optional argument is a sort key. If given, it should be the name of a property and the filter's input should be an array of objects.
1469
-
1470
- ```json title="data"
1471
- {
1472
- "collection": {
1473
- "products": [
1474
- { "title": "A Shoe", "price": "9.95" },
1475
- { "title": "A Tie", "price": "0.50" },
1476
- { "title": "A Hat", "price": "2.50" }
1477
- ]
1478
- }
1479
- }
1480
- ```
1481
-
1482
- ```liquid2 title="template"
1483
- {% assign products_by_price = collection.products | sort: "price" -%}
1484
- {% for product in products_by_price %}
1485
- <h4>{{ product.title }}</h4>
1486
- {% endfor %}
1487
- ```
1488
-
1489
- ```plain title="output"
1490
- <h4>A Tie</h4>
1491
- <h4>A Hat</h4>
1492
- <h4>A Shoe</h4>
1493
- ```
1494
-
1495
- ## sort_natural
1496
-
1497
- ```
1498
- <array> | sort_natural[: <string>]
1499
- ```
1500
-
1501
- Return a copy of the input array with its elements sorted case-insensitively. Array items will be compared by their string representations, forced to lowercase.
1502
-
1503
- ```liquid2
1504
- {% assign my_array = "zebra, octopus, giraffe, Sally Snake" | split: ", " -%}
1505
- {{ my_array | sort_natural | join: ", " }}
1506
- ```
1507
-
1508
- ```plain title="output"
1509
- giraffe, octopus, Sally Snake, zebra
1510
- ```
1511
-
1512
- The optional argument is a sort key. If given, it should be the name of a property and the filter's input should be an array of objects. Array elements are compared using the lowercase string representation of that property.
1513
-
1514
- ```json title="data"
1515
- {
1516
- "collection": {
1517
- "products": [
1518
- { "title": "A Shoe", "company": "Cool Shoes" },
1519
- { "title": "A Tie", "company": "alpha Ties" },
1520
- { "title": "A Hat", "company": "Beta Hats" }
1521
- ]
1522
- }
1523
- }
1524
- ```
1525
-
1526
- ```liquid2 title="template"
1527
- {% assign products_by_company = collection.products | sort_natural: "company" %}
1528
- {% for product in products_by_company %}
1529
- <h4>{{ product.title }}</h4>
1530
- {% endfor %}
1531
- ```
1532
-
1533
- ```plain title="output"
1534
- <h4>A Tie</h4>
1535
- <h4>A Hat</h4>
1536
- <h4>A Shoe</h4>
1537
- ```
1538
-
1539
- ## sort_numeric
1540
-
1541
- TODO
1542
-
1543
- ## split
1544
-
1545
- ```
1546
- <string> | split: <string>
1547
- ```
1548
-
1549
- Return an array of strings that are the input string split on the filter's argument string.
1550
-
1551
- ```liquid2
1552
- {% assign beatles = "John, Paul, George, Ringo" | split: ", " -%}
1553
-
1554
- {% for member in beatles %}
1555
- {{- member }}
1556
- {% endfor %}
1557
- ```
1558
-
1559
- ```plain title="output"
1560
- John
1561
- Paul
1562
- George
1563
- Ringo
1564
- ```
1565
-
1566
- If the argument is undefined or an empty string, the input will be split at every character.
1567
-
1568
- ```liquid2
1569
- {{ "Hello there" | split: nosuchthing | join: "#" }}
1570
- ```
1571
-
1572
- ```plain title="output"
1573
- H#e#l#l#o# #t#h#e#r#e
1574
- ```
1575
-
1576
- ## squish
1577
-
1578
- ```
1579
- <string> | squish
1580
- ```
1581
-
1582
- Return the input string with all leading and trailing whitespace removed, and any other runs of whitespace replaced with a single space.
1583
-
1584
- ```liquid2
1585
- {{ " Hello, \n\t World! \r\n" | squish }}
1586
- ```
1587
-
1588
- ```plain title="output"
1589
- Hello, World!
1590
- ```
1591
-
1592
- ## strip
1593
-
1594
- ```
1595
- <string> | strip
1596
- ```
1597
-
1598
- Return the input string with all leading and trailing whitespace removed. If the input is not a string, it will be converted to a string before stripping whitespace.
1599
-
1600
- ```liquid2
1601
- {{ " So much room for activities " | strip }}!
1602
- ```
1603
-
1604
- ```plain title="output"
1605
- So much room for activities!
1606
- ```
1607
-
1608
- ## strip_html
1609
-
1610
- ```
1611
- <string> | strip_html
1612
- ```
1613
-
1614
- Return the input string with all HTML tags removed.
1615
-
1616
- ```liquid2
1617
- {{ "Have <em>you</em> read <strong>Ulysses</strong>?" | strip_html }}
1618
- ```
1619
-
1620
- ```plain title="output"
1621
- Have you read Ulysses?
1622
- ```
1623
-
1624
- ## strip_newlines
1625
-
1626
- ```
1627
- <string> | strip_newlines
1628
- ```
1629
-
1630
- Return the input string with `\n` and `\r\n` removed.
1631
-
1632
- ```liquid2
1633
- {% capture string_with_newlines %}
1634
- Hello
1635
- there
1636
- {% endcapture -%}
1637
-
1638
- {{ string_with_newlines | strip_newlines }}
1639
- ```
1640
-
1641
- ```plain title="output"
1642
- Hellothere
1643
- ```
1644
-
1645
- ## sum
1646
-
1647
- ```
1648
- <array> | sum[: <string>]
1649
- ```
1650
-
1651
- Return the sum of all numeric elements in an array.
1652
-
1653
- ```liquid2
1654
- {% assign array = '1,2,3' | split: ',' -%}
1655
- {{ array | sum }}
1656
- ```
1657
-
1658
- ```plain title="output"
1659
- 6
1660
- ```
1661
-
1662
- If the optional string argument is given, it is assumed that array items are hash/dict/mapping-like, and the argument should be the name of a property/key. The values at `array[property]` will be summed.
1663
-
1664
- ## take
1665
-
1666
- TODO
1667
-
1668
- ## times
1669
-
1670
- ```
1671
- <number> | times: <number>
1672
- ```
1673
-
1674
- Return the product of the input number and the argument. If either the input or argument are not a number, Liquid will try to convert them to a number. If that conversion fails, `0` is used instead.
1675
-
1676
- ```liquid2
1677
- {{ 3 | times: 2 }}
1678
- {{ "24" | times: "7" }}
1679
- {{ 183.357 | times: 12 }}
1680
- ```
1681
-
1682
- ```plain title="output"
1683
- 6
1684
- 168
1685
- 2200.284
1686
- ```
1687
-
1688
- ## truncate
1689
-
1690
- ```
1691
- <string> | truncate[: <integer>[, <string>]]
1692
- ```
1693
-
1694
- Return a truncated version of the input string. The first argument, length, defaults to `50`. The second argument defaults to an ellipsis (`...`).
1695
-
1696
- If the length of the input string is less than the given length (first argument), the input string will be truncated to `length` minus the length of the second argument, with the second argument appended.
1697
-
1698
- ```liquid2
1699
- {{ "Ground control to Major Tom." | truncate: 20 }}
1700
- {{ "Ground control to Major Tom." | truncate: 25, ", and so on" }}
1701
- {{ "Ground control to Major Tom." | truncate: 20, "" }}
1702
- ```
1703
-
1704
- ```plain title="output"
1705
- Ground control to...
1706
- Ground control, and so on
1707
- Ground control to Ma
1708
- ```
1709
-
1710
- ## truncatewords
1711
-
1712
- ```
1713
- <string> | truncatewords[: <integer>[, <string>]]
1714
- ```
1715
-
1716
- Return the input string truncated to the specified number of words, with the second argument appended. The number of words (first argument) defaults to `15`. The second argument defaults to an ellipsis (`...`).
1717
-
1718
- If the input string already has fewer than the given number of words, it is returned unchanged.
1719
-
1720
- ```liquid2
1721
- {{ "Ground control to Major Tom." | truncatewords: 3 }}
1722
- {{ "Ground control to Major Tom." | truncatewords: 3, "--" }}
1723
- {{ "Ground control to Major Tom." | truncatewords: 3, "" }}
1724
- ```
1725
-
1726
- ```plain title="output"
1727
- Ground control to...
1728
- Ground control to--
1729
- Ground control to
1730
- ```
1731
-
1732
- ## uniq
1733
-
1734
- ```
1735
- <array> | uniq[: <string>]
1736
- ```
1737
-
1738
- Return a copy of the input array with duplicate elements removed.
1739
-
1740
- ```liquid2
1741
- {% assign my_array = "ants, bugs, bees, bugs, ants" | split: ", " -%}
1742
- {{ my_array | uniq | join: ", " }}
1743
- ```
1744
-
1745
- ```plain title="output"
1746
- ants, bugs, bees
1747
- ```
1748
-
1749
- If an argument is given, it should be the name of a property and the filter's input should be an array of objects.
1750
-
1751
- ```json title="data"
1752
- {
1753
- "collection": {
1754
- "products": [
1755
- { "title": "A Shoe", "company": "Cool Shoes" },
1756
- { "title": "A Tie", "company": "alpha Ties" },
1757
- { "title": "Another Tie", "company": "alpha Ties" },
1758
- { "title": "A Hat", "company": "Beta Hats" }
1759
- ]
1760
- }
1761
- }
1762
- ```
1763
-
1764
- ```liquid2 title="template"
1765
- {% assign one_product_from_each_company = collections.products | uniq: "company" -%}
1766
- {% for product in one_product_from_each_company -%}
1767
- - product.title
1768
- {% endfor %}
1769
- ```
1770
-
1771
- ```plain title="output"
1772
- - A Shoe
1773
- - A Tie
1774
- - A Hat
1775
- ```
1776
-
1777
- ## upcase
1778
-
1779
- ```
1780
- <string> | upcase
1781
- ```
1782
-
1783
- Return the input string with all characters in uppercase.
1784
-
1785
- ```liquid2
1786
- {{ 'Hello, World!' | upcase }}
1787
- ```
1788
-
1789
- ```plain title="output"
1790
- HELLO, WORLD!
1791
- ```
1792
-
1793
- ## url_decode
1794
-
1795
- ```
1796
- <string> | url_decode
1797
- ```
1798
-
1799
- Return the input string with `%xx` escapes replaced with their single-character equivalents. Also replaces `'+'` with `' '`.
1800
-
1801
- ```liquid2
1802
- {{ "My+email+address+is+bob%40example.com%21" | url_decode }}
1803
- ```
1804
-
1805
- ```plain title="output"
1806
- My email address is bob@example.com!
1807
- ```
1808
-
1809
- ## url_encode
1810
-
1811
- ```
1812
- <string> | url_encode
1813
- ```
1814
-
1815
- Return the input string with URL reserved characters %-escaped. Also replaces `' '` with `'+'`.
1816
-
1817
- ```liquid2
1818
- {{ My email address is bob@example.com! | url_encode }}
1819
- ```
1820
-
1821
- ```plain title="output"
1822
- My+email+address+is+bob%40example.com%21
1823
- ```
1824
-
1825
- ## where
1826
-
1827
- ```
1828
- <array> | where: <string>[, <object>]
1829
- ```
1830
-
1831
- Return a copy of the input array including only those objects that have a property, named with the first argument, equal to a value, given as the second argument. If a second argument is not given, only elements with the named property that are truthy will be included.
1832
-
1833
- ```json title="data"
1834
- {
1835
- "products": [
1836
- { "title": "Vacuum", "type": "house", "available": true },
1837
- { "title": "Spatula", "type": "kitchen", "available": false },
1838
- { "title": "Television", "type": "lounge", "available": true },
1839
- { "title": "Garlic press", "type": "kitchen", "available": true }
1840
- ]
1841
- }
1842
- ```
1843
-
1844
- ```liquid2
1845
- All products:
1846
- {% for product in products -%}
1847
- - {{ product.title }}
1848
- {% endfor %}
1849
-
1850
- {%- assign kitchen_products = products | where: "type", "kitchen" -%}
1851
-
1852
- Kitchen products:
1853
- {% for product in kitchen_products -%}
1854
- - {{ product.title }}
1855
- {% endfor %}
1856
-
1857
- {%- assign available_products = products | where: "available" -%}
1858
-
1859
- Available products:
1860
- {% for product in available_products -%}
1861
- - {{ product.title }}
1862
- {% endfor %}
1863
- ```
1864
-
1865
- ```plain title="output"
1866
- All products:
1867
- - Vacuum
1868
- - Spatula
1869
- - Television
1870
- - Garlic press
1871
-
1872
- Kitchen products:
1873
- - Spatula
1874
- - Garlic press
1875
-
1876
- Available product:
1877
- - Vacuum
1878
- - Television
1879
- - Garlic press
1880
- ```
1881
-
1882
- ## zip
1883
-
1884
- TODO: