simplydi / civiews
A Simple, Native PHP Templating Engine decoupled from CodeIgniter 4
dev-main
2023-08-05 04:03 UTC
This package is auto-updated.
Last update: 2025-03-05 07:36:35 UTC
README
A Simple, Native PHP Templating Engine decoupled from CodeIgniter 4
Usage
Writing Templates: refer to CI4 docs
Example 1:
$data = [ 'title' => 'Dummy Title' ]; $renderer = new \SimiplyDi\CIViews\Renderer('/path/to/templates/dir'); $renderer->data = $data; // or $renderer->setVar('title', $data['title']); echo $renderer->render('home');
Example with DI
MyController.php:
class MyController { private RendererInterface $renderer; public function __construct(RendererInterface $renderer) { $this->renderer = $renderer; } public function index(): string { $this->renderer->data = [ 'title' => 'Welcome to website', 'content' => 'welcome to website', ]; return $this->renderer->render('home'); } }
Dependency Container (use any container you want). Example:
$container = new Container(); $container->bind(RendererInterface::class, function () { // pass the templates directory as first param and extension you want to use (optional; defaults to .php) return new Renderer(__DIR__ . '/views', '.phtml'); }); $container->bind(MyController::class, function () use ($container) { return new MyController($container->resolve(RendererInterface::class)); });