使用Codeigniter php加载多个视图

问题描述:

我有4个页面,例如

layout/header.php,
layout/header_assets.php,
user/main.php,
layout/footer.php

我想加载这些多视图,所以我已经在控制器中使用了这段代码

i want to load these multiple view so i have used this code in controller

$this->load->view('layout/header_assets');
    $this->load->view('user/main',$data);
    $this->load->view('layout/footer');

但是我有一个问题:

<link rel="apple-touch-icon" href="http://localhost/game/assets/images/apple-touch-icon.png">
<html lang="en">

<head>
</head>

<body>
test
</body>
</html>
<footer>

</footer>

我想要正确的html代码,header标签中的header_assets加载,body标签中的footer加载.

I want html code in proper way, header_assets load in head tag, footer load in body tag.

但是现在,如果我加载第一个header_assets,然后加载main.php.所以header_assets首先在该main.php之后加载

but right now if i load first header_assets and than load main.php. so header_assets load first after that main.php

我想要这种方式

<html lang="en">

<head>
<link rel="apple-touch-icon" href="http://localhost/game/assets/images/apple-touch-icon.png">
</head>

<body>
test

<footer>

</footer>
</body>
</html>

针对您的问题

在控制器中

$this->load->view('layout/header_assets',$data); # Change
$this->load->view('user/main',$data);
$this->load->view('layout/footer');


最佳选择是

文件应包含三部分.

A file should contain 3 part.

  1. 标题
  2. 身体
  3. 脚.

标题内

仅包含

  1. 元标记
  2. 所有CSS和JS

  1. Meta Tags
  2. All CSS and JS

<html lang="en">    
<head>
    <meta>....
    <link href="">....
    <script>....
</head>

体内

这应该包含显示部分代码和所有代码的页面.其中包含

This should contain page displaying part and all codes. This contain

  1. < body> 标记开始....
  2. 其余所有代码

在页脚

其中应包含

  1. 版权
  2. 移动顶部(需要ID)
  3. </body> 标签以</html>
  4. 关闭
  1. Copyright
  2. Move top(id required)
  3. </body> tag close with </html>

因此您可以在codeigniter中通过以下方式加载这三个视图

So you can load this three View in codeigniter by

$this->load->view('layout/header');
$this->load->view('user/main',$data);
$this->load->view('layout/footer')