Bug #50555

Can't get an empty array in if condition

Added by Thomas Christiansen over 8 years ago. Updated almost 5 years ago.

Status:
Closed
Priority:
Should have
Category:
-
Target version:
-
Start date:
2013-07-30
Due date:
% Done:

0%

Estimated time:
Has patch:
No

Description

Hi all,
i am having trouble to get an empty array in a shorthand if condition.

The situation is the following:
I have an Input field which can be required or optional, depending on a checkbox in Backend.

In my Template i tried this;

additionalAttributes="{f:if(condition: '{field.mandatory}', then: '{required: 1}', else: '{}')}"

The "then"-case works fine, i am getting the attribute required="1" as expected.

The problem is, that the '{}' in the else-case returns an empty string, not an empty array. Then i get an exception, because additionalAttributes expects an array.
I tried a couple of things to get this to work like {0: null}, {\'\'} or {null} but i always get either an empty string or an non-empty array like array(0 => null).
The last one is not an option because it would result in an attribute "0" in the html tag.

Me and my colleagues came up with a view workarrounds:

1) Assign an empty array to a variable in the controller and use this variable in the template.
- This is in my case not an option because i am extending an TER extension and dont want to override its controller.

2) Add an "Empty-Array-Getter" to the Model like getEmptyArray which returns an empty array.
- This is a possibility, because i am extending the model as well, but... urghs ;)

3) Write a viewhelper that adds an empty array to the templateVariableContainer.
- This is probably the best workaround and i am using this for now.

The viewhelper looks simply like this:

class EmptyArrayViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
public function render() {
$this->templateVariableContainer->add('emptyArray', array());
return NULL;
}
}

So i use can {emptyArray} in my template now after calling the viewhelper:
<fx:EmptyArray />
...
additionalAttributes="{f:if(condition: '{field.mandatory}', then: '{required: 1}', else: '{emptyArray}')}"

Did we miss anything here or is there no other way to solve this scenario?

Thanks and regards,
Thomas

#1

Updated by Bastian Waidelich over 8 years ago

  • Status changed from New to Needs Feedback
  • Assignee set to Bastian Waidelich

What about

<f:if condition="{field.mandatory}">
  <f:then>
    <f:form.checkbox property="foo" additionalAttributes="{required: true}" />
  </f:then>
  <f:else>
    <f:form.checkbox property="foo" />
  </f:else>
</f:if>

If you need it more than once you could put that code into a partial and use it like this:

  <f:render partial="checkbox" arguments="{requiredCondition: field.mandatory}" />

(and then change {field.mandatory} to {requiredCondition} in the partial)

#2

Updated by Thomas Christiansen over 8 years ago

Yes sure, that is another workaround.

Actually this is where I started and what i did not want because its annoying to maintain.

Thanks for your reply but i am going with the viewhelper solution.

Regards,
Thomas

#3

Updated by Bastian Waidelich over 8 years ago

  • Status changed from Needs Feedback to Closed

Thomas Christiansen wrote:

Thanks for your reply but i am going with the viewhelper solution.

Ok, I'm closing the issue for now because that is actually a missing feature that would be quite hard to implement without huge benefit.

Some background information:
The Fluid argument notation is a fully compatible subset of the JavaScript Object Notation (JSON). For an empty array we'd need to add support for a new symbol:

[]

This would increase complexity and create conflicts with existing Fluid templates.

Feel free to add comments or re-open if you don't agree

#4

Updated by Romain Canon almost 6 years ago

Hi, I just came in the exact same issue. This is really sad Fluid is still not able to handle this kind of stuff.

+1 for the idea just above of adding support for the "[]" notation.

#5

Updated by Vitoandre D'Oria almost 5 years ago

It seems like duplicating the content to keep the source code slim isn't the right or logical answer imho...

Doing somethings like additionalAttributes plus f:if(condition requires right now a empty array variable assigned to our view as workaround.

+1

Also available in: Atom PDF