I have a JSON string with some data I want to render in a template. As Fluid arrays are notated in JSON too, I thought I might just take that JSON string and hand it over to fluid, telling it to treat it just like some other array and use it in the template.
Thus gaining a lot of speed and losing overhead (don't have to split the JSON data up to save it in the DB, can template it easily in fluid).
It wouldn't work, at least not how I tried it.
<f:alias map="{item.jsonData}">
{fieldname}
</f:alias>
It - of course - complained it had received a string, not an array.
Do I have to build a viewhelper and do json_decode
before returning the array to fluid? Or is there a more native way?
Here's the basic controller action:
/**
* action show
*
* @param \NAMESPACE\Myext\Domain\Model\Item $item
* @return void
*/
public function showAction(\NAMESPACE\Myext\Domain\Model\Item $item) {
$this->view->assign('item', $item);
}
Yes you need to use own viewhelper or decode your JSON string in the controller (I prefer the last), depends which is more comfortable for you.
There is no way to decode JSON in Fluid, sorry
As an alternative to using a custom ViewHelper, you could use a transient property in your model. Let's assume your model has a property "jsonData" that is a JSON encoded string.
Now, you add another property $jsonArray and a getter for it:
And in the getter, you decode the data:
A transient property is like a virtual property. You don't need a DB field and TCA definition for it and you cannot do queries based on it, but you have the data available in your object:
In Fluid standalone and TYPO3v8 and up:
See also the
ChainedVariableProvider
which will allow you to use for example a JSON file as base variables and variables from another array to overlay those. Using this VariableProvider causes Fluid to look for a (non-NULL) variable in the normal array first, then the JSON file (or vice versa if you order it thusly).