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

Have anyone had any luck making smarty 3.x + ci 3.x work together?

I tried http://www.phpclasses.org/blog/post/261-...ether.html

and tried: http://www.coolphptools.com/codeigniter-smarty

but both links are for codeigniter 2.x,

and i would like to use this template engine.

Thanks
Reply
#2

(This post was last modified: 04-02-2015, 08:16 AM by aka_ys.)

(03-20-2015, 09:48 AM)poseso Wrote: Have anyone had any luck making smarty 3.x + ci 3.x work together?

I tried http://www.phpclasses.org/blog/post/261-...ether.html

and tried: http://www.coolphptools.com/codeigniter-smarty

but both links are for codeigniter 2.x,

and i would like to use this template engine.

Thanks

Hi, i don't tried much, but you need make this.

- Download the last smarty compilation http://www.smarty.net/download

- Next step path system/libraries create folder Smarty and put inside the smarty compilation

- path system/libraries create Smarty.php with this code:

Code:
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

require_once( BASEPATH.'libraries/Smarty/libs/Smarty.class.php' );

class CI_Smarty extends Smarty {
public 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 );

        // 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 view($template, $data = array(), $return = FALSE)
    {
        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);
        }
    }
}

- Next, put inside this path application/views/ the folders 'templates' and templates_c

- Finally run this in autoload.php (application/config/autoload.php)  

Code:
$autoload['libraries'] = array('smarty');

- And create your controller like this:
Code:
public function index()
    {
        $data['title'] = 'hello world';
        $this->smarty->view('example.tpl',$data);
    }

- Show data in template:

Code:
{$title}
Reply
#3

Hey aka_ys,
Thanks for your tip!!!
Reply
#4

Was there anything else that you did to get this to work?

I have followed the instructions but getting

Code:
An uncaught Exception was encountered

Type: SmartyException

Message: Unable to load template file 'Test.php'

Filename: /public_html/proximity/system/libraries/Smarty/libs/sysplugins/smarty_internal_template.php

Line Number: 219

Backtrace:

File: /public_html/proximity/application/controllers/Test.php
Line: 10
Function: view

File: /public_html/proximity/index.php
Line: 292
Function: require_once

The following is my controller for Test

PHP Code:
<?php
class Test extends CI_Controller{
    function 
__construct(){
        
parent::__construct();
    }
    
    function 
index(){
        
$data['title'] = "Test Page";
        
$data['text'] = "Testing";
        
$this->smarty->view('Test.php',$data);
    }
    
}
?>

Thank you
Reply
#5

(09-20-2015, 07:02 PM)Jagarm Wrote: Was there anything else that you did to get this to work?

I have followed the instructions but getting


Code:
An uncaught Exception was encountered

Type: SmartyException

Message: Unable to load template file 'Test.php'

Filename: /public_html/proximity/system/libraries/Smarty/libs/sysplugins/smarty_internal_template.php

Line Number: 219

Backtrace:

File: /public_html/proximity/application/controllers/Test.php
Line: 10
Function: view

File: /public_html/proximity/index.php
Line: 292
Function: require_once

The following is my controller for Test


PHP Code:
<?php
class Test extends CI_Controller{
 function 
__construct(){
 
parent::__construct();
 }
 
 function 
index(){
 
$data['title'] = "Test Page";
 
$data['text'] = "Testing";
 
$this->smarty->view('Test.php',$data);
 }
 
}
?>

Thank you


I had the same problem but I fixed it downloading Smarty version 3.1.27.

Please, try to download it : https://github.com/smarty-php/smarty/arc....27.tar.gz
Reply
#6

Hi,

Use $this->setTemplateDir(APPPATH . "/views/templates/"); instead of $this->template_dir = APPPATH . "/views/templates/";
Reply
#7

In the above code shared.. it worked fine

but when tried to use some advanced concepts like template inheritance http://www.smarty.net/docs/en/advanced.f...itance.tpl

its giving me issue when tried to add "extends" in $this->smarty->view function

its just dumping on screen the extends file name provided and nothing else.

Let me know if anyone had a success in implementing these concepts

  Dodgy -Abhijeet
Reply
#8

I updated my code at http://www.coolphptools.com/codeigniter-smarty to work with CodeIgniter 3.0 and Smarty 3.x . Please let me know if you find any issues with the new code.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB