我真的不喜欢在每个控制器中写作:
$this->load->view('templates/header');
$this->load->view('body');
$this->load->view('templates/footer');
Run Code Online (Sandbox Code Playgroud)
是否可以这样做,页眉和页脚将自动包含在内,如果我们需要更改它,我们也可以这样做?你怎么处理那件事呢?或者你认为这不是问题?谢谢.
lan*_*ons 102
这是我做的:
<?php
/**
* /application/core/MY_Loader.php
*
*/
class MY_Loader extends CI_Loader {
public function template($template_name, $vars = array(), $return = FALSE)
{
$content = $this->view('templates/header', $vars, $return);
$content .= $this->view($template_name, $vars, $return);
$content .= $this->view('templates/footer', $vars, $return);
if ($return)
{
return $content;
}
}
}
Run Code Online (Sandbox Code Playgroud)
对于CI 3.x:
class MY_Loader extends CI_Loader {
public function template($template_name, $vars = array(), $return = FALSE)
{
if($return):
$content = $this->view('templates/header', $vars, $return);
$content .= $this->view($template_name, $vars, $return);
$content .= $this->view('templates/footer', $vars, $return);
return $content;
else:
$this->view('templates/header', $vars);
$this->view($template_name, $vars);
$this->view('templates/footer', $vars);
endif;
}
}
Run Code Online (Sandbox Code Playgroud)
然后,在您的控制器中,您需要做的就是:
<?php
$this->load->template('body');
Run Code Online (Sandbox Code Playgroud)
Jor*_*eno 42
是.
创建文件夹中调用template.php
的views
文件.
内容template.php
:
$this->load->view('templates/header');
$this->load->view($v);
$this->load->view('templates/footer');
Run Code Online (Sandbox Code Playgroud)
然后从你的控制器你可以做一些事情:
$d['v'] = 'body';
$this->load->view('template', $d);
Run Code Online (Sandbox Code Playgroud)
这实际上是我个人加载所有视图的非常简单的版本.如果你把这个想法发挥到极致,你可以制作一些有趣的模块化布局:
考虑一下,如果创建一个init.php
包含单行的视图:
$this->load->view('html');
Run Code Online (Sandbox Code Playgroud)
现在创建html.php
包含内容的视图:
<!DOCTYPE html>
<html lang="en">
<? $this->load->view('head'); ?>
<? $this->load->view('body'); ?>
</html>
Run Code Online (Sandbox Code Playgroud)
现在创建一个head.php
包含内容的视图:
<head>
<title><?= $title;?></title>
<base href="<?= site_url();?>">
<link rel="shortcut icon" href='favicon.ico'>
<script type='text/javascript'>//Put global scripts here...</script>
<!-- ETC ETC... DO A BUNCH OF OTHER <HEAD> STUFF... -->
</head>
Run Code Online (Sandbox Code Playgroud)
和body.php
与内容视图:
<body>
<div id="mainWrap">
<? $this->load->view('header'); ?>
<? //FINALLY LOAD THE VIEW!!! ?>
<? $this->load->view($v); ?>
<? $this->load->view('footer'); ?>
</div>
</body>
Run Code Online (Sandbox Code Playgroud)
并酌情创建header.php
和footer.php
查看.
现在,当您从控制器调用init时,所有繁重的工作都已完成,您的视图将被包含在内部<html>
和<body>
标签中,您的页眉和页脚将被加载.
$d['v'] = 'fooview'
$this->load->view('init', $d);
Run Code Online (Sandbox Code Playgroud)
试试以下
文件夹结构
-application
--controller
---dashboards.php
--views
---layouts
----application.php
---dashboards
----index.php
Run Code Online (Sandbox Code Playgroud)
调节器
class Dashboards extends CI_Controller
{
public function __construct()
{
parent::__construct();
$data = array();
$data['js'] = 'dashboards.js'
$data['css'] = 'dashbaord.css'
}
public function index()
{
$data = array();
$data['yield'] = 'dashboards/index';
$this->load->view('layouts/application', $data);
}
}
Run Code Online (Sandbox Code Playgroud)
视图
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Some Title</title>
<link rel="stylesheet" href="<?php echo base_url(); ?>assets/css/app.css" />
<link rel="stylesheet" href="<?php echo base_url(); ?>assets/css/<?php echo $css; ?>" />
</head>
<body>
<header></header>
<section id="container" role="main">
<?php $this->load->view($yield); ?>
</section>
<footer></footer>
<script src="<php echo base_url(); ?>assets/js/app.js"></script>
<script src="<php echo base_url(); ?>assets/js/<?php echo $js; ?>"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
当您需要在页眉或页脚中加载不同的js,css或其他任何内容时,请使用该__construct
函数$this->load->vars
这种方式类似铁路
归档时间: |
|
查看次数: |
106446 次 |
最近记录: |