【发布时间】:2011-06-04 16:01:04
【问题描述】:
我在 Google App Engine python 代码中有这个,
class ABC(db.Model):
StringA = db.StringProperty()
StringB = db.StringProperty(multiline=True)
abcs = ABC.all()
template_values = {'abcs': abcs,}
path = os.path.join(os.path.dirname(__file__), 'index.html')
self.response.out.write(template.render(path, template_values))
这个在 index.html 中,
<script type="text/javascript">
var string_A = [];
var string_B = [];
{% for abc in abcs %}
string_A.push("{{ abc.StringA }}");
string_B.push("{{ abc.StringB }}"); //This doesn't work?
{% endfor %}
</script>
我的问题是如何将多行推送到数组中??
提前致谢。
【问题讨论】:
-
Multiline 在 Javascript 上有点棘手,将 string_B 中的 '\n' 替换为 '\' 应该可以。
-
仅替换 \n 可以解决换行符的问题,但会留下其他字符(例如引号)和潜在的 XSS 漏洞。