블럭단위의 독립적인 레이아웃과 효과적인 마크업에 가장 중점을 뒀습니다.
허접하지만 많은 지적 부탁드립니다.
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Layout
{
private $layout = array();
private $data = array();
function __construct()
{
$this->obj =& get_instance();
$this->layout_header = 'default/layout_h';
$this->layout_footer = 'default/layout_f';
$this->data['title'] = $this->obj->config->item('coo_title');
$this->add('js', 'http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js');
if( $this->obj->uri->segment(1) != 'adm' )
{
$this->add('css', RT_PATH.'/css/common.css?20111007');
}
}
public function view($path, $data = array())
{
$this->layout[] = array('path' => $path, 'data' => $data);
}
public function assign($key, $value)
{
$this->data[$key] = $value;
}
public function add($key, $value)
{
if( is_array($value) === TRUE )
{
$this->data[$key] = array_merge( $this->data[$key], $value );
}
else
{
$this->data[$key][] = $value;
}
}
public function output()
{
$this->obj->load->view($this->layout_header, $this->data);
foreach($this->layout as $output)
{
$this->obj->load->view($output['path'], $output['data']);
}
$this->obj->load->view($this->layout_footer);
}
}
/* End of file Layout.php */
/* Location: ./application/libraries/Layout.php */
헤더 부분 마크업입니다.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<meta name="author" content="" />
<title><?=$title?></title>
<?foreach($css as $url):?>
<link rel="stylesheet" href="<?=$url?>" type="text/css" media="screen" />
<?endforeach;?>
<?foreach($js as $url):?>
<script type="text/javascript" src="<?=$url?>"></script>
<?endforeach;?>
</head>
<body>
사용 예
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class ohead extends CI_Controller {
function __construct()
{
parent::__construct();
//$this->output->enable_profiler(TRUE);
$this->load->library('layout');
}
public function index()
{
$this->layout->add('js', JS_PATH.'/co.js');
$this->layout->view('default/header');
$this->layout->view('default/contents');
$this->layout->view('default/footer');
$this->layout->output();
}
}
/* End of file main.php */
/* Location: ./application/controllers/main.php */
해더부분이외에 css 나 js 를 넣지 않기 위해서 개별 js 파일 삽입을 가능하도록 작성했습니다.
단점은 많이 느슨한구조로 보안부분이 문제가 될수도 있지 않을까싶네요..
긴글 읽어주셔서 감사합니다.
|