Bottlepy - 如何从javascript访问瓶子参数{{var}}?

时间:2015-04-07 21:22:02

标签: javascript python bottle

我正在开发一个模板,该模板将被包含在更大的模板中,并且由于某种原因,模板不接受Javascript中的任何参数。

如果从html访问参数,一切都很好。这是一个例子:

test.tpl: 

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

从另一个模板中,我包含test.tpl并使用arg值传递它:

main.tpl:

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

最终结果是,html中的参数显示正常:

from html: some value

但是Windows警报提供了一些奇怪的东西:

[object HTMLLIElement]

发生了什么事?

1 个答案:

答案 0 :(得分:1)

正如@dandavis在评论中指出的那样,在javascript中引用参数的正确方法是将它放在引号中:

test.tpl: 

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

输出:

from script some value

如果没有引号,JavaScript实际看到的是一个字符串,后跟一个名为某个值的变量,它不能与字符串文字连接。这就是我的原始代码在浏览器中的样子:

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

这是错误的。