Download the PHP package vaibhavpandeyvpz/phew without Composer
On this page you can find all versions of the php package vaibhavpandeyvpz/phew. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download vaibhavpandeyvpz/phew
More information about vaibhavpandeyvpz/phew
Files in vaibhavpandeyvpz/phew
Package phew
Short Description Simple (just 2 classes) and native PHP view engine with support for inheritance (layouts).
License MIT
Homepage https://github.com/vaibhavpandeyvpz/phew
Informations about the package phew
vaibhavpandeyvpz/phew
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:
Quick Start
Basic Usage
templates/welcome.php:
Using ArrayAccess for Variables
templates/page.php:
Layout Inheritance
Layouts allow you to define a base template and inject content into it:
templates/layout.php:
templates/page.php:
Blocks
Blocks allow you to define reusable content sections:
Block Append Mode
You can append content to existing blocks:
Partials
Partials are reusable template fragments:
templates/partials/header.php:
templates/page.php:
Namespaces
Organize templates using namespaces:
Custom Functions
Register custom functions that can be called from templates:
templates/page.php:
HTML Escaping
Use the e() method to escape HTML output:
Extensions
Create extensions to add custom functionality:
Error Handling
The engine throws Phew\ViewException when errors occur:
Advanced Examples
Complex Layout with Multiple Blocks
templates/layout.php:
templates/article.php:
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.