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> 

2 Answers 2

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} 
Sign up to request clarification or add additional context in comments.

2 Comments

Yes I did include my namespace. If I just write <car:CarInfo length='5' /> outside til alias-tag, I get the data as expected.
The alias mapped in my example should be cars: not car:
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> 

8 Comments

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.
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).
I have taken away the lenght part, because I am not using it in the first step. <f:debug title="Results car json">{car:CarInfo}</f:debug> then gives me this: '{car:CarInfo}' (13 chars)
I was wondering, can I use v:format.json.decode json=""? But how do I get my json URL into it?
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 <f:debug>{car:carInfo(length: 5)}</f:debug>.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.