我是CodeIgniter的新手。我想创建包含菜单,页脚等基本样式的母版页或布局。我不想在所有页面中编写重复内容并自动加载所有页面。例如,我可以在asp.net中创建母版页或在asp.net mvc中创建布局。我确信我可以在CodeIgniter中完成。
答案 0 :(得分:16)
假设你有一个html页面
<html>
<head>
<title> Hello World </title>
</head>
<body>
<div id="menu">
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</div>
<div id="main-content">
<!-- this is the dynamic part -->
</div>
<div id="footer">
Copy Right 2013 Hello World
</div>
</body>
</html>
你可以把它拆分成
1-标题
2-菜单
3-主要内容
4英尺
你基本上把
<html>
<head>
<title> Hello World </title>
</head>
<body>
在一个名为“view_header”的视图中 然后你把
<div id="menu">
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</div>
<div id="main-content">
在名为“view_menu”的视图中 然后你把
</div>
<div id="footer">
Copy Right 2013 Hello World
</div>
</body>
</html>
在名为“view_footer”的视图中 然后在你的控制器
$this->load->view('view_header');
$this->load->view('view_menu');
$this->load->view('YOUR_VIEW');
$this->load->view('view_footer');
我看到的另一个解决方案更好:创建一个名为view_template.php的视图
<html>
<head>
<title> Hello World </title>
</head>
<body>
<div id="menu">
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</div>
<div id="main-content">
<?php $this->load->view($content); ?>
</div>
<div id="footer">
Copy Right 2013 Hello World
</div>
</body>
</html>
控制器中的表示您要调用名为About
的视图$data = array('content'=>'about');
$this->load->view('view_template',$data);
答案 1 :(得分:3)
您将使用public $ template变量创建一个新的Controller 然后,您的扩展控制器将从主控制器继承$ template变量。
class MY_Controller extends CI_Controller
{
public $template=null;
public function __construct()
{
if(is_null($this->template)){
$this->template = 'layouts/default';
}
}
}
class Admin_Controller extends MY_Controller
{
public function __construct()
{
//Still inherits from MY_Controller
//this time, any controller extending Admin_Controller
//will point to 'views/layouts/admin'
if(is_null($this->template)){
$this->template = 'layouts/admin';
}
}
}
-
class User extends MY_Controller
{
public function index()
{
//$this->template is inherited
//from MY_Controller
//which point to 'views/layouts/default'
//We can also load a view as data
//ie no master layout, INTO our master layout
//note we don't pass $this->template
//and set the third param as true
$dynamic_sidebar = $this->load->view('views/sidebar/dynamic', array(
'data' => 'some_data'
), true);
return $this->load->view($this->template, array(
'partial' => 'users/index' //partial view,
'dynamic_sidebar' => $dynamic_sidebar
));
}
}
<body>
//load the main view
//in our example we have also loaded
//a dynamic sidebar with this view
<?php $this->load->view($partial); ?>
<?php $this->load->view($dynamic_sidebar); ?>
//load a static view
//views/sidebar/static
<?php $this->load->view('sidebar/static'); ?>
</body>
答案 2 :(得分:2)
按照Laravel中的母版页的想法,我们可以这样做:
$this->load->view('show');
设置母版页的值,然后将变量传递给母版页。将您的母版页代码保存在 ob_start()&amp;的 ob_get_clean()强>
<?php $page_title = "My first page"; ?>
<?php ob_start(); ?>
Your header stylesheet links and scripts
<?php $page_header = ob_get_clean(); ?>
<?php ob_start(); ?>
<div>
<h1>This is a Header</h1>
<?php $this->load->view('Partials/list'); ?>
</div>
<?php $page_content = ob_get_clean(); ?>
<?php ob_start(); ?>
Your footer html or scripts
<?php $page_footer = ob_get_clean(); ?>
<?php $this->load->view('Layout/app',array(
'page_title' => $page_title,
'page_header' => $page_header,
'page_content' => $page_content,
'page_footer' => $page_footer
)); ?>
如果您不希望代码拥挤。您可以创建一些部分视图以简化操作。
<ul>
<li>LIst item 1</li>
<li>LIst item 2</li>
<li>LIst item 3</li>
</ul>
<html>
<head>
<title><?= v($page_title) ?></title>
<?= v($page_header) ?>
</head>
<body>
<?= v($page_content) ?>
<?= v($page_footer) ?>
</body>
</html>
<?php
function v(&$var)
{
return isset($var) ? $var : '';
}
这样可以代码:
<html>
<head>
<title>My first page</title>
Your header stylesheet links and scripts
</head>
<body>
<div>
<h1>This is a Header</h1>
<ul>
<li>LIst item 1</li>
<li>LIst item 2</li>
<li>LIst item 3</li>
</ul>
</div>
Your footer html or scripts
</body>
</html>
答案 3 :(得分:0)
我们可能做的是为页面,菜单,页脚等单独查看文件,这对所有页面都是通用的。并将它们包含在每个视图中。像
$this->view('header');
$this->view('menu');
//Some specific content
$this->view('footer');
如果您需要相同的功能而不将上述内容复制到所有视图,则需要在控制器中创建一个函数,如下所示:
private function myviewfunction($current_view)
{
$this->load->view('header');
$this->load->view('menu');
$this->load->view($current_view);
$this->load->view('footer');
return NULL;
}
并在所有页面(方法)中调用此函数
$this->myviewfunction('about'); //About is the specific view for the method