virilebuddy
virilebuddy

Reputation: 11

pass multiple pages in one method in codeigniter

This is the library I have created to load views in codeigniter,

Library:

        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
            ));
    }

Here I want to pass array (having multiple views), currently only one view is going through it.

How I am calling this method.

Controller Method:

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

In the View:

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

Upvotes: 1

Views: 1059

Answers (1)

eray
eray

Reputation: 81

You can do something like this:

 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
        ));
}

Now you can call view like that:

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

or:

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

Upvotes: 1

Related Questions