Bottlepy - How to access bottle arguments {{var}} from javascript?

最后都变了- 提交于 2019-12-11 10:14:15

问题


I'm developing a template that will be included into a larger template, and for some reason the template is not accepting any arguments in Javascript.

Everything is fine if the argument is accessed from the html. Here's an example:

test.tpl: 

<p>from html: {{arg}}</p>
<script type="text/javascript">
window.alert("from script "+{{arg}});
</script>

From another template, I include test.tpl and pass it with a arg value:

main.tpl:

% include('test.tpl', arg='some value')

The end result is that, the argument in html is displayed fine:

from html: some value

But the windows alert gives something weird:

[object HTMLLIElement]

What is going on?


回答1:


As @dandavis pointed out in the comment, the proper way to reference the argument in javascript is to put it in a quote:

test.tpl: 

<p>from html: {{arg}}</p>
<script type="text/javascript">
window.alert("from script "+"{{arg}}");
</script>

Output:

from script some value

Without the quotation mark, what JavaScript actually sees is a string followed by a variable called some value, which is not cannot be concatenated with a string literal. This is how my original code looks like to the browser:

    window.alert("from script " + some value);

which is wrong.



来源:https://stackoverflow.com/questions/29501534/bottlepy-how-to-access-bottle-arguments-var-from-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!