maheswara / ci4smarty
Integration Codeigniter 4 and Smarty Template Engine
v1.0.0
2022-04-16 09:46 UTC
Requires
- php: ^7.1 || ^8.0
- smarty/smarty: ^4.1.0
This package is auto-updated.
Last update: 2025-04-16 16:34:32 UTC
README
Integration Codeigniter 4 and Smarty Template Engine
Requirements
- PHP 7.3+, 8.0+
- CodeIgniter 4.0.4+
- Smarty 4.1.0+
Installation
Installation is best done via Composer. Assuming Composer is installed globally, you may use the following command:
> composer require maheswara/ci4smarty
Manual Installation
Otherwise you can install manually:
-
Download smarty and ci4smarty and put in app/ThirdParty folder
-
Edit and add this line on app/Config/Autoload.php
public $psr4 = [ 'Ci4Smarty' => APPPATH . 'ThirdParty/ci4smarty-1.0.0/src' ]; public $classmap = [ 'Smarty' => APPPATH . 'ThirdParty/smarty-4.1.0/libs/Smarty.class.php', ];
Controller
The easy way to implement this, is by extends your controller from SmartyController.
use Ci4Smarty\Controllers\SmartyController;
class BaseController extends SmartyController{
}
Config
You can override config by adding file SmartyConfig.php to app/Config folder and change it
<?php
namespace Config;
use CodeIgniter\Config\BaseConfig;
class SmartyConfig extends BaseConfig
{
public static $fileExtension = '.tpl';
public static $templateDir = APPPATH . 'Views';
public static $compileDir = WRITEPATH . 'templates_c';
public static $cacheDir = WRITEPATH . 'caches';
public static $configDir = APPPATH . 'Views/configs';
}
Features
This is the features:
- Auto render template base on Controller class and method like: home/index or if url segment is under folder like: admin/dashboard/index
- You can also render manually: $this->render('home/index'); or $this->render('home/index', $data); where $data is array or object
- If you like to change layout you can add $this->setLayout('layout'); before render, and add {$content} in the layout file as a container of rendered template
- You can also use smarty {extend} syntax to implement your layout
- You can stop render by $this->setRendered(true); if you want to debug or print_r your code and eliminate the default exception;
- Don't forget the extension of each template file is .tpl (smarty)