CakePHP Integration Guide
How to use Tplix as a custom View class in CakePHP 4 and 5.
CakePHP · v4 / v5
Tplix + CakePHP
Create a custom TplixView class extending CakePHP's base View and set it as the view class in your controllers.
composer require tplix/tplix
1
Create TplixView
src/View/TplixView.php
<?php
namespace App\View;
use Cake\View\View;
use Tplix\Tplix;
class TplixView extends View
{
private Tplix $tplix;
public function initialize(): void
{
parent::initialize();
$this->tplix = Tplix::getInstance();
$this->tplix->setTemplateDir(ROOT . DS . 'templates' . DS . 'tplix' . DS);
$this->tplix->setCompileDir(TMP . 'tplix_c' . DS);
$this->tplix->setCacheDir(TMP . 'tplix_cache' . DS);
}
public function render(?string $view = null, $layout = null): string
{
$this->tplix->assign($this->viewVars);
return $this->tplix->fetch(($view ?? $this->template) . '.tpl');
}
}
2
Set as View Class in Controller
src/Controller/PagesController.php
<?php
namespace App\Controller;
use App\View\TplixView;
class PagesController extends AppController
{
public string $viewClass = TplixView::class;
public function home(): void
{
$this->set('title', 'Home');
$this->set('posts', $this->Posts->find('latest')->toArray());
}
}