Hello
smarty 3.1.27
CI 3.0.3
i use this method since CI 2.0 , maybe an update is necessary, but it works fine
all smarty files in this folder : application/libraries/smarty/
application/views/templates
application/views/templates_c
application/libraries/Template.php
PHP Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
require_once( APPPATH.'libraries/smarty/Smarty.class.php' );
class Template extends smarty {
function __construct()
{
parent::__construct();
$this->compile_dir = APPPATH . "views/templates_c";
$this->template_dir = APPPATH . "views/templates";
$this->assign( 'APPPATH', APPPATH );
$this->assign( 'BASEPATH', BASEPATH );
$this->left_delimiter = '(-';
$this->right_delimiter = '-)';
$this->force_compile = 0;
$this->caching = FALSE;
$this->cache_lifetime = 600;
// Assign CodeIgniter object by reference to CI
if ( method_exists( $this, 'assignByRef') )
{
$ci =& get_instance();
$this->assignByRef("ci", $ci);
}
//log_message('debug', "Smarty Class Initialized");
}
function appel($template, $data = array()){
if (strpos($template, '.tpl') === FALSE) {
$template .= '.tpl';
}
foreach ($data as $key => $val)
{
$this->assign($key, $val);
}
return $this->fetch($template);
}
function view($template, $data = array(), $return = FALSE)
{
if (strpos($template, '.') === FALSE && strpos($template, ':') === FALSE) {
$template .= '.tpl';
}
foreach ($data as $key => $val)
{
$this->assign($key, $val);
}
if ($return == FALSE)
{
$CI =& get_instance();
if (method_exists( $CI->output, 'set_output' ))
{
$CI->output->set_output( $this->fetch($template) );
}
else
{
$CI->output->final_output = $this->fetch($template);
}
return;
}
else
{
return $this->fetch($template);
}
}
}
an i use like this :
PHP Code:
$this->load->libraries('template'); //or load in autoload in config/autoload.php
$this->template->assign('var','Hello world');
$this->template->view('test');
or
$this->template->view('test',array('var' => 'Hello world'));
if you want use result later :
PHP Code:
$var = $this->template->appel('var',array('var' => 'Hello world'));
or
$this->template->assign('var','Hello world');
$var = $this->template->appel('test');
// your code...
application/views/templates/test.tpl
Code:
This is my test : (-$var-)
output :
Code:
This is my test : Hello world
With array
PHP Code:
$test = array(
'a' => hello,
'b' => world
);
$this->template->view("test",array('var' => $test)));
or
$this->template->assign('var', $test);
$this->template->view("test");
Template test.tpl
PHP Code:
(-foreach from=$var key=k item=v name=item-)
(-$k-) ==> (-$v-)
(-/foreach-)
result