smarty模版使用php标签,如何获取模版变量 in: 后端程序 已经assign一个模版变量$assign,由于要做特殊的循环输出,使用for循环,因此使用到了php标签,但是php语句和模版语句的变量作用域是不同的,因此不能直接获取到 {{php}} for($i=0;$i<count($assign);$i=$i+2){ echo ' <ul> <li> <span class="zz_pic"><a href="'._url('picture',array('col_key'=>'cert','pic_id'=>$assign[$i][pic_id])).'" title=""><img src="uploads/thumb_'.$assign[$i][pic].'" alt=""></a></span> <span class="zz_title"><a href="'._url('picture',array('col_key'=>'cert','pic_id'=>$assign[$i][pic_id])).'" title="">'.$assign[$i][title].'</a></span> </li> <li> <span class="zz_pic"><a href="'._url('picture',array('col_key'=>'cert','pic_id'=>$assign[$i+1][pic_id])).'" title=""><img src="uploads/thumb_'.$assign[$i+1][pic].'" alt=""></a></span> <span class="zz_title"><a href="'._url('picture',array('col_key'=>'cert','pic_id'=>$assign[$i+1][pic_id])).'" title="">'.$assign[$i+1][title].'</a></span> </li>i> </ul>';} {{/php}} 解决的方法是:模版变量全部存在smarty的一个对象里面;只要在for之前进行赋值:$assign = $this->_tpl_vars[assign]; {{php}} $assign = $this->_tpl_vars[assign]; for($i=0;$i<count($assign);$i=$i+2){ echo ' <ul> <li> <span class="zz_pic"><a href="'._url('picture',array('col_key'=>'cert','pic_id'=>$assign[$i][pic_id])).'" title=""><img src="uploads/thumb_'.$assign[$i][pic].'" alt=""></a></span> <span class="zz_title"><a href="'._url('picture',array('col_key'=>'cert','pic_id'=>$assign[$i][pic_id])).'" title="">'.$assign[$i][title].'</a></span> </li> <li> <span class="zz_pic"><a href="'._url('picture',array('col_key'=>'cert','pic_id'=>$assign[$i+1][pic_id])).'" title=""><img src="uploads/thumb_'.$assign[$i+1][pic].'" alt=""></a></span> <span class="zz_title"><a href="'._url('picture',array('col_key'=>'cert','pic_id'=>$assign[$i+1][pic_id])).'" title="">'.$assign[$i+1][title].'</a></span> </li>i> </ul>';} {{/php}}