Skip to content

Conversation

@staatzstreich
Copy link
Contributor

@staatzstreich staatzstreich commented Aug 14, 2025

This patch adds a new <f:contains> ViewHelper to Fluid, which allows
to check if a provided string or array contains a specified value.
Depending on the input, this mimicks PHP's in_array() or str_contains().

The initial version of this patch is intended to be backported to Fluid 4.
In a follow-up, the code and argument types can be improved for Fluid 5.

Resolves: #1125

@mbrodala
Copy link
Contributor

mbrodala commented Aug 14, 2025

I wonder if we should consider extending conditions instead, e.g. using Symfony EL:

{myString contains "foo"}

Then we'd not need to add a viewhelper for each and every bit of functionality.

@s2b
Copy link
Contributor

s2b commented Aug 17, 2025

@mbrodala I agree that we should improve conditions overall. However, in the short term it's probably wise to add ViewHelpers for the most common use cases nonetheless. Also, the following syntax is already possible:

<f:if condition="{f:contains(value: 'foo', array: myArray)} && ...">

@s2b
Copy link
Contributor

s2b commented Aug 19, 2025

The more I think about it, the more I struggle with the VH API. I'm open to other suggestions, but these come to mind:

  • Allow both array and string as one argument and differentiate in the VH based on the type
  • Split into two: f:contains for strings and f:in for arrays

What do you think?

@s2b s2b added the backport label Sep 16, 2025
@brotkrueml
Copy link
Contributor

  • Split into two: f:contains for strings and f:in for arrays

IMHO there should be two dedicated view helpers as you suggested to handle the different types. Additionally, for an f:in view helper (better f:inArray as the purpose is clearer) we should think about the possibility for checking the type (like in_array in PHP with the "strict" argument):

<f:inArray value="some-value" array="{someArray}" strict="{true}">

With "strict" be false by default (like in PHP). Strictness might be difficult when setting a value directly in Fluid:

<f:inArray value="42" array="{someArray}" strict="{true}">

Here the value is a string while the array values might be ints. But that would work:

<f:inArray value="{someValue}" array="{someArray}" strict="{true}">

if {someValue} is passed to Fluid and is really an int.

@s2b
Copy link
Contributor

s2b commented Nov 4, 2025

Thank you for your input here! We discussed this again and we came to the conclusion that we will have one <f:contains> ViewHelper, although slightly differently than it's implemented here:

{myArray -> f:contains(value: 'foo')}
{f:contains(subject: myArray, value: 'foo')}
{myString -> f:contains(value: 'foo')}
{f:contains(subject: myString, value: 'foo')}

So the subject will support both string and array (+ array-like objects). I get the arguments for type strictness, but in my opinion, convenience and consistent naming is more important in a template engine.

I'll adjust the implementation accordingly in the next few days.

@s2b s2b force-pushed the feature/contains-viewhelper branch 4 times, most recently from 3d6a335 to 9a42ac3 Compare November 8, 2025 10:13
@s2b s2b force-pushed the feature/contains-viewhelper branch from 9a42ac3 to 0e3d357 Compare November 8, 2025 10:22
@s2b
Copy link
Contributor

s2b commented Nov 8, 2025

I've adjusted the ViewHelper and documentation. WDYT?

@s2b s2b changed the title [FEATURE] A new <f:contains> ViewHelper should be added to Fluid (#1125) [FEATURE] <f:contains> ViewHelper Nov 8, 2025
@s2b s2b merged commit a834143 into TYPO3:main Nov 10, 2025
5 checks passed
s2b pushed a commit that referenced this pull request Nov 10, 2025
This patch adds a new `<f:contains>` ViewHelper to Fluid, which allows
to check if a provided string or array contains a specified value.
Depending on the input, this mimicks PHP's `in_array()` or `str_contains()`.

The initial version of this patch is intended to be backported to Fluid 4.
In a follow-up, the code and argument types can be improved for Fluid 5.

Resolves: #1125
s2b added a commit that referenced this pull request Nov 10, 2025
This patch adds a new `<f:contains>` ViewHelper to Fluid, which allows
to check if a provided string or array contains a specified value.
Depending on the input, this mimicks PHP's `in_array()` or `str_contains()`.

The initial version of this patch is intended to be backported to Fluid 4.
In a follow-up, the code and argument types can be improved for Fluid 5.

Resolves: #1125

Co-authored-by: Michael Staatz <michael@staatzstreich.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

f:contains ViewHelper

5 participants