ci模板布局方式

1.修改Loader链式加载header和footer方式

参考:http://stackoverflow.com/questions/9540576/header-and-footer-in-codeigniter/

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

        }

    }

}

  在controller中这样调用:

<?php

$this->load->template('body');

2.继承Controller方式

  2.1.在application/core目录下定义MY_Controller内容如下:

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



  class MY_Controller extends CI_Controller

  {

    protected $layout = 'layout/main';

    private $js_files = array();

    private $css_files = array();



    public function __construct()

    {

      parent::__construct();

    }

    public function add_js()

    {

      //添加js文件

    }

    public function add_css(){

      //添加css文件

    }

  //file 表示是否使用渲染子视图文件,viewData表示的是子视图中渲染数据,$layout表示父视图中使用的全局数据

    protected function render($file = NULL, &$viewData = array(), $layoutData = array())

    {

      if( !is_null($file) ) {

        $data['content'] = $this->load->view($file, $viewData, TRUE);

        $data['layout'] = $layoutData;

        $this->load->view($this->layout, $data);

      } else {

        $this->load->view($this->layout, $viewData);

      }

      $viewData = array();

    }

   }

  2.2.在layout下建立 main.php文件,内容如下

<html>

  <head>

  <title><?php echo $layout['title'];?></title>

  </head>

  <body>

    <?php echo $content?>

  </body>

</html>

  2.3.在自定义controller中继承自MY_Controller,添加比如下面测试代码:

public function layout(){

            $data= array('user_name'=>'张三','password'=>'密码');

            $this->render('test_layout',$data,array('title'=>'测试布局'));

        }

  2.4.test_layout.php代码如下

<h1><?php echo $user_name;?></h1>

3.创建Layout类库实现

  3.1.application/libraries目录下创建layout.php文件:

<?php

if (! defined ( 'BASEPATH' ))

	exit ( 'No direct script access allowed' );



class Layout {

	public $layout;

	function __construct($params = array('layout/main')) { 

		$this->layout = $params [0];

	}

	function view($view, $data = null, $flag = false) {

		$ci = &get_instance ();

		$data ['__CONTENT__'] = $ci->load->view ( $view, $data, true ); // 这里的第三个参数true代表不输出,如果是false就会输出,默认是false,和thinkphp里的display和assign类似,这里用第三个参数来控制

		if ($flag) {

			$view = $ci->load->view ( $this->layout, $data, true );

			return $view;

		} else {

			$ci->load->view ( $this->layout, $data, false );

		}

	}

}

  3.2.调用方式

			$data = array (

					'username' => 'abc',

					'register_url' => site_url ( '/account/register/callback' ) 

			);

			$this->layout->view ( 'oauth/callback', $data );

注:在调用之前要先加载layout库(autoload或在controller中加载)。  

你可能感兴趣的:(模板)