<?php
// the "TRUE" argument tells it to return the content, rather than display it immediately
$data['menu'] = $this->load->view('menu', NULL, TRUE);
$this->load->view ('home', $data);
?>
然后将 <?=$menu?>放在您希望菜单显示的位置。
2. “从内部”加载视图
首先把这个放进控制器:
<?php
$this->load->view('home');
?>
然后把这个放到 /application/views/home.php视图中:
<?php $this->view('menu'); ?>
<p>Other home content...</p>
$data['middle'] = 'includeFolder/include_template_view'; //the view you want to include
$this->load->view('main_template_view',$data); //load your main view
创建一个新的 helper (在应用程序/helpers 中) ,其名称为(es)。Common _ helpers.php,下划线很重要)。在这个文件中,您将所有函数(例如构建 html 片段)放在一起。
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
function getHead(){
require_once(APPPATH."views/common/head.php");
}
function getScripts(){
require_once(APPPATH."views/common/scripts.php");
}
function getFooter(){
require_once(APPPATH."views/common/footer.php");
}
在控制器中,只调用 MVC 方面的一个视图,并从自定义助手中调用函数。
class Hello extends CI_Controller {
public function index(){
$this->load->helper('common');
$this->load->view('index');
}
}
<?php
public function view($page = NULL)
{
if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php'))
{
$data['title'] = ucfirst($page); // Capitalize the first letter
// Whoops, we don't have a page for that
show_404();
}
$data= array('');
$data['title'] = ucfirst($page); // Capitalize the first letter
$data['page_layout']='pages/'.$page;
$this->load->view('page_layout', $data);
}
?>
在视图文件夹中创建一个名为 page _ layent.php 的页面
page_layout.php
//This is where you set the layout to call any view through a variable called $page_layout declared in the controller//
<?php
$this->load->view('header');
$this->view($page_layout);
$this->load->view('footer');
?>