Skip to content
Muhammet Şafak edited this page Jun 11, 2026 · 2 revisions

InitPHP Views — Wiki

Welcome to the official documentation for initphp/views — a small, adapter-based view rendering layer for PHP 8.0+. Your application talks to one tiny interface; an adapter turns queued view names plus data into a string. Render with plain PHP, Blade or Twig and switch engines without touching your call sites.

composer require initphp/views
use InitPHP\Views\Facade\View;
use InitPHP\Views\Adapters\PurePHPAdapter;

View::via(new PurePHPAdapter(__DIR__ . '/views'));

echo view('dashboard', ['username' => 'admin']);          // one view
echo view(['header', 'content', 'footer'], ['title' => 'Home']); // many, in order

Register an adapter once; everything after that — the global view() helper and the View facade — flows through it. Swap PurePHPAdapter for BladeAdapter or TwigAdapter and the rest of your code is unchanged.

The package in one table

Symbol Role
View Static facade — register an adapter with via(), then forward every call to it.
view() Global helper — the one-liner most code uses.
ViewAdapterInterface The contract every adapter fulfils: setView, setData, getData, render.
AdapterAbstract Abstract base with the shared queueing/data behaviour; extend it to build your own adapter.
PurePHPAdapter · BladeAdapter · TwigAdapter The three built-in adapters.
ViewException · ViewAdapterException · ViewInvalidArgumentException The exception types; all implement ViewExceptionInterface.

Start here

The adapters at a glance

Adapter Class Engine Needs Best for
Pure PHP Adapters\PurePHPAdapter Plain .php templates core only zero-dependency rendering, small apps, partials
Blade Adapters\BladeAdapter Laravel Blade illuminate/view rich templating, layouts, directives
Twig Adapters\TwigAdapter Symfony Twig twig/twig sandboxed templates, designer-friendly syntax

What you get

  • One interface, three engines. Your code depends on ViewAdapterInterface, never on the concrete engine. Switching is a one-line change at bootstrap.
  • A tiny surface. Queue views with setView(), attach data with setData(), render with render(). That is the whole model.
  • A global helper. view($names, $data) covers the common case in one call, behind a function_exists() guard so it never clashes with your framework.
  • Safe Pure PHP rendering. Templates run in an isolated scope (no access to $this), data cannot collide with the renderer's internals, and resolved paths are confined to the view directory.
  • Reusable adapters. render() clears its state afterwards, so one adapter instance serves many independent renders without leakage.
  • One catchable exception family. Every error implements ViewExceptionInterface.
  • Extensible. Add your own engine by extending AdapterAbstract.

At a glance — behaviour

You call You get
view('home') renders home and returns the output
view(['a', 'b'], $data) a then b, concatenated, sharing $data
view('profile', $object) object's public properties become template data
View::getData('k', 'x') the value for k, or "x" if it is absent
view('home') before View::via() throws ViewException
view('home.php') (Pure PHP) same as 'home' — the .php is not doubled
view('dashboard/index') (Pure PHP) views/dashboard/index.php
view('missing') (Pure PHP) throws ViewException — file not found
view('../secret') (Pure PHP) throws ViewException — outside the view directory

Package metadata

If something in this wiki is unclear, wrong, or out of date, open an issue — documentation fixes are merged eagerly.

Clone this wiki locally