1

How can I use data coming from a custom ViewHelper in my Fluid Template.

I have made a custom ViewHelper that is returning data like this:

{cars: {
0: {car:'VW Golf V TDi 140 GT Sport DSG Van',price:'144900'},
1: {car:'Citroën C5 HDi Elegance',price:'168900'},
2: {car:'Seat Leon TSi Stylance',price:'173000'}
}}

And tried the code below, but it doesn't print anything inf the f:for loop. In FLuid I would like to run through this with a f:for loop. But how do I get my data into the f:for syntax?

<f:alias map="<car:CarInfo length="5" />">
  <table cellpadding="5" cellspacing="0" border="2">
    <f:for each="{cars}" as="car">
      <tr>
        <td>{car.car}</td>
        <td>{car.price}</td>
      </tr>
    </f:for>
  </table>
</f:alias>
andreas
  • 16,357
  • 12
  • 72
  • 76
Jeppe Donslund
  • 469
  • 9
  • 25

2 Answers2

1

your <f:alias code is broken. you need to use inline annotation, since you want to access the viewhelper inside an other viewhelper https://wiki.typo3.org/Fluid_Inline_Notation

<f:alias map="{cars: '{car:CarInfo(length:\'5\')}'}">

Also did you include your namespace like this?

{namespace car=Vendor\Extension\ViewHelpers}
Dimitri L.
  • 4,499
  • 1
  • 15
  • 19
0

Does your ViewHelper return an array or a JSON string? If this is a JSON string you cannot consume variables from it without the use of a third party ViewHelper or JSONVariableProvider (Fluid standalone and TYPO3v8+).

See: Parse an existing JSON string in fluid template?

If your ViewHelper does indeed return an array then the most efficient way to solve your use case is:

<f:for each="{car:carInfo(length: 5)}" as="car">...</f:for>
Community
  • 1
  • 1
Claus Due
  • 4,166
  • 11
  • 23
  • Actually I get an error rigth now saying that Fluid need and arrow but is getting af string, så I think my ViewHelper returns a string. – Jeppe Donslund Sep 03 '16 at 13:40
  • This can also indicate a syntax error if you use inline syntax. Any expression that is not valid syntax will be passed as a string containing Fluid code. This is the first thing you should check, after that you probably should make sure your ViewHelper *does* return an array. You may use `f:debug` to assist (just put the inline piece in an `f:debug` and if that shows Fluid code you've got a syntax error). – Claus Due Sep 03 '16 at 13:42
  • I have taken away the lenght part, because I am not using it in the first step. {car:CarInfo} then gives me this: '{car:CarInfo}' (13 chars) – Jeppe Donslund Sep 04 '16 at 17:59
  • I was wondering, can I use v:format.json.decode json=""? But how do I get my json URL into it? – Jeppe Donslund Sep 04 '16 at 20:23
  • Your debug statement gives you the source code because Fluid will not allow arrays to be created this way when you are outside a ViewHelper tag argument. This was done in order to make it a lot less problematic to write things like JSON in Fluid templates. Debug with `{car:carInfo(length: 5)}`. – Claus Due Sep 04 '16 at 21:05
  • You can use `{car:carInfo(length: 5) -> v:format.json.decode()}` if for some reason you want to preserve that your ViewHelper outputs JSON but you want to iterate over that in Fluid. – Claus Due Sep 04 '16 at 21:06
  • This code {car:carInfo(length:5)} now gives this: array(2 items) ApiVersion => '1.3' (3 chars) Vehicles => array(8 items) But still gives "The argument "map" was registered with type "array", but is of type "string"" Is it still syntax error? – Jeppe Donslund Sep 04 '16 at 21:58
  • ``. I mean no offence with this but you would have a much easier time with Fluid if you study a few example templates with some complex syntax - or if you consume such information better, the official Fluid documentation about inline syntax, in particular about escaping quotes. Lastly you may find my "Mastering Fluid" videos informative as they cover things like these: https://vimeo.com/user20720051/videos. Hope this helps! – Claus Due Sep 04 '16 at 22:06