View Package provides basic methods for creating extendable PHP Views.
Features
- ✓ Native PHP Views, no new syntax to learn.
- ✓ Increase code reuse with view extends and inheritance.
- ✓ Built-in escaping helpers.
- ✓ Easy to extend core using macros.
- ✓ Data sharing across views.
- ✓ Framework-agnostic, will work with any project.
Installation
With Composer
composer require glowy/view
Usage
use Glowy\View\View;
// Create View instance using public method __construct()
$view = new View('welcome');
// Create View instance using global helper function view()
$view = view('welcome');
// Display View.
$view->display();
Extending
View are "macroable", which allows you to add additional methods to the View class at run time. For example, the following code adds a customMethod method to the View class:
use Glowy\Strings\View;
use Glowy\Macroable\Macroable;
View::macro('customMethod', function($arg1 = 1, $arg2 = 1) {
return $arg1 + $arg2;
});
$view = new View();
echo $view->customMethod();
echo $view->customMethod(2, 2);
The above example will output:
2
4
Escaping helpers
echo e("<a href='test'>Test</a>");
// <a href='test'>Test</a>
Methods
Method | Description |
---|---|
appendSection | Start new append section. |
denormalizeName | Denormalize view name. |
display | Displays the rendered view. |
exists | Determining If A View Exists |
extends | Extend parent view. |
fetch | Fetch view. |
fetchFirst | Fetch first view that exists in a given array of views. |
fetchUnless | Fetch view based on the negation of a given condition. |
fetchWhen | Fetch view based on a given condition. |
getData | Share data with all views. |
getFilePath | Get view file path. |
getSection | Get section. |
getShared | Share data with all views. |
hasSection | Determine if section exists. |
includeFirst | Include view and display. |
includeFirst | Include view and display. |
includeUnless | Include view and display based on the negation of a given condition. |
includeWhen | Include view and display based on a given condition. |
normalizeName | Normalize view name. |
prependSection | Start new prepend section. |
render | Render the view file and extracts the view variables before returning the generated output. |
renderUnless | Render the view file and extracts the view variables before returning the generated output based on the negation of a given condition. |
renderWhen | Render the view file and extracts the view variables before returning the generated output based on a given condition. |
setDirectory | Set views directory. |
setExtension | Set views extension. |
share | Share data with all views. |
with | Share data with all views. |
Getting Started
Methods
- appendSection Start new append section.
- denormalizeName Denormalize view name.
- display Displays the rendered view.
- exists Determining If A View Exists
- extends Extend parent view.
- fetch Fetch view.
- fetchFirst Fetch first view that exists in a given array of views.
- fetchUnless Fetch view based on the negation of a given condition.
- fetchWhen Fetch view based on a given condition.
- getData Share data with all views.
- getFilePath Get view file path.
- getSection Get section.
- getShared Share data with all views.
- hasSection Determine if section exists.
- includeFirst Include view and display.
- includeFirst Include view and display.
- includeUnless Include view and display based on the negation of a given condition.
- includeWhen Include view and display based on a given condition.
- normalizeName Normalize view name.
- prependSection Start new prepend section.
- render Render the view file and extracts the view variables before returning the generated output.
- renderUnless Render the view file and extracts the view variables before returning the generated output based on the negation of a given condition.
- renderWhen Render the view file and extracts the view variables before returning the generated output based on a given condition.
- setDirectory Set views directory.
- setExtension Set views extension.
- share Share data with all views.
- with Share data with all views.