视图不显示通过控制器中的主视图加载的内容(CodeIgniter)

时间:2018-02-03 06:58:51

标签: php codeigniter

我创建了两个视图('主要'和' home_view')。通过名为“Home'我通过主视图加载home_view。但结果并不是我在" home_view"上写的。以下是我的网页:

  1. home_view:

    <h1>Hello, from home_view </h1>

  2. Home Controller包含以下代码:

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

  3. 主要观点。我在View文件夹中创建了一个文件夹,名为&#34; Layouts&#34;然后在那个文件夹中我创建了#34; main&#34;视图。主视图包含以下代码:

  4. &#13;
    &#13;
    <!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>
    &#13;
    &#13;
    &#13;

    the screenshothttps://i.stack.imgur.com/9YGX7.png

    更改控制器中的语句我得到了结果,但仍然可以看到该对象:

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

    the screenshot after altering in the controller code

2 个答案:

答案 0 :(得分:0)

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.

答案 1 :(得分:0)

我没有输出但是条形码的原因是拼写错误。在'Main'view的标题部分:

<meter charset="utf-8" >

I should have written this one:

<meta charset="utf-8">