I am trying to add Smarty to CodeIgniter, here are the steps that I did:
Downloaded CI
Downloaded Smarty and put its content in 'application/third_party/smarty' folder
Created 'smarty.php' file in 'application/libraries'
Created 'templates' & 'templates_c' folders inside 'application/views' folder
Created simple 'test.tpl' file in 'application/views/templates' folder
Opened 'autoload.php' in 'application/config' folder and added:
$autoload['libraries'] = array('smarty');
Inside a controller wrote $this->smarty->display('test.tpl';
And I get this error - Message: Unable to load template file 'test.tpl'
After some debugging I have noticed that my Smarty template dir is set to default, and not the one that is specified in the bridging class ('application/libraries/smarty.php')
Here is the bridging class content:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once(APPPATH.'third_party/smarty/Smarty.class.php');
class CI_Smarty extends Smarty {
function __construct()
{
parent::__construct();
// NOT Changig this for some reason
$this->setTemplateDir(APPPATH.'views/templates/');
$this->setCompileDir(APPPATH.'views/templates_c/');
}
}
I think the CI_Smarty class isn't being executed for some reason, thats why my template directory is set to default
also note that if I go directly into Smarty.class.php and change the template directory manually - everything will work
Figured out the problem - bridging class should have been in 'system/libraries' and not in 'application/libraries' folder.
The problem is that you need to load ci_smarty
library which extends smarty
, not smarty
directly.
$autoload['libraries'] = array('ci_smarty');
In your controller,
$this->ci_smarty->display('test.tpl');
Important
Please note that all native CodeIgniter libraries are prefixed with CI_ so DO NOT use that as your prefix.
Try to change library name to custom_smarty
instead.
Last but not least
Don't touch the system directory for any reason, Never ever. It is not the best practice. You can easily create new one or extend the existing libraries in your application/libraries
directory.
Hope it will be useful for you.