-
-
Notifications
You must be signed in to change notification settings - Fork 0
API Reference
Every public member of initphp/views. For narrative explanations, follow the
links into the concept pages.
InitPHP\Views\Facade\View — static entry point. See
The View Facade & Helper. The facade is final and
cannot be instantiated.
public static function via(string|ViewAdapterInterface $adapter): voidRegister the adapter that backs the facade. Accepts an adapter instance, or the
class name of an adapter constructible with no arguments. Throws
ViewAdapterException if the class is missing, does not implement
ViewAdapterInterface, or needs constructor arguments.
Every other static call is forwarded to the registered adapter via
__callStatic(), throwing ViewException if no adapter has
been registered:
public static function setView(string ...$views): ViewAdapterInterface
public static function setData(array|object $data): ViewAdapterInterface
public static function getData(?string $key = null, mixed $default = null): mixed
public static function render(): stringFor the Blade adapter, engine-specific methods such as
directive() are forwarded too.
Global function, registered via Composer autoloading (guarded by
function_exists).
function view(string|array $views, array|object $data = []): stringQueue one or more views, attach $data, render and return the output. A string
renders one view; an array renders several in order. Throws
ViewException if no adapter is registered or a view cannot be
found. See The View Facade & Helper.
InitPHP\Views\Interfaces\ViewAdapterInterface — the contract every adapter
fulfils.
public function setView(string ...$views): static;Queue one or more view names, appended in order. Returns $this.
public function setData(array|object $data): static;Merge data exposed to the queued views. An object is reduced to its public
properties; repeated keys overwrite earlier values. Returns $this.
public function getData(?string $key = null, mixed $default = null): mixed;Read one value, or — when $key is null — the whole data array. Returns
$default when the key is absent. A stored null is distinct from a missing
key.
public function render(): string;Render every queued view and return the concatenated output.
InitPHP\Views\Adapters\AdapterAbstract implements ViewAdapterInterface — shared
behaviour for adapters. See Custom Adapters.
Implements setView(), setData(), getData() and:
public function __toString(): stringRenders the queued views — equivalent to render().
abstract public function render(): string;The one method concrete adapters must implement.
protected function flush(): voidClear the queued views and merged data. Call this after a render so the instance can be reused.
Protected state available to subclasses:
-
protected array $views—list<string>, queued view names. -
protected array $data—array<string, mixed>, merged data.
InitPHP\Views\Adapters\PurePHPAdapter extends AdapterAbstract. See
Pure PHP Adapter.
public function __construct(string $viewDir)$viewDir must exist. Throws ViewInvalidArgumentException
otherwise.
public function render(): stringInclude the queued .php files (the extension is added when missing) in an
isolated scope and return their combined output, then reset state. Throws
ViewException if a view file is not found or resolves outside
$viewDir.
InitPHP\Views\Adapters\BladeAdapter extends AdapterAbstract. Requires
illuminate/view. See Blade Adapter.
public function __construct(
string|array $viewDir,
string $cacheDir,
?\Illuminate\Container\Container $container = null
)Each directory must exist. Throws ViewInvalidArgumentException
otherwise.
public function render(): string
public function make(string $view, array $data = [], array $mergeData = []): \Illuminate\Contracts\View\View
public function file(string $path, array $data = [], array $mergeData = []): \Illuminate\Contracts\View\View
public function exists(string $view): bool
public function share(array|string $key, mixed $value = null): mixed
public function composer(array|string $views, \Closure|string $callback): array
public function creator(array|string $views, \Closure|string $callback): array
public function addNamespace(string $namespace, array|string $hints): static
public function replaceNamespace(string $namespace, array|string $hints): static
public function directive(string $name, callable $handler): void
public function if(string $name, callable $callback): voidAny other call is forwarded to Illuminate\View\Factory via __call() — for
example getShared(), shared(), addLocation().
InitPHP\Views\Adapters\TwigAdapter extends AdapterAbstract. Requires
twig/twig. See Twig Adapter.
public function __construct(string $viewDir, string $cacheDir)Both directories must exist. Throws
ViewInvalidArgumentException otherwise.
public function render(): stringRender each queued template (include the file extension in the name) and return the combined output, then reset state.
public function getEnvironment(): \Twig\EnvironmentThe underlying Twig environment — add extensions, globals and filters through it.
InitPHP\Views\Exceptions. See Exceptions.
interface ViewExceptionInterface extends \Throwable
class ViewException extends \RuntimeException implements ViewExceptionInterface
class ViewAdapterException extends ViewException
class ViewInvalidArgumentException extends \InvalidArgumentException implements ViewExceptionInterfaceEvery concrete exception implements ViewExceptionInterface, so one catch
covers the whole package.
initphp/views · MIT License · part of the InitPHP family
Source · Issues · Discussions · Packagist · Contributing · Security Policy
Getting Started
Adapters
Reference
Guides
Other