Radical Design Course by Jack McDade

From the creator of Statamic

Learn how to make your websites standout and be remembered.

Taking your approach on designing things actually makes it fun, more natural, and overall easier.

— Dominik, Developer

contains Modifier

Check if a value contains another value. Supports both strings and arrays.

Returns true if a match is found, otherwise false.

The first parameter is the "needle" to find in the "haystack". It will read from the context if there is a matching variable, otherwise it will use the parameter as the value.

Strings

Case-insensitive by default but can be made sensitive by setting the second parameter to true.

summary: "It was the best of times, it was the worst of times."
adjective: best
noun: carrot
{{ if summary | contains('BEST') }}
{{ if summary | contains('BEST', true) }}
{{ if summary | contains('adjective') }}
{{ if summary | contains('noun') }}
@if (Statamic::modify($summary)->contains('BEST')->fetch()) ... @endif
@if (Statamic::modify($summary)->contains(['BEST', true])->fetch()) ... @endif
@if (Statamic::modify($summary)->contains('adjective')->fetch()) ... @endif
@if (Statamic::modify($summary)->contains('noun')->fetch()) ... @endif
true (the substring "BEST" was in the string, and it didn't care about the case.)
false (the substring "BEST" was in the string, however it didn't match the case.)
true (there's a field named "adjective", and it got the value which was "best")
false (there's a field named "noun", and it got the value which was "carrot")

Arrays

You can set strict type checking by setting the second parameter to true.

foods:
- bacon
- bread
- tomato
delicious: bacon
gross: broccoli
 
numbers: [1, 2]
number: '1'
{{ if foods | contains('bacon') }}
{{ if foods | contains('delicious') }}
{{ if foods | contains('gross') }}
{{ if (foods | contains('vegan bacon strips')) }}
 
{{ if numbers | contains(number) }}
{{ if numbers | contains(number, true) }}
@if (Statamic::modify($foods)->contains('bacon')->fetch()) ... @endif
@if (Statamic::modify($foods)->contains('delicious')->fetch()) ... @endif
@if (Statamic::modify($foods)->contains('gross')->fetch()) ... @endif
@if (Statamic::modify($foods)->contains('vegan bacon strips')->fetch()) ... @endif
 
@if (Statamic::modify($foods)->contains($number)->fetch()) ... @endif
@if (Statamic::modify($foods)->contains([$number, true])->fetch()) ... @endif
true (there's no field named "bacon", so it searched for literally "bacon")
true (there's a field named "delicious", and it got the value which was "bacon")
false (there's a field named "gross", and it got the value which was "broccoli")
true (there's no field named "vegan bacon strips", so it searched the expression for a literal string "vegan bacon strips")
 
true (the value of "number" is the string "1", which is fine in non-strict mode)
false (with strict mode enabled, the string "1" won't match the integer)
Docs feedback

Submit improvements, related content, or suggestions through Github.

Betterify this page →