Bottlepy - 如何从 javascript 访问 bottle 参数 {{var}}?
Bottlepy - How to access bottle arguments {{var}} from javascript?
我正在开发一个模板,该模板将包含在一个更大的模板中,但出于某种原因,该模板不接受 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]
这是怎么回事?
正如@dandavis 在评论中指出的那样,在 javascript 中引用参数的正确方法是将其放在引号中:
test.tpl:
<p>from html: {{arg}}</p>
<script type="text/javascript">
window.alert("from script "+"{{arg}}");
</script>
输出:
from script some value
没有引号,JavaScript实际看到的是一个字符串,后跟一个名为some value的变量,这不是不能与字符串文字连接。这就是我的原始代码在浏览器中的样子:
window.alert("from script " + some value);
这是错误的。
我正在开发一个模板,该模板将包含在一个更大的模板中,但出于某种原因,该模板不接受 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]
这是怎么回事?
正如@dandavis 在评论中指出的那样,在 javascript 中引用参数的正确方法是将其放在引号中:
test.tpl:
<p>from html: {{arg}}</p>
<script type="text/javascript">
window.alert("from script "+"{{arg}}");
</script>
输出:
from script some value
没有引号,JavaScript实际看到的是一个字符串,后跟一个名为some value的变量,这不是不能与字符串文字连接。这就是我的原始代码在浏览器中的样子:
window.alert("from script " + some value);
这是错误的。