gpt4 book ai didi

php - 如何在 CodeIgniter 中加载页眉、侧边栏、页脚等页面 View ?

转载 作者:可可西里 更新时间:2023-10-31 23:56:55 26 4
gpt4 key购买 nike

我正在尝试在 CodeIgniter 中创建我的第二个 Web 应用程序。

在我之前的项目中,我为页眉、页脚和侧边栏创建了 View 。

在每个页面 Controller 中,我必须像这样加载这些 View :

class Home extends CI_Controller {

public function index()
{
$this->load->view('header');
$this->load->view('sidebar');
$this->load->view('home'); // home
$this->load->view('footer');
}

public function about()
{
$this->load->view('header');
$this->load->view('sidebar');
$this->load->view('about'); // about
$this->load->view('footer');
}

public function contact()
{
$this->load->view('header');
$this->load->view('sidebar');
$this->load->view('contact'); // contact
$this->load->view('footer');
}
}

我不喜欢这个,我觉得我做错了。

有什么建议吗?

最佳答案

事先,你可以通过这种方式建立你的 View :

首先在您的 View 文件夹中创建一个模板

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<meta content="MajidGolshadi" name="author">
<?php echo $html_head; ?>
<title></title>
</head>
<body>
<div id="content">
<?php echo $content; ?>
</div>

<div id="footer">
<?php echo $html_footer; ?>
</div>
</body>
</html>

第二个创建一个库来自动加载你的 View

<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Template
{
private $data;
private $js_file;
private $css_file;
private $CI;

public function __construct()
{
$this->CI =& get_instance();
$this->CI->load->helper('url');

// default CSS and JS that they must be load in any pages

$this->addJS( base_url('assets/js/jquery-1.7.1.min.js') );
$this->addCSS( base_url('assets/css/semantic.min.css') );
}

public function show( $folder, $page, $data=null, $menu=true )
{
if ( ! file_exists('application/views/'.$folder.'/'.$page.'.php' ) )
{
show_404();
}
else
{
$this->data['page_var'] = $data;
$this->load_JS_and_css();
$this->init_menu();

if ($menu)
$this->data['content'] = $this->CI->load->view('template/menu.php', $this->data, true);
else
$this->data['content'] = '';

$this->data['content'] .= $this->CI->load->view($folder.'/'.$page.'.php', $this->data, true);
$this->CI->load->view('template.php', $this->data);
}
}

public function addJS( $name )
{
$js = new stdClass();
$js->file = $name;
$this->js_file[] = $js;
}

public function addCSS( $name )
{
$css = new stdClass();
$css->file = $name;
$this->css_file[] = $css;
}

private function load_JS_and_css()
{
$this->data['html_head'] = '';

if ( $this->css_file )
{
foreach( $this->css_file as $css )
{
$this->data['html_head'] .= "<link rel='stylesheet' type='text/css' href=".$css->file.">". "\n";
}
}

if ( $this->js_file )
{
foreach( $this->js_file as $js )
{
$this->data['html_head'] .= "<script type='text/javascript' src=".$js->file."></script>". "\n";
}
}
}

private function init_menu()
{
// your code to init menus
// it's a sample code you can init some other part of your page
}
}

第三在每个 Controller 构造函数中加载你的库,然后使用这个库自动加载你的 View

要在您的 View 中加载额外的 js,您可以使用 addJS 方法:

$this->template->addJS("your_js_address");

对于 CSS:

$this->template->addCSS("your_css_address");

并使用show 方法显示您的页面内容调用您的 View 文件

$this->template->show("your_view_folder", "view_file_name", $data);

希望这些代码对你有帮助

关于php - 如何在 CodeIgniter 中加载页眉、侧边栏、页脚等页面 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22531049/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com