Bottlepy-如何从javascript访问瓶参数{{var}}? [英] Bottlepy - How to access bottle arguments {{var}} from javascript?

查看:62
本文介绍了Bottlepy-如何从javascript访问瓶参数{{var}}?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

如果从html访问参数,则一切都很好.这是一个示例:

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>

从另一个模板中,我包含test.tpl并将其传递给arg值:

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

main.tpl:

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

最终结果是,html中的参数显示得很好:

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

from html: some value

但是Windows警报发出了一些奇怪的东西:

But the windows alert gives something weird:

[object HTMLLIElement]

这是怎么回事?

推荐答案

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

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>

输出:

from script some value

在没有引号的情况下,JavaScript实际上看到的是一个字符串,后跟一个名为 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);

这是错误的.

这篇关于Bottlepy-如何从javascript访问瓶参数{{var}}?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆