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

Missing Functionality in Fluid - Collection #862

Open
benjaminkott opened this issue Mar 14, 2024 · 4 comments
Open

Missing Functionality in Fluid - Collection #862

benjaminkott opened this issue Mar 14, 2024 · 4 comments

Comments

@benjaminkott
Copy link
Member

benjaminkott commented Mar 14, 2024

Arrays

  • sort
  • ✅ reverse loop (implemented in for)
  • map
  • merge

Strings

  • length
  • substring rendering

Parent Rendering

I want to be able to render parts of an overwritten template.

<f:parent>

Conditions

  • strict comparisons
  • contains/includes

First / Last

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

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

Min / Max / Random

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

<f:max value="{0: '1', 1: '2', 2: '3'}" />
 <!-- 3 -->
 
<f:random value="{0: '1', 1: '2', 2: '3'}" />
<!-- 1|2|3 -->

Round

  • precision: decimals
  • method: direction
    • common
    • ceil
    • floor
<f:round value="15.341" precision="2" method="ceil" />
<!-- 15.35 -->

Alternative: Dedicated ViewHelpers for each.

Replace #863

Replace a string, with another one.

<f:replace value="HELLO###SPACE###FRIENDS" pattern="###SPACE###" replacement=" " />
<!-- HELLO FRIENDS -->
@sandoba
Copy link

sandoba commented Mar 14, 2024

Definitely would love to see the remaining functionality of Twig in Fluid.
Especially something like <f:parent> would be helpful, when only minor changes of e.g. a section are required. Or perhaps with a change of <f:render>:

<f:render partial="ExamplePartial" arguments="{_all}"> ... <f:parent /> ... </f:render>

So changes could be made within the scope of the rendered partial or section.

But if it is meant for a template then this would be more akin to an include? E.g.

<f:include template="ExampleTemplate" arguments="{_all}"> ... <f:parent /> ... </f:include>

Could of course also be called <f:render template="ExampleTemplate" ....

@s2b
Copy link
Contributor

s2b commented Mar 14, 2024

Thank you for your feedback! I just want to leave a few comments here. I think, once we've accomplished some of the quick wins (ViewHelpers), we can close this issue and open separate ones for the bigger feature requests.

Arrays

  • reverse loop

This should already be possible:

<f:for each="{myArray}" as="item" reverse="1">
  • map

Just want to leave that here, since it's buried in a review:

I don't think a clean solution like this is possible without syntax changes. Something like this could be possible though:

{csv -> f:split(separator: ',') -> f:map(callback: 'f:trim')}

Strings

  • substring rendering

Do you mean with numbers, like substr($string, 1, 5)?

Parent Rendering

I want to be able to render parts of an overwritten template.

<f:parent>

Conditions

  • strict comparisons
  • contains/includes

Probably a bit more tough to implement, but valid.

Round

Alternative: Dedicated ViewHelpers for each.

I think that 3 ViewHelpers make more sense here to avoid the string constants.

Replace

Replace a string, with another one.

<f:replace value="HELLO###SPACE###FRIENDS" pattern="###SPACE###" replacement=" " />
<!-- HELLO FRIENDS -->

Can we use search and replace instead (like str_replace does)? I find them easier to understand, also search isn't a regular expression here.

It would also be nice to provide one array with key-value pairs (which str_replace() unfortunately doesn't allow). Maybe support these variants:

<f:replace value="HELLO###SPACE###FRIENDS" search="###SPACE###" replace=" " />
<f:replace value="HELLO###SPACE###FRIENDS" search="{0: '###SPACE###'}" replace="{0: ' '}" />
<f:replace value="HELLO###SPACE###FRIENDS" replace="{'###SPACE###': ' '}" />

@benjaminkott
Copy link
Member Author

benjaminkott commented Mar 15, 2024

@s2b Maybe this could be used as a starting point for a parent view helper
benjaminkott/bootstrap_package@82a837b
The main problem is that the context is missing when rendering the current template. So guessing/config is needed.

Do you mean with numbers, like substr($string, 1, 5)?
Yes, @NeoBlack is a big fan of that.

  • I am generally fine with adjustments for namings, just wanted to give examples.
  • I checked the reverse, simply overlooked it

@mkroener
Copy link

mkroener commented Apr 11, 2024

Currently, there are contexts such as BackendPreviews or fluid templates in content blocks where preparing data is challenging, and no view helpers are available for a straightforward approach. I've seen solutions where developers crafted a workaround by looping with <f:variable> and self-rendering sections.

Being able to repeat a snippet multiple times would be a valuable initial capability.

<f:repeat times="{slotCount}">
    <div class="item-slot"></div>
</f:repeat>

Its easy to do quick and dirty way:

namespace Your\Extension\ViewHelpers;

use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;

class RepeatViewHelper extends AbstractViewHelper {

    public function initializeArguments() {
        $this->registerArgument('times', 'int', 'How many times to repeat the content', true);
    }

    public function render() {
        $times = $this->arguments['times'];
        $output = '';
        for ($i = 0; $i < $times; $i++) {
            $output .= $this->renderChildren();
        }
        return $output;
    }
}

But it would be great to have as a standard viewhelper

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

No branches or pull requests

4 participants