FuelPHP Integration Guide

How to wrap Tplix in a FuelPHP static class and load it as an application package.

FuelPHP · v1.x

Tplix + FuelPHP

Wrap Tplix in a FuelPHP-style static class with a forge() method to follow FuelPHP conventions.

composer require tplix/tplix
1

Create the Tplixer Class

fuel/app/classes/tplixer.php
<?php
use Tplix\Tplix;

class Tplixer
{
    protected static Tplix $instance;

    public static function forge(): static
    {
        if (!isset(static::$instance)) {
            $tplix = Tplix::getInstance();
            $tplix->setTemplateDir(APPPATH . 'views/tplix/');
            $tplix->setCompileDir(APPPATH . 'cache/tplix_c/');
            $tplix->setCacheDir(APPPATH  . 'cache/tplix/');
            static::$instance = new static($tplix);
        }
        return new static(static::$instance->tpl);
    }

    public function __construct(private Tplix $tpl) {}

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

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

Use in a Controller

fuel/app/classes/controller/home.php
<?php
class Controller_Home extends Controller
{
    public function action_index(): Response
    {
        $view = Tplixer::forge();
        $view->assign('title', 'Home');
        $view->assign('items', Model_Post::find('all'));
        return new Response($view->render('home/index.tpl'));
    }
}