nixphp/view
Composer 安装命令:
composer require nixphp/view
包简介
NixPHP View Plugin with simple templating.
README 文档
README
nixphp/view
A lightweight, native PHP templating system — with layout inheritance and block support.
This plugin brings a clean, minimal templating system to your NixPHP application. It lets you define base layouts, use content blocks, and safely output user data — all with pure PHP.
🧩 Part of the official NixPHP plugin collection. Install it when you need structured HTML rendering — without external engines like Twig or Blade.
📦 Features
- ✅ Define layouts and reuse views via
setLayout()andblock()/endblock() - ✅ Render views with
view('template', [...]) - ✅ Return response objects with
render('template', [...]) - ✅ Safe output via
s()(escape helper) - ✅ Fully native PHP – no new syntax or templating engine required
📥 Installation
composer require nixphp/view
The plugin auto-registers itself and adds the view(), render(), assets() and s() helpers globally.
🚀 Usage
🧱 Rendering views
Use the view() helper to render a template and return the result as a string:
$content = view('hello', ['name' => 'World']);
This is useful if you want to process or wrap the HTML manually.
Use the render() helper to return a response object instead (e.g. in your controller):
return render('hello', ['name' => 'World']);
This renders the view and wraps it in a proper response, ready to be returned from any route handler.
To load a template file in another folder, you can use the dot notation:
return render('pages.hello', ['name' => 'World']);
Or even multiple levels:
return render('pages.elements.hello', ['name' => 'World']);
This works for both view() and render().
🧩 Layouts & Blocks
Use setLayout() to define a parent layout, and block()/endblock() to inject content:
views/page.phtml
<?php $this->setLayout('layout') ?> <?php $this->block('content') ?> <h1>Hello <?= s($name) ?>!</h1> <?php $this->endblock('content') ?>
views/layout.phtml
<!doctype html>
<html>
<head>
<title>My App</title>
</head>
<body>
<?= $this->renderBlock('content') ?>
</body>
</html>
🛡️ Escape output
Use the s() helper to sanitize output (HTML-escaped):
<p><?= s($userInput) ?></p>
🎨 Asset Management (CSS & JS)
The plugin includes a small, flexible asset collector used inside layouts to include CSS and JavaScript files.
Assets are added inside views or controllers using the assets() helper:
assets()->add('/assets/style.css'); // CSS assets()->add('/assets/app.js'); // JavaScript (classic) assets()->add('/assets/main.js', 'module'); // JavaScript ES module
Output in layout files
Use assets()->render('css') or assets()->render('js') inside your layout:
<!doctype html>
<html>
<head>
<?= assets()->render('css') ?>
</head>
<body>
<?= $this->renderBlock('content') ?>
<?= assets()->render('js') ?>
</body>
</html>
What gets generated?
CSS:
<link rel="stylesheet" href="/assets/style.css">
Classic JS:
<script src="/assets/app.js"></script>
Module JS:
<script type="module" src="/assets/main.js"></script>
Internals
All paths are automatically HTML-escaped via s().
🔁 Helper Comparison
| Helper | Returns | Use case |
|---|---|---|
render() |
ResponseInterface |
Ideal for controller return values |
view() |
string |
For manual output or further processing |
assets() |
string |
Include CSS & JS files in layouts |
s() |
string |
Escape output |
🔍 Internals
view()resolves and loads.phtmltemplates from the directories listed inview:paths(defaults toviews/first withapp/views/as a fallback) before checking any registered plugin or framework views.setLayout()nests the rendered content into a wrapper view.- Blocks are buffered and stored internally until rendered.
⚙️ Configurable view locations
Set the view:paths configuration to control where templates are resolved inside your application. This plugin ships with src/config.php, which defaults to:
return [ 'view' => [ 'paths' => [ 'views', 'app/views', ], ], ];
The entries are resolved relative to BASE_PATH when they are not absolute paths, so you can place templates anywhere and order them however you need. The plugin checks each directory in order before falling back to registered plugin or framework view paths.
✅ Requirements
nixphp/framework>= 1.0
📄 License
MIT License.
nixphp/view 适用场景与选型建议
nixphp/view 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 143 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 05 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 nixphp/view 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nixphp/view 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 143
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 16
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-05-03