Json to javascript in fluid-templates with extbase - Stack Overflow

I was trying to get a json in an javascript variable. I use fluid-templates in my extbase typo3-extensi

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:

{&quot;content&quot;:&quot;testcontent&quot;}

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:

{&quot;content&quot;:&quot;testcontent&quot;}

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
Add a ment  | 

3 Answers 3

Reset to default 6

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.

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

相关推荐

  • Json to javascript in fluid-templates with extbase - Stack Overflow

    I was trying to get a json in an javascript variable. I use fluid-templates in my extbase typo3-extensi

    1天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信