0

I have a DCE content element and now I need to add a json object inside this template with the text from the variables.

I tried many ways like

<f:format.raw>{</f:format.raw>

or

<f:format.alias map="{l: '{}', r: '}'}"> { json } </f:format.alias>

but nothing works.

<dce:format.WrapWithCurlyBraces>json</dce:format.WrapWithCurlyBraces>

was my last test but this generates an empty output. Is there any way to output some json with the variables from the dce inside?

<f:format.json>{"context": "https://schema.org/"}</f:format.json>

also tested, generates also an empty output.

DCE Version: 2.6.0
TYPO3: 9.5

Oliver Hader
  • 4,093
  • 1
  • 25
  • 47

2 Answers2

1

<f:format.raw>{</f:format.raw> should work. If not, you can also try <f:format.raw value="{" />. For example:

<f:format.raw value="{" />json<f:format.raw value="}" />
Rudy Gnodde
  • 4,286
  • 2
  • 12
  • 34
  • that don't work too, my test line: ```````` the output is only: ```````` with: ````<![CDATA[{]]>"@context" : "https://schema.org/"<![CDATA[{]]>```` the output is: ```````` – user3852729 Jan 07 '21 at 09:10
  • You're right. That doesn't work since TYPO3 8. I've updated my answer with a solution that should work with newer TYPO3 versions. – Rudy Gnodde Jan 07 '21 at 09:33
0

You don't need that many workarounds ;) You just used the JSON-ViewHelper incorrectly. It expects a (Fluid)-array as input.

<f:format.json value="{context: 'https://schema.org/'}" />

Since the " in the JSON will get escaped before being output to HTML we just use f:format:raw on the whole string.

<f:format.raw><f:format.json value="{context: 'https://schema.org/'}" /></f:format.raw>

Or, probably nicer:

<f:variable name="jsonData" value="{context: 'https://schema.org/'}" />
{jsonData -> f:format.json() -> f:format.raw()}

That's equal to:

<f:variable name="jsonData" value="{context: 'https://schema.org/'}" />
<f:format.raw><f:format.json>{jsonData}</f:format.json></f:format.raw>

(In your case I think you have jsonData already as the variable json, don't you?)

Jonas Eberle
  • 2,835
  • 1
  • 15
  • 25