smarty变量不会输出

时间:2013-06-27 08:24:10

标签: php html pagination smarty

我正在尝试使用paginator类 http://net.tutsplus.com/tutorials/php/how-to-paginate-data-with-php/但由于某些原因,我无法在模板中的smarty var中输出分页链接。

我试过这个:

function listadverts($where = null, $orderby = null, $limit = null) {         

        $pages = new Paginator;
        $pages->items_total = $this->countadverts($where);
        $pages->mid_range = 9;
        $pages->paginate();

        $row = $this->db->dbh->query('SELECT ad.*, (SELECT img.image FROM '.$this->config->db_prefix.'_images AS img WHERE img.aid = ad.aid LIMIT 1) AS img FROM '.$this->config->db_prefix.'_adverts ad WHERE (ad.approved = 1) '. $where .' ORDER BY '.$orderby.' cr_date DESC '. $pages->limit .'');

        $smarty = new Smarty();

        $smarty->assign('paginate', $pages->display_pages());
        return $row;
    }

在我的模板中我有

{$paginate}

功能

$pages->display_pages()

返回$this->return;

其中$ this->返回包含此HTML

$this->return .= ($i == $this->current_page) ? "<a class=\"current\" href=\"#\">$i</a> ":"<a class=\"paginate\" href=\"$target?page=$i&ipp=$this->items_per_page\">$i</a> ";

{$ paginate | @var_dump}只返回NULL,但如果我var_dump($ smarty)我可以看到

["paginate"]=> object(Smarty_Variable)#114 (3) { ["value"]=> string(227) "1 2 3 4 All "

解决方案:

return array(0 => $row, 1 => $pages->display_pages());

$res = $adverts->listadverts();    
$app->view()->setData('adverts', $res[0]);
$app->view()->setData('paginate', $res[1]);

1 个答案:

答案 0 :(得分:1)

代码示例表明$ smarty应该只存在于您的函数中。尝试全球$ smarty;也可以在外面访问,或者在函数外部将数据插入$ smarty

function listadverts($where = null, $orderby = null, $limit = null) { 

    $pages = new Paginator;
    $pages->items_total = $this->countadverts($where);
    $pages->mid_range = 9;
    $pages->paginate();

    $row = $this->db->dbh->query('SELECT ad.*, (SELECT img.image FROM '.$this->config->db_prefix.'_images AS img WHERE img.aid = ad.aid LIMIT 1) AS img FROM '.$this->config->db_prefix.'_adverts ad WHERE (ad.approved = 1) '. $where .' ORDER BY '.$orderby.' cr_date DESC '. $pages->limit .'');

    return $pages->display_pages();
}

$smarty = new Smarty();
$smarty->assign('paginate', listadverts(... ));