CodeIgniter Integration Guide (CI3 & CI4)

How to use Tplix as a library in CodeIgniter 3 and as a namespaced service in CodeIgniter 4.

CodeIgniter · CI3 & CI4

Tplix + CodeIgniter

A drop-in library for CI3 and a namespaced service class for CI4 — same Tplix API, both versions covered.

composer require tplix/tplix
1

Create the Library

Save this file as application/libraries/Tplixer.php. It wraps the Tplix singleton and exposes familiar CI-style methods.

application/libraries/Tplixer.php
<?php
use Tplix\Tplix;

class Tplixer
{
    public Tplix $tpl;

    public function __construct()
    {
        $this->tpl = Tplix::getInstance();
        $this->tpl->setAllowAllPropertyAccess(true);
        $this->tpl->debugging      = false;
        $this->tpl->debugging_ctrl =
            ($_SERVER['SERVER_NAME'] === 'dev.myapp.test') ? 'URL' : 'NONE';

        // Register custom plugins
        $this->tpl->registerPlugin('function', '_je',                '_je');
        $this->tpl->registerPlugin('function', '_jd',                '_jd');
        $this->tpl->registerPlugin('function', 'lang',               'WsLang');
        $this->tpl->registerPlugin('function', 'WsLang',             'WsLang');
        $this->tpl->registerPlugin('function', 'ConvertDateToShort', 'ConvertDateToShort');
        $this->tpl->registerPlugin('function', 'displayMsgBox',      'displayMsgBox');

        $this->tpl->setTemplateDir(APPPATH . 'views/tplix/');
    }

    public function assign(array|string $key, mixed $value = null): static
    {
        $this->tpl->assign($key, $value);
        return $this;
    }

    public function display(string $tpl): void  { echo $this->tpl->fetch($tpl); }
    public function render(string $tpl): string  { return $this->tpl->fetch($tpl); }
    public function fetch(string $tpl): string   { return $this->tpl->fetch($tpl); }
}
2

Load & Use in a Controller

application/controllers/Home.php
<?php
class Home extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->library('Tplixer');
    }

    public function index(): void
    {
        $this->tplixer->assign('title', 'Home');
        $this->tplixer->assign('items', $this->some_model->get_all());
        $this->tplixer->display('home.tpl');
    }
}
1

Create the Library Class

app/Libraries/Tplixer.php
<?php
namespace App\Libraries;

use Tplix\Tplix;

class Tplixer
{
    public Tplix $tpl;

    public function __construct()
    {
        $this->tpl = Tplix::getInstance();
        $this->tpl->setTemplateDir(APPPATH . 'Views/tplix/');
        $this->tpl->setCompileDir(WRITEPATH . 'cache/tplix_c/');
        $this->tpl->setCacheDir(WRITEPATH   . 'cache/tplix/');
        $this->tpl->debugging = ENVIRONMENT === 'development';
    }

    public function assign(array|string $key, mixed $value = null): void
    {
        $this->tpl->assign($key, $value);
    }

    public function render(string $tpl): string
    {
        return $this->tpl->fetch($tpl);
    }
}
2

Use in a Controller

app/Controllers/Home.php
<?php
namespace App\Controllers;

use App\Libraries\Tplixer;

class Home extends BaseController
{
    public function index()
    {
        $tpl = new Tplixer();
        $tpl->assign('title', 'Welcome');
        $tpl->assign('user',  service('auth')->user());
        return $this->response->setBody($tpl->render('home.tpl'));
    }
}