在codeigniter中的一个方法中传递多个页面

时间:2017-07-17 06:55:36

标签: php codeigniter codeigniter-3

这是我创建的用于在codeigniter中加载视图的库,

库:

        public function view($view_name, $params = array(), $layout){

        $renderedview = $this->CI->load->view($view_name,$params,TRUE);

        if($this->data['title'])
        {
            $this->data['title'] = $this->title_separator.$this->data['title'];
        }

        if(array_key_exists('error', $this->data)){
            $error = $this->data['error'];
        }
        else{
            $error = '';
        }

        $this->CI->load->view('layouts/'.$layout, array(
                'content_for_layout' => $renderedview,
                'title_for_layout' => $this->data['title'],
                'error' => $error
            ));
    }

这里我想传递数组(有多个视图),目前只有一个视图正在通过它。

我如何称呼此方法。

控制器方法:

public function __adminRegisterView()
{
    $this->layouts->setTitle('Admin Register');
    $this->layouts->view('pages/admin/account/register','','admin/loginregister');  
}

视图

<body class="login-img3-body">
<?php echo $content_for_layout; ?>
</body>

1 个答案:

答案 0 :(得分:1)

您可以这样做:

 public function view($view_name, $params = array(), $layout){

    if(!is_array($view_name))
    {
        $view_name[] = $view_name;
    }

    $renderedview = "";
    foreach($view_name as $view)
    {
         $renderedview .= $this->CI->load->view($view,$params,TRUE);
    }


    if($this->data['title'])
    {
        $this->data['title'] = $this->title_separator.$this->data['title'];
    }

    if(array_key_exists('error', $this->data)){
        $error = $this->data['error'];
    }
    else{
        $error = '';
    }

    $this->CI->load->view('layouts/'.$layout, array(
            'content_for_layout' => $renderedview,
            'title_for_layout' => $this->data['title'],
            'error' => $error
        ));
}

现在你可以这样调用视图:

$this->layouts->view(array('pages/admin/account/register','pages/admin/account/login','test_view'),'','admin/loginregister');

或:

$this->layouts->view('pages/admin/account/register','','admin/loginregister');