Black
Black

Reputation: 20212

check if variable is of type array in fluid

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?

Upvotes: 3

Views: 6216

Answers (2)

Black
Black

Reputation: 20212

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>

Upvotes: 3

Jost
Jost

Reputation: 5840

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>

Upvotes: 6

Related Questions