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

Hi,

I want to make a website using CodeIgniter 3 and including Smarty 3 (I know how to use them separatly but doesn't works when I use them together). I watched lot of forums but nothing helped me.

Could you tell me how to install it correctly and functionnal with the good files as controller and view.

Thank you a lot if anyone can give me a quick fix.

Cordially
Reply
#2

(This post was last modified: 12-09-2015, 01:50 PM by cartalot.)

always look in stackoverflow: http://stackoverflow.com/search?q=codeigniter+smarty
Reply
#3

There are also some threads in this forum, accessed from our search ... for instance http://forum.codeigniter.com/thread-1551...ght=smarty
Reply
#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
#5

Thanks all, I tried these tutorials
It wokrs but the views files must be in the main folder as index.php

I tried to found a solution but it still doesn't work

Anyone know why what it's like that ?

Thanks
Reply
#6

I found the solution thank a lot for answered !
Reply
#7

Why not share your solution here? Perhaps it can help someone else in the future.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB