如何在自定义smarty函数的参数里使用cookie变量?

#1 stalker

<{widget val=$smarty.cookies.id}>
function __template_widget($params){
  return "";
}
结果value什么都没有
而这样可以:

2012-06-02 18:47:15

#2 jake

模板里面cookie的用法没错,手册是这样写的
http://www.smarty.net/docs/en/language.variables.smarty.tpllanguage.variables.smarty.request

或者你可以
function __template_widget($params){
  return "";
}

2012-06-02 21:20:57

#3 stalker

但是不一定要使用cookie啊  也有可能是其它东西

2012-06-02 22:42:06

#4 jake

使用其他值也可以,spAddViewFunction没有问题,可以任意使用。

你上面的代码输出不了值,估计是因为你对cookie的使用不了解,对cookie的使用不正确。

请看cookie测试的要素:http://www.speedphp.com/interaction-session.html

“赋值cookie和使用cookie必须要分开两次页面来进行”

这里是我做的例子,可以自己试试。

控制器
class main extends spController
{
        function index(){
                spAddViewFunction('widget', '__template_widget');
                $this->display('main_index.html');
        }
        
        function assignto() {
                echo setcookie('idname', 'hello world', time()+3600);
        
        }
}

function __template_widget($params){
  return "";
}
模板
{$smarty.cookies.idname}
{widget valname=$smarty.cookies.idname}
先访问
http://localhost/?a=assignto
再访问
http://localhost/

就会有显示了。

2012-06-03 10:09:41

#5 stalker

我好像发现我的问题了  本来这个cookie在本页面上次刷新或者别的页面的时候已经生成  但是在本页面初始化的时候按照原来的值又更新了一下cookie的值  虽然cookie的值没有变   但还是导致cookie在本页无法被调用  我再看看。。。

2012-06-05 02:19:05