Tiara
Tiara

Reputation: 156

Load View in View CodeIgniter

I want to load a view which come from logical statement (in controller) IN a view. Simply like this

Controller

 public function index(){
   $this->lib();
   $this->view('main');
 }
 public function lib(){
   if(TRUE){
     // $this->view('something1') in $this->view('main')
   }
   else{
     // $this->view('something2') in $this->view('main')
   }
 }

** View **

<html>
<body>
<!-- view from $this->view('main') -->
<!-- want to show view from lib() -->
</body>
</html>

How to show $this->view('something') get from lib() in $this->view('main') ? Any idea ?

Upvotes: 0

Views: 453

Answers (2)

Hasta Dhana
Hasta Dhana

Reputation: 4719

You could preserve the main view and also keep the lib conditional view and pass it to the main controller.
Controller :

public function index(){
    $data['lib_view'] = $this->lib();
    $this->view('main', $data);
}

public function lib(){
    if(TRUE){
        return $this->load->view('something1', true);
    }
    else{
        return $this->load->view('something2', true);
    }
}

'main' View :

<html>
<body>
<!-- view from $this->view('main') -->
<?php echo $lib_view ?>
</body>
</html>

Upvotes: 0

Alex
Alex

Reputation: 9265

The only way that I've found works is this:

Controller:

public function index() {

     $data['internal_view'] = $this->load->view('stmt', [], true);
     $this->load->view('main', $data);

}

"main" View:

<html>
<body>
<?php echo $internal_view; ?>
</body>
</html>

Note: the 3rd param in view allows for the view to be returned as a string rather than automatically outputted to the browser. Because of this functionality, you can assign a view as a return string from another function and use it to generate internal_view or whatever you decide to call it.

Upvotes: 1

Related Questions