简体   繁体   English

如何将javascript变量分配给smarty变量

[英]how to assign a javascript variable to a smarty variable

I got a problem regarding how to assign a java script variable to a smarty variable. 我有一个关于如何将Java脚本变量分配给smarty变量的问题。 Here is the code snippet. 这是代码片段。

function getDateInfo(date, wantsClassName)
{                  
    var as_number = Calendar.dateToInt(date); //This as_number is the variable which should be assigned to smarty variable    
}

How can i accomplish this. 我怎样才能做到这一点。

any help will be appreciated.. 任何帮助将不胜感激..

Thanks n advance -- Fero 谢谢您提前-Fero

You can't assign a client-side value to a smarty variable, as smarty is a templating language that runs on the server. 您不能将客户端值分配给smarty变量,因为smarty是在服务器上运行的模板语言。 Smarty assignments can only be done on the server-side, that is to say, from PHP. Smarty分配只能在服务器端进行,也就是说,可以从PHP进行。 Eg: 例如:

$smarty->assign('timestamp',time());

So what you can do is something like: 因此,您可以执行以下操作:

    $smarty->assign('timestamp',time()); //in your PHP script

    //in your JS
    var currentTS = {$timestamp};

See http://www.smarty.net/manual/en/api.assign.php 参见http://www.smarty.net/manual/en/api.assign.php

For anything AJAX with PHP, I'd propose the xajax library: 对于任何使用PHP的AJAX,我建议使用xajax库:

http://www.xajax-project.org/ http://www.xajax-project.org/

Using xajax, you register a PHP function to be exposed to your client side JS code. 使用xajax,您将注册一个PHP函数以向您的客户端JS代码公开。 By magic, if you call registered functions in JS, xajax packs the request with parameters and passes it back to the server. 神奇的是,如果您在JS中调用注册的函数,则xajax会将请求与参数打包在一起,然后将其传递回服务器。

Moreover, you are able to push more than update from your PHP code to the browser. 而且,您不仅可以将更新从PHP代码推送到浏览器。 Something like 'Replace element X with ... and replace element Y with ...' in a single XHR reply. 在单个XHR答复中,类似于“用...替换元素X,用...替换元素Y”。

xajax makes AJAX style code very clean. xajax使AJAX样式代码非常干净。

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

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