lighthouse / view
PHP Template Engine for the Lighthouse framework
Installs: 11
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/lighthouse/view
Requires
- php: ^8.2
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^11.0
README
A simple, powerful PHP template engine for the Lighthouse framework.
Installation
composer require lighthouse/view
Requirements
- PHP 8.2 or higher
Features
- PHP-based templates (no new syntax to learn)
- Layouts with sections
- Partials/includes
- Automatic HTML escaping
- Global shared data
- Dot notation for view paths
Quick Start
Basic Rendering
use Lighthouse\View\View; $view = new View('/path/to/views'); // Render a view echo $view->render('welcome', ['name' => 'John']);
views/welcome.php:
<h1>Hello, <?= $view->e($name) ?>!</h1>
Escaping
Always escape user data to prevent XSS attacks:
// In templates, use $view->e() or $view->escape() <p><?= $view->e($userInput) ?></p> // For JSON (e.g., in JavaScript) <script> var data = <?= $view->json($data) ?>; </script>
Layouts
Create a layout template:
views/layouts/main.php:
<!DOCTYPE html> <html> <head> <title><?= $view->yield('title', 'My App') ?></title> </head> <body> <header> <?= $view->yield('header') ?> </header> <main> <?= $view->yield('content') ?> </main> <footer>© 2025</footer> </body> </html>
Extend the layout in your view:
views/users/index.php:
<?php $view->extends('layouts.main'); ?> <?php $view->section('title'); ?> Users List <?php $view->endSection(); ?> <?php $view->section('content'); ?> <h1>Users</h1> <ul> <?php foreach ($users as $user): ?> <li><?= $view->e($user['name']) ?></li> <?php endforeach; ?> </ul> <?php $view->endSection(); ?>
Partials
Include reusable components:
views/partials/alert.php:
<div class="alert alert-<?= $view->e($type) ?>"> <?= $view->e($message) ?> </div>
Usage:
<?php $view->include('partials.alert', ['type' => 'success', 'message' => 'Saved!']); ?>
Or capture the output:
$html = $view->partial('partials.alert', ['type' => 'error', 'message' => 'Failed']);
Shared Data
Share data across all views:
// Share single value $view->share('appName', 'My Application'); // Share multiple values $view->share([ 'appName' => 'My Application', 'version' => '1.0.0', 'user' => $currentUser, ]);
Access in any template:
<title><?= $view->e($appName) ?></title>
Dot Notation
View paths use dot notation:
$view->render('users.index'); // views/users/index.php $view->render('admin.users.edit'); // views/admin/users/edit.php $view->partial('partials.nav'); // views/partials/nav.php
Check View Exists
if ($view->exists('emails.welcome')) { echo $view->render('emails.welcome', $data); }
API Reference
View
| Method | Description |
|---|---|
render(string $view, array $data) |
Render a view template |
partial(string $partial, array $data) |
Render and return a partial |
include(string $partial, array $data) |
Render and echo a partial |
extends(string $layout) |
Set layout for current view |
section(string $name) |
Start capturing a section |
endSection() |
End section capture |
yield(string $name, string $default) |
Output section content |
hasSection(string $name) |
Check if section exists |
share(string|array $key, mixed $value) |
Share data globally |
getShared(?string $key) |
Get shared data |
escape(string $value) |
Escape HTML entities |
e(string $value) |
Alias for escape() |
json(mixed $value) |
Safe JSON encoding |
exists(string $view) |
Check if view file exists |
getBasePath() |
Get views base path |
setBasePath(string $path) |
Set views base path |
Template Variables
Inside templates, these variables are available:
$view- The View instance (for helpers)- Any data passed to
render()orpartial() - Any globally shared data
Testing
composer test
License
MIT License. See LICENSE for details.
Part of the Lighthouse Framework
This package is part of the Lighthouse Framework, an educational PHP framework designed to teach how modern frameworks work internally.