Reputation: 524
I was trying to get a json in an javascript variable. I use fluid-templates in my extbase typo3-extension. In my action I load some json with curl. This json I assign to the template. In my template it looks like:
<script type="text/javascript">
var json = {jsonVarFromControllerAction};
</script>
In this case the json is interpreted as html-code. It looks like:
{"content":"testcontent"}
In the controller-action its an correct json!
{"content": "testcontent"}
Whats the solution?
Upvotes: 3
Views: 3935
Reputation: 31147
vhs has format.json.encode
:
<script type="text/javascript">
var title = {v:format.json.encode(value: branch.name)};
//.. do something
</script>
Upvotes: 0
Reputation: 524
I wrote my own viewhelper. This viewhelper only gets the json-content and html_entity_decode it.
public function render($json)
{
return html_entity_decode($json);
}
It works well, but i ask myself, why i should write an helper to get the pure content of my own variables?
Upvotes: 0
Reputation: 55798
Use <f:format.htmlentitiesDecode>
ViewHelper to decode that, for an example:
<script type="text/javascript">
var json = <f:format.htmlentitiesDecode>{jsonVarFromControllerAction}</f:format.htmlentitiesDecode>;
</script>
You can browse all available ViewHelpers in typo3/sysext/fluid/Classes/ViewHelpers
Other option is getting JSON formatted with PHP directly from the action with AJAX (without passing it to the view) it's useful if you want to fetch new data without refreshing the whole page.
Upvotes: 6