cakePHP 2.0은 1.x와 디렉토리 구조 및 사용법에서 많이 차이가 난다.
cakePHP 1.x에서 Smarty 연동법은 있으나 cakePHP 2.0에서는 없어서 혼자 삽집을 하고 나서 까먹을까봐 정리해본다.
1. Smarty의 lib를 /vendors/smarty 폴더에 복사
- /app/Vendor에 복사를 하든 /vendors에 복사를 하든 둘 다 정상적으로 동작을 하니 둘 중 편한곳에 설치해도 무방하다.
2. /app/View에 smarty.php 파일 만들기
- View 클래스를 확장하여 다음과 같이 작성한다. Smarty 3.0 에서는 plugin을 알아서 인식하므로 제외를 하고 주석한 부분은 입맛에 맞게 설정한다.
Class SmartyView extends View{
function __construct (&$controller) {
parent::__construct($controller);
if (is_object($controller)) {
$count = count($this->__passedVars);
for ($j = 0; $j < $count; $j++) {
$var = $this->__passedVars[$j];
$this->{$var} = $controller->{$var};
}
}
if(!App::import('Vendor', 'Smarty', array('file' => 'smarty'.DS.'Smarty.class.php')))
die('error Loading Smarty Class');
$this->Smarty = new Smarty();
$this->ext= '.tpl';
//$this->Smarty->compile_dir = TMP.'smarty'.DS.'compile'.DS;
//$this->Smarty->cache_dir = TMP.'smarty'.DS.'cache'.DS;
//$this->Smarty->caching=true;
$this->Helpers = new HelperCollection($this);
}
protected function _render($___viewFn, $___dataForView = array()) {
if (empty($___dataForView)) {
$___dataForView = $this->viewVars;
}
extract($___dataForView, EXTR_SKIP);
foreach($___dataForView as $data => $value) {
if(!is_object($data)) {
$this->Smarty->assign($data, $value);
}
}
$this->Smarty->display($___viewFn);
}
}
3. /lib/Cake/Controller/AppController.php 파일 수정
- 되도록이면 /lib/ 하위 파일들을 수정하는 것은 꺼림칙하나 Smarty를 전역으로 사용하기 위해선 수정이 필요하다.
하나의 파일에만 사용할려면 해당 Controller에서 선언을 하면 된다.
class AppController extends Controller {
var $viewClass = 'Smarty';
}
4. default.ctp
- cakePHP는 default.ctp 에서 코드를 실행시키는 구조이다. default.ctp의 위치는 /lib/Cake/View/Layouts/default.ctp이다
사용을 할려면 이 파일을 수정하든지, 사용하지 않으면 3의 파일에 var $autoLayout = false;를 지정하면 된다.
5. template 파일 위치 변경하기
- 위의 대로 하면 template위치는 /app/View/에 위치를 할 것이다. 그러나 대부분 template파일은 따로 지정되어 있거나,
혹은 스킨기능(근래는 css로 조절을 하지만 따로 가지고 있는 경우도 있다)을 위해 skin01, skin02 이런식으로 지정할 필요성이 생긴다.
2번의 클래스에서 _getViewFileName() 함수를 오버라이딩하자.
$paths는 사용가능한 path를 불러온다. 찍어보면
/app/View/, /app/views, /lib/Cake/View에서 찾는다. 여기에 우리가 사용할 템플릿 위치를 하나 더 추가하면 된다.
아님 좀 더 고급스럽고 멋지게 만드셔도 된다.
function _getViewFileName($action)
{
$action = Inflector::underscore($action);
$paths = $this->_paths();
$paths[] = $_SERVER["DOCUMENT_ROOT"]."/template/skin01/";
if (!is_null($this->webservices)) {
$type = strtolower($this->webservices) . DS;
} else {
$type = null;
}
if (empty($action)) {
$action = $this->action;
}
$position = strpos($action, '..');
if ($position === false) {
} else {
$action = explode('/', $action);
$i = array_search('..', $action);
unset($action[$i - 1]);
unset($action[$i]);
$action='..' . DS . implode(DS, $action);
}
foreach($paths as $path) {
if (file_exists($path . $this->viewPath . DS . $this->subDir . $type . $action . $this->ext)) {
$viewFileName = $path . $this->viewPath . DS . $this->subDir . $type . $action . $this->ext;
return $viewFileName;
}
}
// added for .ctp viewPath fallback
foreach($paths as $path) {
if (file_exists($path . $this->viewPath . DS . $type . $action . '.ctp')) {
$viewFileName = $path . $this->viewPath . DS . $type . $action . '.ctp';
return $viewFileName;
}
}
return $viewFileName;
}
// 줄이 이상해 졌다...ㅜㅜ 줄 띄우기에 민감한데..
6. 좀 더 보완해야 될 문제
- Smarty 오류 메세지 처리
tpl 파일에 에러가 있으면 에러메세지를 출력하는데 해석되지 않는 php 코드를 쏟아낸다. 이 부분은 좀 더 봐야 될 것 같다.

