3

Is it possible in fluid to check if the type of a variable is an array? I search for something like this.

<f:if condition='{myvar.Type == "Array"}'></f:if>

Or do I have to create my own ViewHelper for this purpose?

Black
  • 18,150
  • 39
  • 158
  • 271

2 Answers2

6

You have to either create your own ViewHelper, or use the existing one from EXT:vhs.

It works similar to the f:if ViewHelper:

<v:condition.type.isArray value="{myvar}">
    <f:then>
        ...
    </f:then>
    <f:else>
        ...
    </f:else>
</v:condition.type.isArray>
Jost
  • 5,948
  • 8
  • 42
  • 72
  • Then i have to write my own viewhelper i think, i get "your php version is higher than allowed" if i try to install the extension "VHS: Fluid Viewhelpers" – Black Jul 18 '16 at 12:28
  • Which version of TYPO3 are you using? – Jost Jul 18 '16 at 12:49
  • 1
    Which version of TYPO3 are you using? If 6.2.x, you should not use PHP 7 (it's officially compatible to PHP 5.3.7 up to 5.6), and if 7.6.x, VHS should be available in version 3.0.0, which supports PHP 7.0.x. – Jost Jul 18 '16 at 12:51
  • Sorry, SO had some hickups, that doubled the comment. – Jost Jul 18 '16 at 12:52
  • I am using Typo3 7.6.9, thanks i will search for v3! – Black Jul 18 '16 at 13:31
  • I need to call it as inline like `` if I want to use and/or – phse Jul 27 '18 at 09:46
3

I solved it by writing my own ViewHelper

class TestViewHelper extends AbstractViewHelper 
{
    /**
    * Arguments Initialization
    */
    public function initializeArguments()
    {
        $this->registerArgument('myvar', 'string', 'test', TRUE);
    }

    /**
    * @return integer test
    */
    public function render() 
    {

        $arg      = $this->arguments['myvar'];
        $argType  = gettype($arg);
​
        if (preg_match("/array/i", "$argType")) {
            return 1;    //match
        } else {
            return 0;    //No match
        }
    }
}

Usage:

{namespace mynamespace=YOUR_EXTENSION_NAME\YOUR_VENDOR_NAME\ViewHelpers}

<f:if condition="<mynamespace:isarray myvar='{_all}'/>==1">
    <f:then>
        <strong>_all is an Array</strong><br>
    </f:then>
    <f:else>
        <strong>_all is not an Array</strong><br>
    </f:else>
</f:if>
Black
  • 18,150
  • 39
  • 158
  • 271
  • 2
    You could use `return is_array($this->arguments['myvar']);`, no need for doing regex magic here. – Jost Jul 19 '16 at 21:43