initphp/views
Composer 安装命令:
composer require initphp/views
包简介
A small, adapter-based view rendering layer for PHP with PurePHP, Blade and Twig back-ends.
README 文档
README
A small, adapter-based view rendering layer for PHP. Write your application against one tiny interface and render with plain PHP, Blade or Twig — switching engines is a one-line change.
Requirements
- PHP 8.0 or higher
The bundled adapters need their engine only when you use them:
| Adapter | Class | Engine | Needs |
|---|---|---|---|
| Pure PHP | InitPHP\Views\Adapters\PurePHPAdapter |
Plain .php templates |
— (core only) |
| Blade | InitPHP\Views\Adapters\BladeAdapter |
Laravel Blade | illuminate/view |
| Twig | InitPHP\Views\Adapters\TwigAdapter |
Symfony Twig | twig/twig |
Installation
composer require initphp/views
Then install the engine for the adapter you want (skip this for the Pure PHP adapter):
composer require illuminate/view # for BladeAdapter composer require twig/twig # for TwigAdapter
Quick start
Register an adapter once through the View facade, then render from anywhere
with the global view() helper.
require 'vendor/autoload.php'; use InitPHP\Views\Facade\View; use InitPHP\Views\Adapters\PurePHPAdapter; View::via(new PurePHPAdapter(__DIR__ . '/views')); echo view('dashboard', ['username' => 'admin']);
views/dashboard.php:
<h1>Welcome, <?= htmlspecialchars($username) ?></h1>
Rendering multiple views
Pass a list of names to render them in order and concatenate the output:
echo view(['header', 'content', 'footer'], ['title' => 'Home']);
Passing data
$data is an associative array, or an object whose public properties are used:
$user = new stdClass(); $user->username = 'admin'; echo view('profile', $user);
The view() helper and the View facade
The view() helper is a thin wrapper over the facade:
function view(string|array $views, array|object $data = []): string;
It queues the views, attaches the data and renders — equivalent to:
echo View::setView('header', 'footer')->setData(['title' => 'Home'])->render();
Every call you make on View is forwarded to the registered adapter:
| Call | Returns | Purpose |
|---|---|---|
View::via(string|ViewAdapterInterface $adapter) |
void |
Register the adapter that backs the facade. |
View::setView(string ...$views) |
adapter | Queue one or more views, in order. |
View::setData(array|object $data) |
adapter | Merge data exposed to the views. |
View::getData(?string $key = null, mixed $default = null) |
mixed |
Read merged data (or everything when $key is null). |
View::render() |
string |
Render the queue and return the output. |
View::via() accepts a ready-made adapter instance, or the class name of an
adapter that can be built with no constructor arguments. Calling the facade
before an adapter is registered throws a ViewException.
Adapters
Pure PHP adapter
Renders ordinary .php files. The .php extension is added automatically when
missing, and each file is evaluated in an isolated scope: it receives the data
as local variables and has no access to the adapter instance ($this).
use InitPHP\Views\Facade\View; use InitPHP\Views\Adapters\PurePHPAdapter; View::via(new PurePHPAdapter(__DIR__ . '/views')); echo view('dashboard/index', ['username' => 'admin']);
A missing view file throws a ViewException.
Blade adapter
Bootstraps a standalone Blade engine — no full Laravel application required.
Install illuminate/view first.
use InitPHP\Views\Facade\View; use InitPHP\Views\Adapters\BladeAdapter; View::via(new BladeAdapter(__DIR__ . '/views', __DIR__ . '/cache')); echo view('dashboard', ['username' => 'admin']);
Both directories must exist. The first argument may also be an array of template directories. Register custom directives and conditionals on the adapter instance:
$blade = new BladeAdapter(__DIR__ . '/views', __DIR__ . '/cache'); View::via($blade); $blade->directive('datetime', static function (string $expression): string { return "<?php echo ($expression)->format('Y-m-d H:i'); ?>"; }); $blade->if('admin', static fn ($user): bool => $user->isAdmin());
The adapter also exposes the most common factory methods — make(), file(),
exists(), share(), composer(), creator(), addNamespace() and
replaceNamespace() — and forwards any other call to the underlying Blade
factory. See the Blade documentation.
Twig adapter
Bootstraps a Twig environment. Install twig/twig first.
use InitPHP\Views\Facade\View; use InitPHP\Views\Adapters\TwigAdapter; View::via(new TwigAdapter(__DIR__ . '/views', __DIR__ . '/cache')); echo view('dashboard.html.twig', ['username' => 'admin']);
Both directories must exist. Twig does not add a file extension, so include
it in the view name. Use getEnvironment() to add extensions, globals or
filters:
$twig = new TwigAdapter(__DIR__ . '/views', __DIR__ . '/cache'); $twig->getEnvironment()->addGlobal('app_name', 'InitPHP'); View::via($twig);
Exceptions
All exceptions implement InitPHP\Views\Exceptions\ViewExceptionInterface, so a
single catch can handle every failure this package raises.
| Exception | Extends | Thrown when |
|---|---|---|
ViewException |
RuntimeException |
The facade is used before an adapter is registered, or a view file is missing. |
ViewAdapterException |
ViewException |
View::via() is given an invalid adapter. |
ViewInvalidArgumentException |
InvalidArgumentException |
A view or cache directory does not exist. |
use InitPHP\Views\Exceptions\ViewExceptionInterface; try { echo view('dashboard', $data); } catch (ViewExceptionInterface $e) { // handle any InitPHP Views failure }
Custom adapters
Implement InitPHP\Views\Interfaces\ViewAdapterInterface (or extend
AdapterAbstract, which already handles queueing and data) and register it with
View::via(). See docs/custom-adapter.md.
Documentation
Full developer documentation lives in docs/:
- Getting started
- The facade and the
view()helper - Pure PHP adapter
- Blade adapter
- Twig adapter
- Writing a custom adapter
- Exceptions
Contributing
Bug reports and pull requests are welcome. Please read the org-wide contribution guide first. Run the full check suite locally before opening a PR:
composer ci # php-cs-fixer (dry-run) + phpstan + phpunit
Credits
License
Released under the MIT License. Copyright © 2022 InitPHP.
initphp/views 适用场景与选型建议
initphp/views 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 46 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 01 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「twig」 「template」 「view」 「Rendering」 「views」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 initphp/views 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 initphp/views 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 initphp/views 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
This Symfony bundle integrates PhpSpreadsheet into Symfony using Twig.
A Twig extension to insert css as inline styles with a tag
Caching and compression for Twig assets (JavaScript and CSS).
Twig extensions for Tracy Debugger
Provides code-quality helpers to your Twig templates.
统计信息
- 总下载量: 46
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 15
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-01-14