TYPO3 - Fluid returns the string “array”

2019-09-16 03:55发布

问题:

I have this in my template:

<v:iterator.explode content="<f:format.nl2br>{artnumbers.qualitynumber.certificates}</f:format.nl2br>" glue="<br />" as="expCertificates">


if artnumbers.qualitynumber.certificates is empty, it returns teh string "array".

Is that a bug?

回答1:

You are using the ViewHelper. This ViewHelper replaces newlines in a string (!) with
tags. You cannot use it for an array and that's why it returns "array".

I don't know why you need the explode viewHelper. Why don't you just iterate through your certificates?

<f:for each="{artnumbers.qualitynumber.certificates}" as="certificate">
    {certificate}<br />
</f:for>

EDIT: If certificates is a string containing newlines, use the newline as glue for explode:

<v:iterator.explode content="{artnumbers.qualitynumber.certificates}" glue="\n" as="expCertificates">

You could also try to use CONSTANT:LF in glue as suggested in the documentation of the ViewHelper: https://fedext.net/viewhelpers/vhs/master/Iterator/ExplodeViewHelper.html



回答2:

v:iterator.explode when used with the as argument implies the variable assigned to as is only available inside the tag content:

<v:iterator.explode content="1,2,3" as="numbers"> {numbers} is an array </v:iterator.explode> {numbers} is no longer defined.

This behaviour changed from VHS 1.7 to 1.8 (from memory).

Alternatively, do:

{artnumbers.qualitynumber.certificates -> f:format.nl2br() -> v:iterator.explode(glue: '<br />') -> v:var.set(name: 'extractedCertificates')} <f:for each="{extractedCertificates}" as="certificate"> {certificate} </f:for>

Or better, but assumes your lines are ONLY separated by a single line break:

{artnumbers.qualitynumber.certificates -> v:iterator.explode(glue: 'constant:LF') -> v:var.set(name: 'extractedCertificates')} <f:for each="{extractedCertificates}" as="certificate"> {certificate} </f:for>

Which of course lets you skip the nl2br step.

Even more compacted:

<f:for each="{artnumbers.qualitynumber.certificates -> v:iterator.explode(glue: 'constant:LF')}" as="certificate"> {certificate} </f:for>



标签: typo3 fluid