简体   繁体   English

如何将JSON插入zope变色龙模板的脚本时代?

[英]How can I insert JSON into a script tage in a zope chameleon template?

I am building a pyramid/python app where my view callable for a certain template passes in a value called data. 我正在构建一个pyramid / python应用程序,其中可为特定模板调用的视图将传入一个名为data的值。 This variable is an array in the form of [[[x1,y1,z1,],...],[[v1,v2,v3],...]] 此变量是[[[x1,y1,z1,],...],[[v1,v2,v3],...]]形式的数组

in my viewcallable I have 在我看来,我有

import json

jsdata = json.dumps(data)

I want to put it into a javascript script tag section of my template so: 我想将其放入模板的javascript脚本标签部分,以便:

<script>
data=${jsdata}
</script>

but i'm pretty sure that syntax is incorrect. 但我很确定语法不正确。 How can I do this? 我怎样才能做到这一点?

Edit: from this: http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/templates.html it seems that Genshi style replacements are the way to go, which I take to mean that what I have above is correct. 编辑:从这里: http ://docs.pylonsproject.org/projects/pyramid/zh-CN/latest/narr/templates.html看来,Genshi样式替换是要走的路,我认为这是我上面提到的内容是正确的。 I am still unsure however, about whether this should be treated differently because it is going inside a javascript tag. 但是,我仍然不确定是否应该区别对待,因为它在javascript标记内。 Is this true? 这是真的?

You want to insert a JavaScript array, not a Python list. 您要插入JavaScript数组,而不是Python列表。

The easiest way to convert between Python and JavaScript formats is to use the json module. 在Python和JavaScript格式之间转换的最简单方法是使用json模块。 JSON is a JavaScript subset for data after all: JSON毕竟是数据的JavaScript子集:

import json

jsdata = (json.dumps(data)
            .replace(u'<', u'\\u003c')
            .replace(u'>', u'\\u003e')
            .replace(u'&', u'\\u0026')
            .replace(u"'", u'\\u0027'))

then pass jsdata to your template instead. 然后将jsdata传递给您的模板。 The str.replace() calls ensure that the data remains HTML safe. 调用str.replace()确保数据保持HTML安全。

In the template, interpolate this without escaping: 在模板中,对其进行插值而不进行转义:

<script>
var data = ${structure:jsdata};
</script>

I'm not sure about Chameleon, but "classical" Zope Page Templates did not allow to do anything inside script tags - if you don't see your variables interpolated it is likely that Chameleon behaves the same. 我不确定Chameleon,但是“经典的” Zope页面模板不允许在script标签内做任何事情-如果您看不到内插的变量,则Chameleon的行为可能相同。 The reason for this, as I understand, is to avoid exactly this type of code generation (you're generating JavaScript from Python via the template). 据我了解,这样做的原因是完全避免了这种类型的代码生成(您正在通过模板从Python生成JavaScript)。 Also, ZPT is an XML-based templating language and the content of <script> tags does not have to be a valid XML. 同样,ZPT是基于XML的模板语言, <script>标记的内容不必是有效的XML。

To work around the problem, you could do something like 要变通解决此问题,您可以执行以下操作

jsdata = '<script type="text/javascript">var data = ' + json.dumps(data) + ';</script>'

and in your template insert the whole thing: 然后在模板中插入整个内容:

<tal:myscript replace="structure jsdata" />

Alternatively, you could do something like 或者,您可以执行类似

<tal:lt replace="string:&lt;" />script>
   var data = <tal:script replace="structure jsdata" />;
<tal:lt replace="string:&lt;" />/script>

which would hide the script tags from Chameleon. 这将隐藏变色龙的script标签。

It would be a good practice to try to keep the amount of generated JavaScript in your pages as minimal as possible. 尝试使页面中生成的JavaScript数量尽可能少是一个好习惯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM