[CodeIgniter] 그누보드 스타일,스크립트 추가 함수 만들기
그누보드, 영 카트에 보면 add_javascript, add_stylesheet 함수가 존재합니다.
이 함수를 사용하면 <link>와 <script>를 <head> 안에 넣을 수 있게 됩니다.
그누보드 코딩을 하다가 다른 코드를 보면 프런트 관련했을 때 귀찮은 부분이 생기는데 이 부분을 덜어보고자 하여 구현해봅니다.
우선 첫 번째로 중요한 것은 후킹입니다.
후킹을 사용하기 위해서는 application/conifg/config.php 안에서 $config ['enable_hooks'] 값을
변경하고 나면 application/config/hooks.php에서 후킹 관련하여 설정을 잡아줍니다.
$hook['display_override'] = array(
'class' => 'Html_hook',
'function' => 'run',
'filename' => 'Html_hook.php',
'filepath' => 'hooks'
);
위 코드에서 $hook배열의 키값에 넣은 display_override는 처리하는 순간에 대한 값입니다. display_override 말고도 pre_controller, post_controller 등의 값이 들어갈 수 있습니다.
같은 처리 포인트에서 여러 개의 훅을 할 경우는 $hook [][]처럼 배열을 한 단계 더 넣으시면 됩니다.
$hook의 값을 살펴봅시다.
array(
'class' => 'Html_hook', // 훅 클래스 명
'function' => 'run',// 후크 함수명
'filename' => 'Html_hook.php','Html_hook.php',//훅 파일명
'filepath' => 'hooks''hooks'//훅 경로명
);
위처럼 작성을 했다면 훅이 실행될 시 실행되는 것은
application/hooks/Html_hook.php 파일에 Html_hook클래스 run함수가 실행되는 것입니다.
Html_hook.php 파일내용입니다.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Html_hook extends CI_Controller
{
private $CI;
function __construct()
{
$this->CI =& get_instance();//코드이그나이터 객체를 가져옴
}
public function run(){
$buffer = $this->CI->output->get_output(); //화면에 출력된 내용을 가져옴.
/*
<title></title>
타이틀 명 변경
*/
$title = $this->CI->config->item('title');
if(isset($title) and !empty($title))
{
$buffer = preg_replace('#(<title>)(.*?)(</title>)#', '$1' . $title . '$3', $buffer);
}
else{
$buffer = preg_replace('#(<title>)(.*?)(</title>)#', '$1$2$3', $buffer);
}
$stylesheet = '';
$links = $this->CI->config->item('css');
if(!empty($links)) {
foreach ($links as $key => $row) {
$order[$key] = $row[0];
$index[$key] = $key;
$style[$key] = $row[1];
}
array_multisort($order, SORT_ASC, $index, SORT_ASC, $links);
//$links = apply_replace('html_process_css_files', $links);//그누보드 후크 라이브러리 이용시.
foreach($links as $link) {
if(!trim($link[1]))
continue;
$link[1] = preg_replace('#(\.css)#i', '$1?v='.date('ymd'), $link[1]);
// $link_tag =
$link_tag = $link[1];
$stylesheet .= $link_tag;
}
}
/*
</title>
<link rel="stylesheet" href="default.css">
밑으로 스킨의 스타일시트가 위치하도록 하게 한다.
*/
// $buffer = preg_replace('#(</title>[^<]*<link[^>]+>)#', "$1$stylesheet", $buffer);
$buffer = preg_replace('#(</title>)#', "$1$stylesheet", $buffer);
$javascript = '';
$scripts = $this->CI->config->item('js');
$php_eol = '';
unset($order);
unset($index);
if(!empty($scripts)) {
foreach ($scripts as $key => $row) {
$order[$key] = $row[0];
$index[$key] = $key;
$script[$key] = $row[1];
}
array_multisort($order, SORT_ASC, $index, SORT_ASC, $scripts);
//$scripts = apply_replace('html_process_script_files', $scripts);//그누보드 후크 라이브러리 이용시.
foreach($scripts as $js) {
if(!trim($js[1]))
continue;
$js[1] = preg_replace('#(\.js)#i', '$1?v='.date('ymd'), $js[1]);
//$script_tag = script_tag($js[1]);
$script_tag = $js[1];
$javascript .= $script_tag;
}
}
/*
</head>
<body>
전에 스킨의 자바스크립트가 위치하도록 하게 한다.
*/
$nl = '';
if($javascript)
$nl = "\n";
$buffer = preg_replace('#(</head>[^<]*<body[^>]*>)#', "$javascript{$nl}$1", $buffer);
$this->CI->output->set_output($buffer)->_display(); //내용을 저장하고 화면에 출력
}
}
이것으로 그드보드, 영 카드 추가 함수를 구현하였습니다.
다만 아직 초기라서 사용 중간에 문제가 생길 수 있으니 테스트는 필수입니다.