0

有没有办法在php 中我可以在控制器中制作模板但使用另一个控制器渲染它。假设我有两个控制器。HeaderController 和 SearchController。

搜索控制器

class Search extends \system\core\BaseController
{
    public function Index()
    {
        $data['text_search'] = 'Search..';

        // This $this->template->render down below is what I don't want now
        // okay asign the data but do not display the template yet
        echo $this->template->render('common/search', $data);
    }
}

虚拟 SearchController 应该将 $data 分配给模板 search.tpl 但不渲染/显示模板。

这是我将调用上述控制器的地方

标头控制器

class HeaderController extends \system\core\BaseController
{
    public function Index()
    {
        // Some codes

        // Call / load the SearchController and asign it to $data['search'] 
        $data['search'] =  $this->load->controller('common/SearchController');

        // and then pass all $data and render/display it.
        echo $this->template->render('common/header', $data);
    }
}

有没有办法做到这一点?

4

1 回答 1

1

The issue actually comes from the fact, that you are using echo within your class. If your "controllers" (well, they actually seem to be a combination of view and controller) were to return either the content or Response class instance, then you problem should disappear.

于 2017-10-30T15:32:04.547 回答