PHPackages                             rotexsoft/file-renderer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. rotexsoft/file-renderer

ActiveLibrary[Templating &amp; Views](/categories/templating)

rotexsoft/file-renderer
=======================

A tool for rendering PHP files that are meant to generate output. Easy to use as a view component in an MVC framework.

8.0.1(3mo ago)55.0k1[1 issues](https://github.com/rotexsoft/file-renderer/issues)2BSD-3-ClausePHPPHP &gt;=8.2CI passing

Since Apr 21Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/rotexsoft/file-renderer)[ Packagist](https://packagist.org/packages/rotexsoft/file-renderer)[ Docs](https://github.com/rotexsoft/file-renderer)[ RSS](/packages/rotexsoft-file-renderer/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (5)Versions (34)Used By (2)

PHP File Renderer
=================

[](#php-file-renderer)

[![PHP Tests and Code Quality Tools](https://github.com/rotexsoft/file-renderer/workflows/Run%20PHP%20Tests%20and%20Code%20Quality%20Tools/badge.svg)](https://github.com/rotexsoft/file-renderer/actions?query=workflow%3A%22Run+PHP+Tests+and+Code+Quality+Tools%22) [![GitHub release (latest SemVer)](https://camo.githubusercontent.com/d120df24d14e4bc69a62718ef2236fec4c772a9ada08afd3cff30a9357ae8009/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f726f746578736f66742f66696c652d72656e6465726572)](https://camo.githubusercontent.com/d120df24d14e4bc69a62718ef2236fec4c772a9ada08afd3cff30a9357ae8009/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f726f746578736f66742f66696c652d72656e6465726572) [![GitHub](https://camo.githubusercontent.com/8a7f8b91af1a847a20f4436ca1ef2293dd19974b659cb7e1871c05d148cd729f/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f726f746578736f66742f66696c652d72656e6465726572)](https://camo.githubusercontent.com/8a7f8b91af1a847a20f4436ca1ef2293dd19974b659cb7e1871c05d148cd729f/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f726f746578736f66742f66696c652d72656e6465726572) [![Coverage Status](https://camo.githubusercontent.com/1d6112a0af2eb5e4fef3c15477ee17b7dabc32727785d2844481ff6179683f17/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f726f746578736f66742f66696c652d72656e64657265722f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/rotexsoft/file-renderer?branch=master) [![GitHub code size in bytes](https://camo.githubusercontent.com/85bae88917db7f152a5a9cb1bfb769f8881f5c1cf061e8456d5352585707d4e1/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c616e6775616765732f636f64652d73697a652f726f746578736f66742f66696c652d72656e6465726572)](https://camo.githubusercontent.com/85bae88917db7f152a5a9cb1bfb769f8881f5c1cf061e8456d5352585707d4e1/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c616e6775616765732f636f64652d73697a652f726f746578736f66742f66696c652d72656e6465726572) [![Packagist Downloads](https://camo.githubusercontent.com/bf76169a1d790a2a042ac5940d214ce435f688510b29239995dc6d9da9946e84/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f726f746578736f66742f66696c652d72656e6465726572)](https://camo.githubusercontent.com/bf76169a1d790a2a042ac5940d214ce435f688510b29239995dc6d9da9946e84/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f726f746578736f66742f66696c652d72656e6465726572) [![GitHub top language](https://camo.githubusercontent.com/bebdc1157a7780d9f917d88b2a4cde260da7d478ada3428980cd67547306602c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c616e6775616765732f746f702f726f746578736f66742f66696c652d72656e6465726572)](https://camo.githubusercontent.com/bebdc1157a7780d9f917d88b2a4cde260da7d478ada3428980cd67547306602c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c616e6775616765732f746f702f726f746578736f66742f66696c652d72656e6465726572) [![Packagist PHP Version Support](https://camo.githubusercontent.com/5091f3bed3eee8540f56a2c1f59bbc027c2c0a6f2d821b450bd808cf428749e2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f726f746578736f66742f66696c652d72656e6465726572)](https://camo.githubusercontent.com/5091f3bed3eee8540f56a2c1f59bbc027c2c0a6f2d821b450bd808cf428749e2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f726f746578736f66742f66696c652d72656e6465726572) [![GitHub commits since latest release (by date)](https://camo.githubusercontent.com/edfeaf304967cf88822adf534f3714107d8ed59f33004f536f081beda2b5bddb/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f636f6d6d6974732d73696e63652f726f746578736f66742f66696c652d72656e64657265722f6c6174657374)](https://camo.githubusercontent.com/edfeaf304967cf88822adf534f3714107d8ed59f33004f536f081beda2b5bddb/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f636f6d6d6974732d73696e63652f726f746578736f66742f66696c652d72656e64657265722f6c6174657374) [![GitHub last commit](https://camo.githubusercontent.com/3c528388d1ad86abfcf46bcdb48fc48f47efd83c77903465849a63de46202732/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6173742d636f6d6d69742f726f746578736f66742f66696c652d72656e6465726572)](https://camo.githubusercontent.com/3c528388d1ad86abfcf46bcdb48fc48f47efd83c77903465849a63de46202732/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6173742d636f6d6d69742f726f746578736f66742f66696c652d72656e6465726572) [![GitHub Release Date](https://camo.githubusercontent.com/4a5945d8f80df055ce10ed66b7cd179ba9e5104ffce04bba1e8adee1efa330bd/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652d646174652f726f746578736f66742f66696c652d72656e6465726572)](https://camo.githubusercontent.com/4a5945d8f80df055ce10ed66b7cd179ba9e5104ffce04bba1e8adee1efa330bd/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652d646174652f726f746578736f66742f66696c652d72656e6465726572) [![Libraries.io dependency status for GitHub repo](https://camo.githubusercontent.com/dfa63b96c8616450b1bf326eeda78415923f56d1fc4fa045b338553e90034479/68747470733a2f2f696d672e736869656c64732e696f2f6c6962726172696573696f2f6769746875622f726f746578736f66742f66696c652d72656e6465726572)](https://libraries.io/packagist/rotexsoft%2Ffile-renderer)

- [PHP File Renderer](#php-file-renderer)
    - [Introduction](#introduction)
        - [Main Features](#main-features)
    - [Acknowledgement](#acknowledgement)
    - [Changelog](#changelog)
    - [Contribution](#contribution)
        - [Running Tests](#running-tests)
    - [Requirements](#requirements)
    - [Installation](#installation)
    - [Usage](#usage)
        - [Basic Usage](#basic-usage)
        - [Getting, Setting and Unsetting View Data](#getting-setting-and-unsetting-view-data)
            - [Setting View Data](#setting-view-data)
            - [Getting View Data](#getting-view-data)
            - [Unsetting View Data](#unsetting-view-data)
        - [File paths](#file-paths)
        - [Escaping Data to be Passed to Views](#escaping-data-to-be-passed-to-views)
            - [Using Wild Cards in Escape Specification](#using-wild-cards-in-escape-specification)
        - [Advanced Usage](#advanced-usage)
            - [Implementing a Two-Step View Templating System](#implementing-a-two-step-view-templating-system)
                - [Sharing Data between Layout and Page Content Views](#sharing-data-between-layout-and-page-content-views)
            - [Nesting Renderers](#nesting-renderers)

Introduction
------------

[](#introduction)

This is a simple, elegant and flexible tool that can be used to render php files (also referred to as `Views` within this documentation) that emit valid html output to a web-browser. It is designed to be unobtrusive; your view files can easily be used with a different rendering or templating library. You do not need to learn any new syntax (or markup / templating language) in order to compose your views; simply write your views in plain old php. This package also provides escaping functionality for data passed to the view file(s).

This package can easily be used by framework developers to implement the View layer of an MVC (Model-View-Controller) framework. It can also be easily incorporated into existing frameworks.

100% Unit Test Coverage.

Users of this package are still responsible for making sure that they validate or sanitize data coming into their application(s) via user input (eg. via html forms) with tools like [Respect\\Validation](https://github.com/Respect/Validation), [Valitron](https://github.com/vlucas/valitron), [Upload](https://github.com/brandonsavage/Upload), [Volan](https://github.com/serkin/Volan), [Sirius Validation](https://github.com/siriusphp/validation), [Filterus](https://github.com/ircmaxell/filterus), etc.

### Main Features

[](#main-features)

- No new templating language or syntax to learn, just good old PHP is required to compose view files to be rendered.
    - View file(s) in existing projects can easily be adapted for rendering via this package with very little effort.
- This package can be easily used to implement a Two-Step-View system where all your site's pages share a common layout file and the content for each page gets injected into the layout file.
- Data (variables) can be injected into the files to be rendered via an associative array whose keys will be converted to variables when rendering occurs.
    - Escaping is performed on the values in the data array based on specified escape rules.
- Auto-Escaping is possible on a per instance basis of the **Renderer** class (escaping rules must be specified when instantiating a **Renderer** object for this feature to work).

Acknowledgement
---------------

[](#acknowledgement)

The escaping functionality in this package is implemented using the [laminas-escaper](https://docs.laminas.dev/laminas-escaper/) package.

Changelog
---------

[](#changelog)

[Here](https://github.com/rotexsoft/file-renderer/releases).

Contribution
------------

[](#contribution)

Since the goal of this package is to be lean and flexible, pull requests for significant new features will not be accepted. Users are encouraged to extend the package with new feature(s) in their own projects. However, bug fix and documentation enhancement related pull requests are greatly welcomed.

### Running Tests

[](#running-tests)

` ./vendor/bin/phpunit --coverage-text`

### Branching

[](#branching)

These are the branches in this repository:

- **master:** contains code for the latest major version of this package
- **1.X:** contains code for the **1.x** version of this package
- **2.X:** contains code for the **2.x** version of this package
- **5.x:** contains code for the **5.x** version of this package
- **6.x:** contains code for the **6.x** version of this package
- **7.x:** contains code for the **7.x** version of this package

Requirements
------------

[](#requirements)

- PHP 8.1+
- Not currently certified for use with HHVM (unit tests failing when run in an HHVM environment).

Installation
------------

[](#installation)

`composer require rotexsoft/file-renderer`

Usage
-----

[](#usage)

### Basic Usage

[](#basic-usage)

Your main php script may look like below (let's call it `test.php` and assume it's located at the root directory of your project's folder):

`/some/path/my-project/test.php`

```
