Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter 3 + Smarty 3
#4

(This post was last modified: 12-10-2015, 04:03 AM by kenji.)

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
PHP Code:
==> hello
==> world 
Reply


Messages In This Thread
CodeIgniter 3 + Smarty 3 - by DarkNight197 - 12-09-2015, 01:34 PM
RE: CodeIgniter 3 + Smarty 3 - by cartalot - 12-09-2015, 01:49 PM
RE: CodeIgniter 3 + Smarty 3 - by ciadmin - 12-09-2015, 03:08 PM
RE: CodeIgniter 3 + Smarty 3 - by kenji - 12-10-2015, 03:07 AM
RE: CodeIgniter 3 + Smarty 3 - by DarkNight197 - 12-23-2015, 11:20 AM
RE: CodeIgniter 3 + Smarty 3 - by DarkNight197 - 01-04-2016, 09:35 AM
RE: CodeIgniter 3 + Smarty 3 - by Diederik - 01-04-2016, 12:43 PM



Theme © iAndrew 2016 - Forum software by © MyBB