Parsing an existing JSON string in a fluid template?

I have a JSON string with some data that I want to display in a template. Since JSON also mentions Fluid arrays, I thought I could just take this JSON string and pass it to the liquid, telling her to treat it the same way as some other array, and use it in the template.

Thus, getting more speed and losing overhead (you do not need to separate JSON data to save it in the database, you can easily smooth it out in the liquid).

That would not work, at least not in the way I tried.

<f:alias map="{item.jsonData}"> {fieldname} </f:alias> 

He, of course, complained that he received a string, not an array.

Should I create a viewhelper and make json_decode before returning the array to the liquid? Or is there a more native way?

Here's the main action of the controller:

 /** * 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); } 
0
fluid typo3 extbase
source share
4 answers

As an alternative to using a custom ViewHelper, you can use the transient property in your model. Suppose your model has a jsonData property, which is a JSON encoded string.

Now you add another $ jsonArray property and getter for it:

 /** * @var array * @transient */ protected $jsonArray; 

And in the receiver you decode the data:

 /** * @return array */ public function getJsonArray() { return json_decode($this->jsonData); } 

The transient property is similar to a virtual property. You do not need a DB field and a TCA definition for it, and you cannot make queries based on this, but you have data available in your object:

 <f:for each="{item.jsonArray}" as="value"> {value} </f:for> 
+5
source share

Yes, you need to use your own viewhelper or decode the JSON string in the controller (I prefer the latter), depending on what is more convenient for you.

Cannot decode JSON in Fluid, sorry

+1
source share

In autonomous fluid and TYPO3v8 and up:

 $this->view->assign('json', new \TYPO3Fluid\Fluid\Variables\JSONVariableProvider('path/to/my/fileOrUrl.json')); // then in Fluid: {json.any.path.inside.jsonfile} 

See also ChainedVariableProvider , which allows you to use, for example, a JSON file as base variables and variables from another array to overlap them. Using this VariableProvider causes Fluid to first look for a variable (non-NULL) in a regular array, then a JSON file (or vice versa if you order it this way).

+1
source share
 <script type="text/javascript"> var json = '{f:format.htmlentitiesDecode(value:your_value)}'; var your_value = jQuery.parseJSON(json); </script> 
0
source share

All Articles