Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] JoinViewHelper #861

Merged
merged 5 commits into from
Apr 4, 2024
Merged

Conversation

benjaminkott
Copy link
Member

@benjaminkott benjaminkott commented Mar 14, 2024

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

@benjaminkott benjaminkott force-pushed the feature/joinviewhelper branch 2 times, most recently from 1ef153f to cc010eb Compare March 14, 2024 15:10
.ddev/config.yaml Outdated Show resolved Hide resolved
@garvinhicking
Copy link
Contributor

Nice, I like that "separatorLast" argument!

src/ViewHelpers/JoinViewHelper.php Outdated Show resolved Hide resolved
src/ViewHelpers/JoinViewHelper.php Outdated Show resolved Hide resolved
src/ViewHelpers/JoinViewHelper.php Outdated Show resolved Hide resolved
src/ViewHelpers/JoinViewHelper.php Outdated Show resolved Hide resolved
@benjaminkott benjaminkott force-pushed the feature/joinviewhelper branch 3 times, most recently from b9ea6cd to cdc6998 Compare March 15, 2024 09:59
benjaminkott and others added 2 commits March 20, 2024 16:56
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.

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

Results in the output:

```html
123
```

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

Results in the output:

```html
1, 2, 3
```

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

Results in the output:

```html
1, 2 and 3
```
@nhovratov
Copy link

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
Copy link
Contributor

s2b commented Apr 2, 2024

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
Copy link

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
Copy link
Member

mbrodala commented Apr 3, 2024

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.

@s2b s2b merged commit 2038523 into TYPO3:main Apr 4, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants