3

Is it possible to create an array or an object with f:variable in TYPO3 9.x?

The example below does not work, because it treat the object as an string.

<f:variable name="person">{firstname:'Max', lastname:'Mustermann'}</f:variable>

Update: This example is working:

<f:variable name="person" value="{firstname:'Max', lastname:'Mustermann'}" />

Why is the tag syntax treaten differently?

What's interesting, is the fact that you can 'build' arrays in fluid and pass them as arguments to a partial.

Example:

<f:render partial="Components/ImageGallery" section="ImageGallery" arguments="{
  imageGallery: {
    0:{imageurl:'https://www.placehold.it/640x480',imagealt:'First Image', },
    1:{imageurl:'https://www.placehold.it/640x480',imagealt:'Second Alt'}
  }
}" />

It would be great, if it is possible in fluid to build this object with f:variable. Yes i know, i could assign this object, but that is not my intention.

bschauer
  • 958
  • 9
  • 33
kimomat
  • 2,211
  • 23
  • 37

1 Answers1

0

Here you have an example partial from https://github.com/Startpiloten/startpilot

<html xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers" data-namespace-typo3-fluid="true">

<f:if condition="{file}">
    <f:if condition="{breakpoints}">
        <f:else>
            <f:variable name="breakpoints" value="{settings.breakpoints}" />
        </f:else>
    </f:if>
    <f:link.typolink parameter="{file.link}" class="picturelink">
        <picture>
            <f:for each="{breakpoints}" as="breakpoint" iteration="i_breakpoints">
                <f:if condition="{i_breakpoints.isLast}">
                    <f:else>
                        <source srcset="{f:uri.image(image: file, maxWidth: breakpoint.maxWidth, cropVariant: breakpoint.cropVariant)}" media="({breakpoint.media}: {breakpoint.size}px)">
                    </f:else>
                    <f:then>
                        <source srcset="{f:uri.image(image: file, maxWidth: breakpoint.maxWidth, cropVariant: breakpoint.cropVariant)}" media="({breakpoint.media}: {breakpoint.size}px)">
                        <img class="img-fluid" src="{f:uri.image(image: file, maxWidth: breakpoint.maxWidth, cropVariant: breakpoint.cropVariant)}" alt="{file.alternative}" title="{file.title}">
                    </f:then>
                </f:if>
            </f:for>
        </picture>
    </f:link.typolink>
</f:if>

<f:comment>

    <!---Render Image with custom sizes from TS settings--->
    <f:render partial="ImageRender" arguments="{file:file, breakpoints:settings.breakpoints}" />

    <!---Render Image with custom sizes -- START --->
    <f:variable name="breakpoints" value="{
                            0:{media:'max-width', size:375, maxWidth:375, cropVariant:'mobile'},
                            1:{media:'max-width', size:480, maxWidth:480, cropVariant:'mobile'},
                            2:{media:'max-width', size:767, maxWidth:767, cropVariant:'tablet'},
                            3:{media:'max-width', size:991, maxWidth:991, cropVariant:'tablet'},
                            4:{media:'max-width', size:1279, maxWidth:1279, cropVariant:'default'},
                            5:{media:'max-width', size:1479, maxWidth:1479, cropVariant:'default'},
                            6:{media:'min-width', size:1480, maxWidth:2000, cropVariant:'default'}
                        }"/>

    <f:render partial="ImageRender" arguments="{file:image, breakpoints:breakpoints}" />
    <!---Render Image with custom sizes -- END --->

    <!---Render Image with default sizes -- START --->
    <f:render partial="ImageRender" arguments="{file:image}" />

</f:comment>

</html>
bschauer
  • 958
  • 9
  • 33
  • Thanks for the answer. The example shows an array with a fixed number of items. But how would you build this with a for loop? By the way an very interesting provider extension! – kimomat Feb 27 '19 at 14:01
  • Its in the same file - the variable is given into the partial (f:comment is only an example how to call it), and the `` renders the loop – bschauer Feb 27 '19 at 14:24
  • Maybe my question is written a bit confusing. i would like to build an dynamic array as f:variable inside the f:variable i need to loop an array and build a new array. In your example, the array already has 6 fixed items. – kimomat Feb 27 '19 at 14:31