PHPackages                             devanych/view-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. devanych/view-renderer

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

devanych/view-renderer
======================

Simple PHP View Renderer

1.0.1(5y ago)153.2k↑16.7%31MITPHPPHP ^7.4|^8.0

Since Oct 5Pushed 3y ago1 watchersCompare

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

READMEChangelog (2)Dependencies (3)Versions (3)Used By (1)

Simple View Renderer
====================

[](#simple-view-renderer)

[![License](https://camo.githubusercontent.com/2421539d55ec8cce258f42ae061ad8d440534bfb1c8c5c9116d04c1cd5279639/68747470733a2f2f706f7365722e707567782e6f72672f646576616e7963682f766965772d72656e64657265722f6c6963656e7365)](https://packagist.org/packages/devanych/view-renderer)[![Latest Stable Version](https://camo.githubusercontent.com/cc51f7648034daa092cef2eb95610ddacde955525c1620607ea8bf7cdbd49593/68747470733a2f2f706f7365722e707567782e6f72672f646576616e7963682f766965772d72656e64657265722f76)](https://packagist.org/packages/devanych/view-renderer)[![Total Downloads](https://camo.githubusercontent.com/e788c78ddccf575d38ad07c82abf1782d629c9da37469b268036c7333db12f23/68747470733a2f2f706f7365722e707567782e6f72672f646576616e7963682f766965772d72656e64657265722f646f776e6c6f616473)](https://packagist.org/packages/devanych/view-renderer)[![GitHub Build Status](https://github.com/devanych/view-renderer/workflows/build/badge.svg)](https://github.com/devanych/view-renderer/actions)[![GitHub Static Analysis Status](https://github.com/devanych/view-renderer/workflows/static/badge.svg)](https://github.com/devanych/view-renderer/actions)[![Scrutinizer Code Coverage](https://camo.githubusercontent.com/557af24f8e033a3367c0b1e388e06d3ee3d9c615f7f4e96c0517275aa5870535/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f646576616e7963682f766965772d72656e64657265722f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/devanych/view-renderer/?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/edf115fd1306385e314cd9151d14a693d98a3a7ef435d71f95ced370968144b2/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f646576616e7963682f766965772d72656e64657265722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/devanych/view-renderer/?branch=master)

A small and easy-to-use PHP library designed for rendering native PHP views.

Highlights:

- Convenient flat block system.
- Setting global parameters for all views.
- Support for using existing PHP functions.
- Easy extensibility by adding custom functions.
- Flexible layout functionality with unlimited nesting.

A guide with a detailed description in Russian language is [available here](https://devanych.ru/development/rendering-nativnyh-php-predstavlenij).

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

[](#installation)

This package requires PHP version 7.4 or later.

```
composer require devanych/view-renderer

```

Usage
-----

[](#usage)

The simplest example of rendering:

```
use Devanych\View\Renderer;

$renderer = new Renderer('/path/to/root/directory/of/views');

$content = $renderer->render('path/to/view/file', [
    'variableName' => 'the value of a variable of any type',
]);

echo $content;
```

The `render()` method takes as its first argument the path to the view file, which must be relative to the directory passed to the constructor when the object was created. The second argument passes an array of parameters (`name => value`) that will be converted to variables inside the view.

```
$renderer->render('post/show', [
    'post' => $posts->get($id),
]);
```

in the view:

```

```

The view file can be with or without an extension, if the file extension is not specified, `.php` will be substituted by default. The default extension can be changed by passing it to the constructor as the second argument.

```
$renderer = new Renderer('/path/to/root/directory/of/views', 'tpl');
```

Within layouts and views, a renderer instance is available at `$this`.

```
