Skip to content

A Simple, Native PHP Templating Engine decoupled from CodeIgniter 4

License

Notifications You must be signed in to change notification settings

bharatkumar200/CIViews

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CIViews

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));
});

About

A Simple, Native PHP Templating Engine decoupled from CodeIgniter 4

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages