I'm using Bottle and Python 2.7.
I would like to pass a list variable from my Bottle's controller to the view page, where it will be used in a JavaScript variable.
@app.route('/foo'):
def foo():
l = [{'name':'Matthew'}]
return template('foo', l=l)
I've also done:
l = json.dumps([{'name':'Matthew'}]
In my view
<script type="text/javascript">
$(document).ready({
var l = {{l}}
l.forEach(function(entry) {
console.log(entry);
});
});
</script>
However my console says I have a syntax error. When I open the HTML, it is rendered like:
var l = [{"name": "Matthew"]
How can I transfer a python object for use in a JavaScript variable?
The substitutions between {{
and }}
are HTML Entity encoded to prevent XSS amongst other things.
Try this:
var l = {{!l}};
Bottle (via, the SimpleTemplate engine) is escaping the output to prevent XSS vulnerability. This is good, generally. You can disable the escaping temporarily using !
like:
var l = {{!l}};
The docs for it are here
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.