2010. 4. 22.

[Code Igniter] layout 적용하기

http://codeigniter.com/wiki/layout_library/ 에서 Layout Library를 구하고 사용법을 확인할 수 있다.

1. /application/libraries/Layout.php 에 다음 소스를 저장한다.
<?php  
if (!defined('BASEPATH')) exit('No direct script access allowed');
 
class Layout
{
 
var $obj;
var $layout;
 
function Layout($layout = "layout_main")
{
$this->obj =& get_instance();
$this->layout = $layout;
}
 
function setLayout($layout)
{
$this->layout = $layout;
}
 
function view($view, $data=null, $return=false)
{
$loadedData = array();
$loadedData['content_for_layout'] = $this->obj->load->view($view,$data,true);
 
if($return)
{
$output = $this->obj->load->view($this->layout, $loadedData, true);
return $output;
}
else
{
$this->obj->load->view($this->layout, $loadedData, false);
}
}
}
?
2. /application/controllers/intro.php (임의로 만든 파일) 의 생성자에 다음 코드를 넣는다.
<?php

class
Intro extends Controller {

function __construct()
{
parent::Controller();

$this->load->library('layout', 'layouts/default');
$this->layout->setLayout('layouts/default');
}
function index()
{
$this->layout->view('layouts/default');
}
}

3. /application/views/layouts/default.php 에 다음 코드를 넣는다.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ko" lang="ko" dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CI 레이아웃</title>
<style type="text/css">
.header {
width: 760px;
height: 50px;
border: 1px solid #CCCCCC;
}
.navigation {
float: left;
}
.navigation li {
float: left;
margin-right: 10px;
list-style: none;
cursor: pointer;
}
</style>
</head>
<body>
<div class="header">
<ul class="navigation">
<li id="home">Home</li>
<li id="send">Send</li>
</ul>
</div>
</body>
</html>

댓글 없음:

댓글 쓰기