view

Lightweight PHP view-renderer.

René Kulik on 10.05.2018

Some time ago I worked on a small PHP project where I had to pass data to a view and render it. A common and simple task. I did not use any framework so I searched for a ready-to-use solution on GitHub. I found a bunch of packages which provided the functionality I needed, but all of these components where blown up by supporting different types of view-template-files, caching mechanisms, included routers, etc. Therefore, I decided to create my own lightweight PHP view-renderer – easy to use, an intuitive API and no other dependencies.

Requirements

This package requires PHP 7.1 or higher.

Install

Via composer

$ composer require rkulik/view

Usage

Initialize view factory and render template.php:

<?php

require 'vendor/autoload.php';

$viewFactory = new \Rkulik\View\ViewFactory();

$helloWorld = 'Hello, World!';

echo $viewFactory->make('template.php')->with(compact('helloWorld'));

Handle data inside template.php:

<?php

echo '<h1>' . $helloWorld . '</h1>';

Testing

As you might already know from one of my previous posts, testing is very important to me. That’s why this package has a 100% code coverage.

Code Coverage

You can run the tests using following:

$ composer test

Conclusion

For those of you who are looking for a lightweight and easy to use PHP view-renderer without dependencies, the package can be found on GitHub.