<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
// Please see Smarty user guide for more info:
// http://smarty.php.net/manual/en/api.variables.php
// The name of the directory where templates are located.
$config['template_dir'] = dirname(FCPATH);
// The directory where compiled templates are located
$config['compile_dir'] = BASEPATH.'cache/';
//This tells Smarty whether or not to cache the output of the templates to the $cache_dir.
$config['caching'] = 0;
// This forces Smarty to (re)compile templates on every invocation.
// When deploying, change this value to 0
$config['force_compile'] = 1;
$config['compile_check'] = TRUE;
?>
※ 캐쉬 파일은 CodeIgniter/system/cache/ 에 저장되고,
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
require "smarty/Smarty.class.php";
class Smarty_parser extends Smarty {
function Smarty_parser($config = array())
{
parent::Smarty();
if (count($config) > 0)
{
$this->initialize($config);
}
// register Smarty resource named "ci"
$this->register_resource("ci", array($this,
"ci_get_template", "ci_get_timestamp", "ci_get_secure", "ci_get_trusted")
);
log_message('debug', "Smarty_parser Class Initialized");
}
/**
* Initialize preferences
*/
function initialize($config = array())
{
foreach ($config as $key => $val)
{
if (isset($this->$key))
{
$method = 'set_'.$key;
if (method_exists($this, $method))
{
$this->$method($val);
} else {
$this->$key = $val;
}
}
}
}
/**
* Set the left/right variable delimiters
*/
function set_delimiters($l = '{', $r = '}')
{
$this->left_delimiter = $l;
$this->right_delimiter = $r;
}
/**
* Parse a template using Smarty engine
*
* Parses pseudo-variables contained in the specified template,
* replacing them with the data in the second param.
* Allows CI and Smarty code to be combined in the same template
* by prefixing template name with "ci:".
*/
function parse($template, $data, $return = FALSE)
{
if ($template == '')
{
return FALSE;
}
$CI =& get_instance();
$CI->benchmark->mark('smarty_parse_start');
if (is_array($data))
{
$this->assign(&$data);
}
// make CI object directly accessible from a template (optional)
$this->assign_by_ref('CI', $CI);
$template = $this->fetch($template);
if ($return == FALSE)
{
$CI->output->final_output = $template;
}
$CI->benchmark->mark('smarty_parse_end');
return $template;
}
/**
* Smarty resource accessor functions
*/
function ci_get_template ($tpl_name, &$tpl_source, &$smarty_obj)
{
$CI =& get_instance();
// ask CI to fetch our template
$tpl_source = $CI->load->view($tpl_name, $smarty_obj->get_template_vars(), true);
return true;
}
function ci_get_timestamp($view, &$timestamp, &$smarty_obj)
{
$CI =& get_instance();
// Taken verbatim from _ci_load (Loader.php, 580):
$ext = pathinfo($view, PATHINFO_EXTENSION);
$file = ($ext == '') ? $view.EXT : $view;
$path = $CI->load->_ci_view_path.$file;
// get file modification date
$timestamp = filectime($path);
return ($timestamp !== FALSE);
}
function ci_get_secure($tpl_name, &$smarty_obj)
{
// assume all templates are secure
return true;
}
function ci_get_trusted($tpl_name, &$smarty_obj)
{
// not used for templates
}
}
?>
준비완료.
<?php
class Smarty_test extends Controller {
function Smarty_test() {
parent::Controller();
}
function index(){
//라이브러리 호출
//config/autoload.php에 설정하면 편리.
$this->load->library('smarty_parser');
$data['title'] = "템플릿 파일 테스트";
$data['body'] = "스마티 템플릿으로 테스트 합니다.";
$this->smarty_parser->parse("ci:template_test.tpl", $data);
}
}
?>
<h3>{$title}</h3>
<h1>{$body}</h1>
저도 CI세상님과 같은 이유로 패스.
뷰페이지에서는
<? foreach ($list as $o): ?>
<table>
<?=$o->val?>
</table>
<? endforeach; ?>
로 처리하면 그럭저럭 깔끔(?) 하더라구요.
ps : Template_ 도 좋읍니다.
컨트롤러에서 완벽하게(?) 데이터 처리만 하신다면 ci의 템플릿도 쓸만합니다. ^^