php CodeIgniter 中的页眉和页脚
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9540576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Header and footer in CodeIgniter
提问by good_evening
I really don't enjoy writing in every controller:
我真的不喜欢在每个控制器中编写:
$this->load->view('templates/header');
$this->load->view('body');
$this->load->view('templates/footer');
Is it possible to do, that header and footer would be included automatically and if we need to change it, we could also do that? How do you deal with that? Or it's not a problem in your opinion? Thanks.
是否可以这样做,页眉和页脚将自动包含在内,如果我们需要更改它,我们也可以这样做?你怎么处理?或者在您看来这不是问题?谢谢。
回答by landons
Here's what I do:
这是我所做的:
<?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;
}
}
}
For CI 3.x:
对于 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;
}
}
Then, in your controller, this is all you have to do:
然后,在您的控制器中,这就是您所要做的:
<?php
$this->load->template('body');
回答by Jordan Arseno
Yes.
是的。
Create a file called template.php
in your views
folder.
template.php
在您的views
文件夹中创建一个名为的文件。
The contents of template.php
:
内容template.php
:
$this->load->view('templates/header');
$this->load->view($v);
$this->load->view('templates/footer');
Then from your controller you can do something like:
然后从您的控制器中,您可以执行以下操作:
$d['v'] = 'body';
$this->load->view('template', $d);
This is actually a very simplistic version of how I personally load all of my views. If you take this idea to the extreme, you can make some interesting modular layouts:
这实际上是我个人加载所有视图的非常简单的版本。如果你把这个想法发挥到极致,你可以做出一些有趣的模块化布局:
Consider if you create a view called init.php
that contains the single line:
考虑是否创建一个init.php
包含单行的视图:
$this->load->view('html');
Now create the view html.php
with contents:
现在创建html.php
包含内容的视图:
<!DOCTYPE html>
<html lang="en">
<? $this->load->view('head'); ?>
<? $this->load->view('body'); ?>
</html>
Now create a view head.php
with contents:
现在创建一个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>
And a body.php
view with contents:
以及body.php
包含内容的视图:
<body>
<div id="mainWrap">
<? $this->load->view('header'); ?>
<? //FINALLY LOAD THE VIEW!!! ?>
<? $this->load->view($v); ?>
<? $this->load->view('footer'); ?>
</div>
</body>
And create header.php
and footer.php
views as appropriate.
并根据需要创建header.php
和footer.php
查看。
Now when you call the init from the controller all the heavy lifting is done and your views will be wrapped inside <html>
and <body>
tags, your headers and footers will be loaded in.
现在,当您从控制器所有繁重完成调用init和您的意见将被包裹在里面<html>
和<body>
标签,页眉和页脚会在加载。
$d['v'] = 'fooview'
$this->load->view('init', $d);
回答by Andre Dublin
Try following
尝试以下
Folder structure
文件夹结构
-application
--controller
---dashboards.php
--views
---layouts
----application.php
---dashboards
----index.php
Controller
控制器
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);
}
}
View
看法
<!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>
When you need to load different js, css or whatever in the header or footer use the __construct
function to $this->load->vars
当您需要加载不同的 js、css 或页眉或页脚中的任何内容时,请使用该__construct
函数$this->load->vars
Kind of a rails like approach here
有点像这里的方法
回答by Datadimension
Or more complex, but makes life easy is to use more constants in boot. So subclasses can be defined freely, and a single method to show view. Also selected constants can be passed to javascript in the header.
或者更复杂,但让生活更轻松的是在引导中使用更多常量。因此可以自由定义子类,并使用单个方法来显示视图。还可以将选定的常量传递给标题中的 javascript。
<?php
/*
* extends codeigniter main controller
*/
class CH_Controller extends CI_Controller {
protected $viewdata;
public function __construct() {
parent::__construct();
//hard code / override and transfer only required constants (for security) server constants
//such as domain name to client - this is for code porting and no passwords or database details
//should be used - ajax is for this
$this->viewdata = array(
"constants_js" => array(
"TOP_DOMAIN"=>TOP_DOMAIN,
"C_UROOT" => C_UROOT,
"UROOT" => UROOT,
"DOMAIN"=> DOMAIN
)
);
}
public function show($viewloc) {
$this->load->view('templates/header', $this->viewdata);
$this->load->view($viewloc, $this->viewdata);
$this->load->view('templates/footer', $this->viewdata);
}
//loads custom class objects if not already loaded
public function loadplugin($newclass) {
if (!class_exists("PL_" . $newclass)) {
require(CI_PLUGIN . "PL_" . $newclass . ".php");
}
}
then simply:
然后简单地:
$this->show("<path>/views/viewname/whatever_V.php");
will load header, view and footer.
将加载页眉、视图和页脚。
回答by Jamshid Hashimi
You can use your config.php file, and also use the power of helpers in CodeIgniter.
您可以使用 config.php 文件,也可以使用 CodeIgniter 中助手的强大功能。
$config['header_css'] = array('style.css','prettyPhoto.css','nivo-slider.css');
$config['header_js'] = array('core.js','core.js',
'jquery-1.4.1.min.js',
'jquery-slidedeck.pack.lite.js',
'jquery-prettyPhoto.js',
'jquery.nivo.slider.js');
Source: https://jamshidhashimi.com/dynamically-add-javascript-and-css-files-in-codeigniter-header-page/
来源:https: //jamshidhashimi.com/dynamically-add-javascript-and-css-files-in-codeigniter-header-page/
回答by Line in Linus
A simple rewrite of @Landons MY_Loader, to include multiple files for the body, e.i. page unique sidebars...
对@Landons MY_Loader 的简单重写,为正文包含多个文件,ei 页面独特的侧边栏...
<?php
class MY_Loader extends CI_Loader {
public function template($template_name, $vars = array(), $return = FALSE)
{
$content = $this->view('frontend/templates/header', $vars, $return);
if(is_array($template_name)) { //return all values in contents
foreach($template_name as $file_to_load) {
$content .= $this->view('frontend/'.$file_to_load, $vars, $return);
}
}
else {
$content .= $this->view('frontend/'.$template_name, $vars, $return);
}
$content .= $this->view('frontend/templates/footer', $vars, $return);
if ($return)
{
return $content;
}
}
}
This works both ways...
这两种方式都有效...
Including one file to template:
包括一个文件到模板:
$data['moo'] = 'my data'];
$this->load->template('home', $data);
Include multiple files to template:
在模板中包含多个文件:
$data['catalog'] = 'catalog load 1';
$data['sidebar'] = 'sidebar load 2';
$load = array('catalog/catalog', 'catalog/sidebar');
$this->load->template($load, $data);
回答by shaz3e
CodeIgniter-Assetsis easy to configure repository to have custom header and footer with CodeIgniter I hope this will solve your problem.
CodeIgniter-Assets很容易配置存储库以使用 CodeIgniter 自定义页眉和页脚我希望这能解决您的问题。
回答by siraj k
Redefine the CI_Loader::view function by adding a file named as 'MY_Loader.php' in your application/core folder and adding the following content
通过在 application/core 文件夹中添加名为“MY_Loader.php”的文件并添加以下内容来重新定义 CI_Loader::view 函数
/**
* /application/core/MY_Loader.php
*/
class MY_Loader extends CI_Loader
{
public function view($view, $vars = array(), $return = FALSE, $include_template=TRUE)
{
$header='';
$footer='';
if($include_template)
{
$header=parent::view('templates/header',$vars,$return);
}
$content=parent::view($view, $vars,$return);
if($include_template)
{
$footer=parent::view('templates/footer',$vars,$return);
}
if($return)
return "$header$content$footer";
return $this;
}
}
回答by Fifi
I tried almost all the answers proposed on this page and many other stuff. The best option I finally keeped on all my websites is the following architecture:
我尝试了此页面上提出的几乎所有答案以及许多其他内容。我最终保留在所有网站上的最佳选择是以下架构:
A single view
单一视图
I display only one view in the browser. Here is my main view (/views/page.php):
我在浏览器中只显示一个视图。这是我的主要观点(/views/page.php):
<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<!DOCTYPE html>
<html lang="en">
<head>
<?= $header ?? '' ?>
</head>
<body>
<div style="width:1200px">
<?= $content ?? '' ?>
</div>
</body>
</html>
Controllers deal with multiple views
控制器处理多个视图
Of course, I had several views but they are concatenated to build the $header
and the $content
variables. Here is my controller:
当然,我有几个视图,但它们被连接起来以构建$header
和$content
变量。这是我的控制器:
$data['header'] = $this->load->view('templates/google-analytics', '', TRUE)
.$this->load->view('templates/javascript', '', TRUE)
.$this->load->view('templates/css', '', TRUE);
$data['content'] = $this->load->view('templates/navbar', '', TRUE)
.$this->load->view('templates/alert', $myData, TRUE)
.$this->load->view('home/index', $myData, TRUE)
.$this->load->view('home/footer', '', TRUE)
.$this->load->view('templates/modal-login', '', TRUE);
$this->load->view('templates/page', $data);
- Look how beautiful and clear is the source code.
- You no longer have HTML markup opened in one view and closed in another.
- Each view is now dedicated to one and only one stuff.
- Look how views are concatenated: method chaining pattern, or should we say: concatanatedchaining pattern!
- You can add optional parts (for example a third
$javascript
variable at the end of the body) - I frequently extend CI_Controller to overload
$this->load->view
with extra parameters dedicated to my application to keep my controllers clean. - If you are always loading the same views on several pages (this is finally the answer to the question), two options depending on your needs:
- load views in views
- extend CI_Controller or CI_Loader
- 看看源代码多么漂亮和清晰。
- 您不再需要在一个视图中打开 HTML 标记并在另一个视图中关闭。
- 现在,每个视图都专用于一个且仅一个内容。
- 看意见是如何连接在一起:方法链接模式,或者我们应该说:concatanated链格局!
- 您可以添加可选部分(例如
$javascript
,正文末尾的第三个变量) - 我经常扩展 CI_Controller 以重载
$this->load->view
专用于我的应用程序的额外参数,以保持我的控制器干净。 - 如果您总是在多个页面上加载相同的视图(这就是问题的最终答案),根据您的需要有两个选项:
- 在视图中加载视图
- 扩展 CI_Controller 或 CI_Loader
I'm so proud of this architecture...
我为这座建筑感到非常自豪......
回答by HanSal
i had reached for this and i hope to help all create my_controller in application/core then put this code in it with change as your file's name
我已经达到了这个目的,我希望帮助所有人在应用程序/核心中创建 my_controller 然后将此代码放入其中并更改为您的文件名
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
// this is page helper to load pages daunamically
class MY_Controller extends CI_Controller {
function loadPage($user,$data,$page='home'){
switch($user){
case 'user':
$this->load->view('Temp/head',$data);
$this->load->view('Temp/us_sidebar',$data);
$this->load->view('Users/'.$page,$data);
$this->load->view('Temp/footer',$data);
break;
case 'admin':
$this->load->view('Temp/head',$data);
$this->load->view('Temp/ad_sidebar',$data);
$this->load->view('Admin/'.$page,$data);
$this->load->view('Temp/footer',$data);
break;
case 'visitor';
$this->load->view('Temp/head',$data);
$this->load->view($page);
$this->load->view('Temp/footer',$data);
break;
default:
echo 'wrong argument';
die();
}//end switch
}//end function loadPage
}
in your controller use this
在你的控制器中使用这个
class yourControllerName extends MY_Controller
note : about name of controller prefix you have to be sure about your prefix on config.php file i hope that give help to any one
注意:关于控制器前缀的名称,您必须确定您在 config.php 文件中的前缀,希望对任何人有所帮助