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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22531049/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:05:37  来源:igfitidea点击:

How to load page views like header, sidebar, footer in CodeIgniter?

phpcodeignitertemplates

提问by Kamil

Im trying to create my second web application in CodeIgniter.

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

In my previous project I created views for page header, footer and sidebar.

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

In every page controller I had to load these views like this:

在每个页面控制器中,我都必须像这样加载这些视图:

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');
    }
}

I don't like this and I feel im doing it wrong.

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

Any suggestions?

有什么建议?

回答by Majid Golshadi

in advance, you can through this way to build your view:

提前,您可以通过这种方式来构建您的视图:

firstcreate an Template in your view folder

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

<!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>

secondcreate an library to load your view automaticaly

其次创建一个库来自动加载您的视图

<?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
    }
}

thirdload your library in every controller constructor and then use this lib to load you view automatically

第三次在每个控制器构造函数中加载您的库,然后使用此库自动加载您的视图

to load additional js in your view you can use addJSmethod in this way:

要在您的视图中加载其他 js,您可以通过addJS以下方式使用方法:

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

for Css:

对于 CSS:

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

and to show your page content call your view file with showmethod

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

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

i hope this codes help you

我希望这些代码可以帮助你

回答by Dexter

create template.php in view put this code into this

在视图中创建 template.php 将此代码放入此

 <?php  
    $this->load->view('header');
    $this->load->view($middle);
    $this->load->view('footer');    
?>

in controller use this code

在控制器中使用此代码

public function index()
    {
        $data['middle'] = 'home';
    $this->load->view('template',$data);
    }

回答by Rooneyl

You could load the header,sidebar, footer in a main template file that you use.

您可以在您使用的主模板文件中加载页眉、侧边栏、页脚。

// contact.html
<?php $this->load->view('header');?>
<p>Yak yak yak</p>
<?php $this->load->view('footer');?> 

Or use a template library like this one

或使用模板库像这样一个

回答by Mudshark

You could create a library (or helper) like so:

您可以像这样创建一个库(或助手):

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

class View_lib{

   function __construct(){
    $this->CI =& get_instance();
   }


   function load_view($current_page){
       $parts = array('header', 'sidebar', $current_page, 'footer');
       foreach($parts as $part){
       $this->CI->load->view($part);
    }
   }    
}

Controller:

控制器:

public function index(){
    $this->load->library('view_lib'); // probably better autoload this
    $this->view_lib->load_view('about');
}

Kind of barebones, but you get the idea...

有点准系统,但你明白了......

回答by Shailender Ahuja

The best way is to add these files in constructors

最好的方法是在构造函数中添加这些文件

function __construct() { 
     parent::__construct(); 
        $this->load->helper('url'); 
        $this->load->database();
        $this->load->view('prots/header');
        $this->load->view('prots/top');
        $this->load->view('prots/navigation');
  }