美文网首页
Ci框架引入Layout

Ci框架引入Layout

作者: iscona | 来源:发表于2017-02-21 10:13 被阅读0次
    1. 视图views目录下新建目录_layouts存放layout文件
    2. libraries新建扩展Layout.php
    <?php  
    if (!defined('BASEPATH')) exit('No direct script access allowed');   
    class Layout 
    { 
        
        var $obj; 
        var $layout; 
        
        function __construct($layout = "member") 
        { 
            $this->obj =& get_instance(); 
            $this->layout = '_layouts/' . $layout; 
        } 
      
        function setLayout($layout) 
        { 
          $this->layout = $layout; 
        } 
        
        function view($view, $data=null, $return=false) 
        { 
            $data['content_for_layout'] = $this->obj->load->view($view,$data,true); 
            
            if($return) 
            { 
                $output = $this->obj->load->view($this->layout,$data, true); 
                return $output; 
            } 
            else 
            { 
                $this->obj->load->view($this->layout,$data, false); 
            } 
        } 
    } 
    
    1. 我默认使用member作为layout的,因此在_layouts新建layout视图文件member.php
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="utf-8">
        <title>这是member的layout</title>
    </head>
    <body>
    <?php echo $content_for_layout?>
    </body>
    </html>
    
    1. 控制器controller中使用如下
    class Welcome extends CI_Controller { 
        public function __construct()
        {
            parent::__construct();
            $this->load->library('layout');
        } 
        public function test_layout()
        {
            $data['test'] = '这是<a href="http://www.phpddt.com">PHP点点通</a>测试教程';
            $this->layout->view('content',$data);
        }    
    }
    
    1. views中新建content.php如下
    <h2>这是content内容</h2>
    <?php echo $test;?>
    
    1. 目录结构如下
    Paste_Image.png

    相关文章

      网友评论

          本文标题:Ci框架引入Layout

          本文链接:https://www.haomeiwen.com/subject/zdvywttx.html