TYPO3 / Fluid

Fluid template rendering engine - Standalone version
GNU Lesser General Public License v3.0
151 stars 93 forks source link

[FEATURE] JoinViewHelper #861

Closed benjaminkott closed 4 months ago

benjaminkott commented 5 months ago

The JoinViewHelper combines elements from an array into a single string. You can specify both a general separator and a special one for the last element that is rendered between the elements.

Examples

Simple join

<f:join value="{0: '1', 1: '2', 2: '3'}" />   

Results in the output:

123

Join with separator

<f:join value="{0: '1', 1: '2', 2: '3'}" separator=", " />

Results in the output:

1, 2, 3

Join with separator, and special one for the last

<f:join value="{0: '1', 1: '2', 2: '3'}" separator=", " separatorLast=" and " />

Results in the output:

1, 2 and 3
garvinhicking commented 5 months ago

Nice, I like that "separatorLast" argument!

nhovratov commented 4 months ago

It would be cool to have a property argument, so when you provide an array of objects/arrays like {data.categories} then you can map it to a specific property like title.

<f:split value="{data.categories}" property="title" separator=", "/>
s2b commented 4 months ago

There seem to be very different opinions about features like this (add to join VH vs. add a separate VH). I would suggest to add the VH as-is in the first step and to extend it afterwards if it's general consensus to go this route.

chrissonntag commented 4 months ago

Seems to be a useful ViewHelper and will be happy to use it. I'd rather have named it ImplodeViewHelper for that's what it is actually called in PHP, but that's just me.

Nevertheless I'd like to ask why the class has to be final? I've just checked and it seems that only a minority of ViewHelpers are declared final - for whatever reason even. It'd be quite convenient if we could extend this or any other ViewHelper for that matter.

mbrodala commented 4 months ago

Nevertheless I'd like to ask why the class has to be final? I've just checked and it seems that only a minority of ViewHelpers are declared final - for whatever reason even. It'd be quite convenient if we could extend this or any other ViewHelper for that matter.

See e.g. https://matthiasnoback.nl/2018/09/final-classes-by-default-why/

In a perfect Fluid world each viewhelper is tiny and does a single thing, thus is atomic. Extending these wouldn't make sense in the first place and would also lead to tight coupling which would make changes to Fluid itself harder.