vaibhavpandeyvpz/phew
Composer 安装命令:
composer require vaibhavpandeyvpz/phew
包简介
Simple (just 2 classes) and native PHP view engine with support for inheritance (layouts).
README 文档
README
A simple, lightweight, and native PHP view engine with support for template inheritance (layouts), blocks, partials, and namespaces. Built with just 2 core classes and zero dependencies.
Features
- 🚀 Lightweight: Just 2 core classes, zero dependencies
- 📦 Native PHP: No compilation, no special syntax to learn
- 🎨 Layout Inheritance: Extend parent templates with blocks
- 🧩 Partials: Reusable template fragments
- 📁 Namespaces: Organize templates with namespaces
- 🔧 Extensible: Register custom functions and extensions
- ✅ Type Safe: Full PHP 8.2+ type hints and strict types
- 🧪 Well Tested: 100% code coverage
Requirements
- PHP 8.2 or higher
Installation
Install via Composer:
composer require vaibhavpandeyvpz/phew
Quick Start
Basic Usage
<?php use Phew\View; // Create a view instance $view = new View(); // Add template folder $view->add(__DIR__ . '/templates'); // Render a template echo $view->fetch('welcome.php', ['name' => 'World']);
templates/welcome.php:
Hello, <?= $name ?>!
Using ArrayAccess for Variables
<?php use Phew\View; $view = new View(); $view->add(__DIR__ . '/templates'); // Set variables using array syntax $view['title'] = 'My Page'; $view['user'] = ['name' => 'John', 'email' => 'john@example.com']; // Variables are available in all templates echo $view->fetch('page.php');
templates/page.php:
<h1><?= $title ?></h1> <p>Welcome, <?= $user['name'] ?>!</p>
Layout Inheritance
Layouts allow you to define a base template and inject content into it:
templates/layout.php:
<!DOCTYPE html> <html> <head> <title><?= $this->block('title') ?></title> </head> <body> <header><?= $this->block('header') ?></header> <main><?= $this->block('content') ?></main> </body> </html>
templates/page.php:
<?php $this->begin('title'); ?>My Page Title<?php $this->end(); $this->begin('header'); ?>Welcome<?php $this->end(); $this->layout('layout.php'); ?> <p>This is the main content.</p>
Blocks
Blocks allow you to define reusable content sections:
<?php // Define a block $this->begin('sidebar'); ?> <ul> <li>Item 1</li> <li>Item 2</li> </ul> <?php $this->end(); // Use the block later echo $this->block('sidebar');
Block Append Mode
You can append content to existing blocks:
<?php // First definition $this->begin('scripts', true); ?>console.log('First script');<?php $this->end(); // Appends to existing block $this->begin('scripts', true); ?>console.log('Second script');<?php $this->end(); // Outputs both scripts echo $this->block('scripts');
Partials
Partials are reusable template fragments:
templates/partials/header.php:
<header>
<h1><?= $title ?></h1>
<nav>...</nav>
</header>
templates/page.php:
<?= $this->fetch('partials/header.php', ['title' => 'My Site']) ?> <main>Content here</main>
Namespaces
Organize templates using namespaces:
<?php $view = new View(); // Add folders with namespaces $view->add(__DIR__ . '/admin/templates', 'admin'); $view->add(__DIR__ . '/public/templates', 'public'); // Use namespace when fetching echo $view->fetch('admin:dashboard.php'); echo $view->fetch('public:home.php');
Custom Functions
Register custom functions that can be called from templates:
<?php $view = new View(); $view->add(__DIR__ . '/templates'); // Register a function $view->register('uppercase', fn(string $str) => strtoupper($str)); $view->register('formatDate', function($date) { return date('Y-m-d', strtotime($date)); });
templates/page.php:
<p><?= $this->uppercase('hello world') ?></p> <p><?= $this->formatDate('2024-01-01') ?></p>
HTML Escaping
Use the e() method to escape HTML output:
<?php $userInput = '<script>alert("XSS")</script>'; ?> <p><?= $this->e($userInput) ?></p> <!-- Outputs: <script>alert("XSS")</script> -->
Extensions
Create extensions to add custom functionality:
<?php use Phew\ExtensionInterface; use Phew\ViewInterface; class MyExtension implements ExtensionInterface { public function extend(ViewInterface $view): void { $view->register('myFunction', fn() => 'Hello from extension!'); } } $view = new View(); $view->install(new MyExtension());
Error Handling
The engine throws Phew\ViewException when errors occur:
<?php use Phew\View; use Phew\ViewException; try { $view = new View(); echo $view->fetch('nonexistent.php'); } catch (ViewException $e) { echo 'Template error: ' . $e->getMessage(); }
Advanced Examples
Complex Layout with Multiple Blocks
templates/layout.php:
<!DOCTYPE html> <html> <head> <title><?= $this->block('title') ?> - <?= $siteName ?></title> <?= $this->block('head') ?> </head> <body> <?= $this->block('header') ?> <main><?= $this->block('content') ?></main> <?= $this->block('footer') ?> </body> </html>
templates/article.php:
<?php $this->begin('title'); ?>Article Title<?php $this->end(); $this->begin('head'); ?><meta name="description" content="Article description"><?php $this->end(); $this->begin('header'); ?><?= $this->fetch('partials/nav.php') ?><?php $this->end(); $this->begin('content'); ?> <article> <h1><?= $article['title'] ?></h1> <p><?= $article['content'] ?></p> </article> <?php $this->end(); $this->begin('footer'); ?><?= $this->fetch('partials/footer.php') ?><?php $this->end(); $this->layout('layout.php', ['siteName' => 'My Blog']);
API Reference
View Class
add(string $folder, string $ns = ''): static- Add a template folderfetch(string $template, ?array $vars = null): string- Render a templatefind(string $template): string- Find template pathregister(string $function, callable $callback): static- Register a functioninvoke(string $function, array $arguments): mixed- Invoke a registered functioninstall(ExtensionInterface $extension): static- Install an extension- Implements
ArrayAccessfor variable management
Template Methods (available in templates)
$this->begin(string $name, bool $append = false): void- Start a block$this->end(): void- End current block$this->block(string $name): string- Get block content$this->layout(string $name, ?array $vars = null): void- Set layout$this->fetch(string $name, ?array $vars = null): string- Render partial$this->e(mixed $value): string- Escape HTML$this->{function}(...$args)- Call registered function
License
This project is licensed under the MIT License. See the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Support
For issues, questions, or feature requests, please use the GitHub issue tracker.
vaibhavpandeyvpz/phew 适用场景与选型建议
vaibhavpandeyvpz/phew 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 278 次下载、GitHub Stars 达 2, 最近一次更新时间为 2016 年 04 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「template」 「view」 「engine」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 vaibhavpandeyvpz/phew 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 vaibhavpandeyvpz/phew 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 vaibhavpandeyvpz/phew 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
A SilverStripe module to optimise the Meta, crawling, indexing, and sharing of your website content (forked from Cyber-Duck/Silverstripe-SEO)
A Processor for Markup written in PHP. Allows extraction of Markup into a data structure, orchestrated nested manipulation of said structure, and output as (optimized) Markup.
Illuminate View for Morningmedley.
Blade template for Kirby
Allow KoolReport to use twig template engine to render view
统计信息
- 总下载量: 278
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 11
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-04-30