简体   繁体   English

使用模板工具包检测javascript变量?

[英]detect javascript variable with template toolkit?

is there a way I can set a variable/ do something (anything??) in javascript jquery, and detect it with template toolkit? 有没有一种方法可以设置变量/在javascript jquery中做某事(什么?),并使用模板工具包检测到它?

I want to use it as a way to change the content based on if the browser is running javascript. 我想使用它作为一种基于浏览器是否正在运行javascript来更改内容的方法。

My code would flow something like: 我的代码将如下所示:

<scipt type="text/javascript">
variable_name = 1;
</script>

[% IF variable_name == 1 %]
do stuff here, js turned on
[% ELSE %]
//do stuff here, js turned off
[% END %]

when I say anything, I mean is there anything I can do in the JS that template toolkit can pick up? 当我说什么的时候,我的意思是我可以在JS中做任何事情来使模板工具包能够使用? like show/hide a div with JS and detect it with template toolkit? 像使用JS显示/隐藏div并使用模板工具包检测它?

It would be unwise to even try to develop an application in that direction. 甚至尝试朝那个方向开发应用程序都是不明智的。

Since variable_name is a variable, as a programmer, you should expect it to vary, you should expect that it'll change. 由于variable_name是变量,因此作为程序员,您应该期望它会有所变化,并且应该期望它会发生变化。 (And if you don't, use a constant.) (如果不这样做,请使用常量。)

Since the Template Toolkit runs FIRST on the server, and the Javascript runs SECOND on the client, if that variable changes to 0 in the JS, you won't have the content available in the ELSE block of the TT code (which is probably what you're expecting). 由于Template Toolkit在服务器上首先运行,而Javascript在客户端上运行SECOND,因此,如果JS中该变量更改为0,则您将在TT代码的ELSE块中没有可用的内容(可能是您正在期待)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM