user3027490
user3027490

Reputation: 31

View does not display the content loaded through main view in controller (CodeIgniter)

I have created two views ('main' and 'home_view'). Through a controller called 'Home' I load home_view through main view. But the result is not what I had written on the "home_view". Here below are my pages:

  1. home_view:

    <h1>Hello, from home_view </h1>

  2. Home Controller contains the following code:

    <?php class Home extends CI_Controller { public function index () { $data['main_view']="home_view"; $this->load->view('Layouts/main',$data); } } ?>

  3. Main view. I created a folder in View folder called "Layouts" then in that folder I created "main" view. The Main view contains the following code:

<!DOCTYPE html>
<html lang="en">
<head>
	<meter charset="UTF-8">
	<title>Document</title>
	<link rel="stylesheet" href="<?php echo base_url();?>assets/css/bootstrap.min.css"> 
	<script src="<?php echo base_url();?>assets/js/jquery.js"></script>
	<script src="<?php echo base_url();?>assets/js/bootstrap.min.js"></script>

</head>
<body>
<div class="container">
	
	<div class="col-xs-3">
		

	</div>

	<div class="col-xs-9">
				<?php $this->load->view($main_view); ?>
			
	</div>
</div>
</body>
</html>

the screenshot: https://i.sstatic.net/9YGX7.png

altering the statement in the controller I get the results but still, that object is visible:

`$data['main_view']= $this->load->view('home_view');
 $this->load->view('Layouts/main',$data);`

the screenshot after altering in the controller code

Upvotes: 0

Views: 201

Answers (2)

user3027490
user3027490

Reputation: 31

The reason I was getting no output but a bar, was typo error. In the heading section of the 'Main'view:

<meter charset="utf-8" >

I should have written this one:

<meta charset="utf-8">

"

Upvotes: 0

Daniyal Nasir
Daniyal Nasir

Reputation: 707

I was avoiding to suggest you to load the view in advance and pass to the other view first, but since nothing seems to work for you. Do like this in your controller:

<?php
    // the "TRUE" argument tells it to return the content, rather than display it immediately
    $data['main_view'] = $this->load->view('home_view', NULL, TRUE);
    $this->load->view ('Layouts/main', $data);
?>

Then put <?= $main_view ?> in your view at the point you want the menu to appear.

WARNING: When you pass a view this way (with the TRUE parameter ) you are converting all your view content into a string. This works most of the time, but once you have heavy data, or perhaps when you are passing arrays or JSON. it needs to be handled.

Upvotes: 0

Related Questions