lukman-ss / view
A lightweight PHP view renderer with file lookup, namespaced views, sections, layouts, partials, and escaping helpers.
v1.0.2
2026-06-14 07:55 UTC
Requires
- php: >=8.2
Requires (Dev)
- phpunit/phpunit: ^10.5
README
A lightweight PHP view renderer with file lookup, namespaced views, layouts, sections, partials, and escaping helpers.
Requirements
- PHP >= 8.2
Installation
composer require lukman-ss/view
Usage
use Lukman\View\FileViewFinder; use Lukman\View\PhpEngine; use Lukman\View\ViewFactory; $finder = new FileViewFinder([__DIR__ . '/views']); $engine = new PhpEngine(); $view = new ViewFactory($finder, $engine); echo $view->render('home', ['name' => 'Lukman']);
views/home.php:
Hello, <?php echo $e($name); ?>
View Names
Dot notation maps to paths:
echo $view->render('pages.home');
This resolves views/pages/home.php.
Namespaced views:
$finder->addNamespace('admin', __DIR__ . '/views/admin'); echo $view->render('admin::dashboard.index');
Shared Data
$view->share('appName', 'Demo'); $view->share(['locale' => 'en']); echo $view->render('home', ['appName' => 'Override']);
Render data overrides shared data.
Layouts and Sections
views/layouts/app.php:
<title><?php echo $section('title', 'Default'); ?></title> <main><?php echo $section('content'); ?></main>
views/home.php:
<?php $extend('layouts.app'); ?> <?php $start('title'); ?>Home<?php $end(); ?> <?php $start('content'); ?> Hello, <?php echo $e($name); ?> <?php $end(); ?>
Partials
<?php echo $include('partials.card', ['title' => 'Profile']); ?>
Included views receive parent data. Additional include data overrides parent data.
Escaping
<?php echo $e($value); ?> <?php echo $raw($html); ?>
$e() escapes with htmlspecialchars using ENT_QUOTES | ENT_SUBSTITUTE and UTF-8. $raw() returns unescaped string output.
Exceptions
Lukman\View\Exception\ViewNotFoundExceptionLukman\View\Exception\ViewException
Running Tests
composer test
