fyre / view
A template rendering library.
Installs: 167
Dependents: 3
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 1
Open Issues: 0
pkg:composer/fyre/view
Requires
- fyre/container: ^1.0
- fyre/csp: ^7.0
- fyre/csrf: ^5.0
- fyre/formatter: ^4.0
- fyre/formbuilder: ^3.0
- fyre/htmlhelper: ^3.0
- fyre/lang: ^5.0
- fyre/macro: ^1.0
- fyre/orm: ^12.0
- fyre/path: ^2.0
- fyre/router: ^8.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.59
- fyre/php-cs-fixer-config: ^1.0
- phpunit/phpunit: ^12
- dev-main
- v11.0.2
- v11.0.1
- v11.0
- v10.1.11
- v10.1.10
- v10.1.9
- v10.1.8
- v10.1.7
- v10.1.6
- v10.1.5
- v10.1.4
- v10.1.3
- v10.1.2
- v10.1.1
- v10.1.0
- v10.0.1
- v10.0
- v9.0.1
- v9.0
- v8.0
- v7.1.0
- v7.0.1
- v7.0
- v6.1.4
- v6.1.3
- v6.1.2
- v6.1.1
- v6.1.0
- v6.0.1
- v6.0
- v5.0.5
- v5.0.4
- v5.0.3
- v5.0.2
- v5.0.1
- 4.0.1
- v3.0.2
- v3.0.1
- v3.0
- v2.0.1
- v2.0
- v1.1.0
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0
This package is auto-updated.
Last update: 2025-10-21 07:30:31 UTC
README
FyreView is a free, open-source template rendering library for PHP.
Table Of Contents
- Installation
- Basic Usage
- Methods
- Layouts
- Cells
- Elements
- Blocks
- Template Locator
- Cell Registry
- Helper Registry
- Helpers
Installation
Using Composer
composer require fyre/view
In PHP:
use Fyre\View\View;
Basic Usage
$templateLocatoris a TemplateLocator.$helperRegistryis a HelperRegistry.$cellRegistryis a CellRegistry.$requestis a ServerRequest.
$view = new View($templateLocator, $helperRegistry, $cellRegistry, $request);
Any dependencies will be injected automatically when loading from the Container.
$view = $container->use(View::class);
Methods
Get Data
Get the view data.
$data = $view->getData();
Get Layout
Get the layout.
$layout = $view->getLayout();
Get Request
Get the ServerRequest.
$request = $view->getRequest();
Load Helper
Load a Helper.
$nameis a string representing the helper name.$optionsis an array containing helper options.
$helper = $view->loadHelper($name, $options);
Render
Render a template.
$fileis a string representing the template file.
echo $view->render($file);
Templates files must end in the extension .php, and must exist in one of the defined TemplateLocator paths.
Set Data
Set view data.
$datais an array containing data to pass to the template.
$view->setData($data);
Set Layout
Set the layout.
$layoutis a string representing the layout file.
$view->setLayout($layout);
Layout files must end in the extension .php, and must exist in a "layouts" folder in one of the defined TemplateLocator paths.
Layouts
You can use layouts when rendering views by placing a default.php file in a layouts folder of one of the defined TemplateLocator paths. You can create multiple layouts, and specify the layout to use with the setLayout method above.
The rendered content is passed to the layout file via the content method of $this. Any other defined data is also passed to the layout.
$this->content();
Cells
Custom cells can be created by extending \Fyre\View\Cell, and suffixing the class name with "Cell".
Cell
Render a Cell.
$cellis a string, and can either represent the cell name (implementing adisplaymethod) or in the format of "Cell::method".$argsis an array of arguments that will be passed to the cell method, and will default to [].
echo $this->cell($cell, $args);
Cell classes must exist in a namespace that will be loaded by the CellRegistry.
Elements
Element
Render an element.
$fileis a string representing the element file.$datais an array containing data to pass to the element, and will default to [].
echo $this->element($file, $data);
Element files must end in the extension .php, and must exist in an "elements" folder in one of the defined TemplateLocator paths.
Blocks
Append
Append content to a block.
$nameis a string representing the block name.
$this->append($name);
Any output until the block is ended will be appended to the block.
Assign
Assign content to a block.
$nameis a string representing the block name.$contentis a string representing the content.
$this->assign($name, $content);
End
End a block.
$this->end();
Fetch
Fetch a block.
$nameis a string representing the block name.$defaultis a string representing the default value, and will default to "".
$block = $this->fetch($name, $default);
Prepend
Prepend content to a block.
$nameis a string representing the block name.
$this->prepend($name);
Any output until the block is ended will be prepended to the block.
Reset
Reset content of a block.
$nameis a string representing the block name.
$this->reset($name);
Start
$nameis a string representing the block name.
Start content for a block.
$this->start($name);
Template Locator
use Fyre\View\TemplateLocator;
$templateLocator = new TemplateLocator();
Autoloading
It is recommended to bind the TemplateLocator to the Container as a singleton.
$container->singleton(TemplateLocator::class);
Template Locator Methods
Add Path
Add a path for loading templates.
$pathis a string representing the path.
$templateLocator->addPath($path);
Get Paths
Get the paths.
$paths = $templateLocator->getPaths();
Locate
Find a file in paths.
$fileis a string representing the file name.$folderis a string representing the folder name, and will default to "".
$filePath = $templateLocator->findFile($file, $folder);
Remove Path
$pathis a string representing the path.
$templateLocator->removePath($path);
Template Locator Static Methods
Normalize
Normalize a template file name.
$stringis a string representing the file name.
$normalized = TemplateLocator::normalize($string);
Cell Registry
use Fyre\View\CellRegistry;
$containeris a Container.
$cellRegistry = new CellRegistry($container);
Autoloading
It is recommended to bind the CellRegistry to the Container as a singleton.
$container->singleton(CellRegistry::class);
Any dependencies will be injected automatically when loading from the Container.
$cellRegistry = $container->use(CellRegistry::class);
Cell Registry Methods
Add Namespace
Add a namespace for automatically loading cells.
$namespaceis a string representing the namespace.
$cellRegistry->addNamespace($namespace);
Build
Build a cell.
$nameis a string representing the cell name.$viewis a View.$optionsis an array containing cell options.
$cell = $cellRegistry->build($name, $view, $options);
Clear
Clear all namespaces and cells.
$cellRegistry->clear();
Find
Find a cell class.
$nameis a string representing the cell name.
$className = $cellRegistry->find($name);
Get Namespaces
Get the namespaces.
$namespaces = $cellRegistry->getNamespaces();
Has Namespace
Check if a namespace exists.
$namespaceis a string representing the namespace.
$hasNamespace = $cellRegistry->hasNamespace($namespace);
Remove Namespace
Remove a namespace.
$namespaceis a string representing the namespace.
$cellRegistry->removeNamespace($namespace);
Helper Registry
use Fyre\View\HelperRegistry;
$containeris a Container.
$helperRegistry = new HelperRegistry($container);
Autoloading
It is recommended to bind the HelperRegistry to the Container as a singleton.
$container->singleton(HelperRegistry::class);
Any dependencies will be injected automatically when loading from the Container.
$helperRegistry = $container->use(HelperRegistry::class);
Helper Registry Methods
Add Namespace
Add a namespace for automatically loading helpers.
$namespaceis a string representing the namespace.
$helperRegistry->addNamespace($namespace);
Build
Build a helper.
$nameis a string representing the helper name.$viewis a View.$optionsis an array containing helper options.
$helper = $helperRegistry->build($name, $view, $options);
Clear
Clear all namespaces and helpers.
$helperRegistry->clear();
Find
Find a helper class.
$nameis a string representing the helper name.
$className = $helperRegistry->find($name);
Get Namespaces
Get the namespaces.
$namespaces = $helperRegistry->getNamespaces();
Has Namespace
Check if a namespace exists.
$namespaceis a string representing the namespace.
$hasNamespace = $helperRegistry->hasNamespace($namespace);
Remove Namespace
Remove a namespace.
$namespaceis a string representing the namespace.
$helperRegistry->removeNamespace($namespace);
Helpers
Helpers can be used inside of templates or elements using the class name as a property of $this.
$helper = $this->MyHelper;
Alternatively, you can load a helper with configuration options using the loadHelper method of the View.
Custom helpers can be created by extending \Fyre\View\Helper, suffixing the class name with "Helper", and ensuring the __construct method accepts View as the argument (and optionally an $options array as the second parameter).
Get Config
Get the configuration options.
$config = $helper->getConfig();
Get View
Get the View.
$view = $helper->getView();
CSP
The CSP helper allows an easy way to generate nonces, and automatically add them to your CSP policies.
Script Nonce
Generate a script nonce.
$nonce = $this->CSP->scriptNonce();
Style Nonce
Generate a style nonce.
$nonce = $this->CSP->styleNonce();
Form
Button
Render a button element.
$contentis a string representing the button content.$optionsis an array of options for rendering the button.
$button = $this->Form->button($content, $options);
By default, the button content will be HTML escaped. To disable this, set the escape value to false in the options array.
All other options will be created as attributes on the button element.
Close
Render a form close tag.
$close = $this->Form->close();
Fieldset Close
Render a fieldset close tag.
$fieldsetClose = $this->Form->fieldsetClose();
Fieldset Open
Render a fieldset open tag.
$optionsis an array of options for rendering the fieldset.
$fieldset = $this->Form->fieldsetOpen($options);
All options will be created as attributes on the fieldset element.
Input
Render an input element.
$keyis a string representing the field key, using dot notation.$optionsis an array of options for rendering the label.
$input = $this->Form->input($key, $options);
All options will be created as attributes on the input element.
- The default
idandnameattributes will be converted from the field key. - The input
typeand other default attributes will be determined from the Table and Model Validation. - The default value will be retrieved from the ServerRequest
$_POSTdata or form context. - Select options can be specified using the
optionskey. - Checkboxes and radio inputs can be marked as checked by setting the
checkedoption to true. - Multiple select menus will (by default) render a hidden field with an empty value. This can be disabled by setting the
hiddenFieldoption to false. - Checkboxes will (by default) render a hidden field with the value "0". This can be disabled by setting the
hiddenFieldoption to false.
You can also use the following helper methods to generate specific input type fields.
$input = $this->Form->checkbox($key, $options); $input = $this->Form->color($key, $options); $input = $this->Form->date($key, $options); $input = $this->Form->datetime($key, $options); $input = $this->Form->email($key, $options); $input = $this->Form->file($key, $options); $input = $this->Form->hidden($key, $options); $input = $this->Form->image($key, $options); $input = $this->Form->month($key, $options); $input = $this->Form->number($key, $options); $input = $this->Form->password($key, $options); $input = $this->Form->radio($key, $options); $input = $this->Form->range($key, $options); $input = $this->Form->reset($key, $options); $input = $this->Form->search($key, $options); $input = $this->Form->select($key, $options); $input = $this->Form->selectMulti($key, $options); $input = $this->Form->submit($key, $options); $input = $this->Form->tel($key, $options); $input = $this->Form->text($key, $options); $input = $this->Form->time($key, $options); $input = $this->Form->url($key, $options); $input = $this->Form->week($key, $options);
Label
Render a label element.
$keyis a string representing the field key.$optionsis an array of options for rendering the label.
$label = $this->Form->label($key, $options);
The label text will be retrieved from the Lang using the Field.{field_name} key or converted from the field name. You can also set custom label text by setting the text option.
By default, the label content will be HTML escaped. To disable this, set the escape value to false in the options array.
All other options will be created as attributes on the label element. The default for attribute will be converted from the field key.
Legend
Render a legend element.
$contentis a string representing the legend content.$optionsis an array of options for rendering the legend.
$legend = $this->Form->legend($content, $options);
By default, the legend content will be HTML escaped. To disable this, set the escape value to false in the options array.
All other options will be created as attributes on the legend element.
Open
Render a form open tag.
$itemis an Entity or other (supported) object representing the form context, and will default to null$optionsis an array of options for rendering the form.
$open = $this->Form->open($item, $options);
All options will be created as attributes on the form element.
Open Multipart
Render a multipart form open tag.
$itemis an array or object representing the form context, and will default to null$optionsis an array of options for rendering the form.
$open = $this->Form->openMultipart($item, $options);
All options will be created as attributes on the form element.
Format
The format helper provides a convenient wrapper for Formatter methods.
Url
The URL helper provides methods for generating Router links.
Link
Generate an anchor link for a destination.
$contentis a string representing the link content.$optionsis an array containing options.escapeis a boolean indicating whether to escape the link content, and will default to true.fullBaseis a boolean indicating whether to use the full base URI, and will default to false.
$link = $this->Url->link($content, $options);
Path
Generate a url for a relative path.
$pathis a string representing the file path.$optionsis an array containing options.fullBaseis a boolean indicating whether to use the full base URI, and will default to false.
$url = $this->Url->path($path, $options);
To
Generate a url for a named route.
$nameis a string representing the route alias.$argumentsis an array containing the route arguments.?is an array containing route query parameters.#is a string representing the fragment component of the URI.
$optionsis an array containing options.fullBaseis a boolean indicating whether to use the full base URI, and will default to false.
$url = $this->Url->to($destination, $options);