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?
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?
Share Improve this question edited Jan 10, 2013 at 11:53 freshp asked Jan 9, 2013 at 11:21 freshpfreshp 5245 silver badges21 bronze badges 1- Just disable quoting in template engine – sbmaxx Commented Jan 9, 2013 at 11:30
3 Answers
Reset to default 6Use <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.
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?
vhs has format.json.encode
:
<script type="text/javascript">
var title = {v:format.json.encode(value: branch.name)};
//.. do something
</script>
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744940661a4602301.html
评论列表(0条)