Download the PHP package coroq/view without Composer
On this page you can find all versions of the php package coroq/view. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package view
Short Description Renders plain PHP view template files with data
License MIT
Informations about the package view
coroq/view
Renders plain PHP view template files with data.
Requirements
- PHP 8.0+
Installation
Quick Start
Suppose you have view templates organized like this:
In your code:
Your template file at view/welcome.php:
The included view/common/header.php:
Benefits
Centralized base directory: Define template directory once in the constructor, avoiding scattered __DIR__ . '/../../../view/...' paths.
String return: Returns rendered output as a string instead of echoing directly. Useful for creating PSR-7 responses. Eliminates manual ob_start() and ob_get_clean() wrapping.
Variable isolation: Template variables are isolated from controller logic. Templates receive only the variables they need. Variables are accessed directly as $username instead of $data['username'].
Usage
render()
render(string $template, array $data): string
Renders a template and returns the output as a string.
$template- Path to the template file, relative to the base directory. Must not contain absolute paths or directory traversal (../).$data- Associative array of variables to pass to the template. Keys become variable names.- Returns: The rendered HTML as a string.
include()
include(string $template, array $data): void
Renders a template and echoes the output directly. Use within templates as $this->include().
- Parameters: Same as
render(). - Returns: Nothing (outputs directly).
templateExists()
templateExists(string $template): bool
Checks if a template file exists.
$template- Same asrender().- Returns:
trueif the template file exists,falseotherwise.
Useful when rendering templates based on user-provided paths:
Subclass for helpers
Define reusable components as methods in a ViewRenderer subclass:
Template using helpers:
Multiple view classes
Separate view classes for different areas of your application:
HTML escaping
HTML escaping is out of scope for this library. You have three options:
1. Use htmlspecialchars() directly
2. Use an HTML handling library like coroq/html
3. Define escape method in ViewRenderer subclass
License
MIT