PHPackages                             jasny/view - 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. [HTTP &amp; Networking](/categories/http)
4. /
5. jasny/view

AbandonedArchivedLibrary[HTTP &amp; Networking](/categories/http)

jasny/view
==========

An abstraction for using PSR-7 with template engines

v1.1.0(8y ago)27.9k↓33.3%1MITPHPPHP &gt;=5.6.0

Since Feb 7Pushed 8y agoCompare

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

READMEChangelog (3)Dependencies (6)Versions (5)Used By (1)

Jasny View
==========

[](#jasny-view)

[![Build Status](https://camo.githubusercontent.com/f9e92f6289e61d31265ff751a1de915224e72e0529e3a542eccaa3367f3cc7e4/68747470733a2f2f7472617669732d63692e6f72672f6a61736e792f766965772e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/jasny/view)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/5a138c40840da2993ee79fe29f0338ba195563edce5a7e9c0db166f35ccf55e8/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6a61736e792f766965772f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/jasny/view/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/c246a84bb059cd52260751454730476a35a1f059b01ec982e88e5b746a5ab9f0/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6a61736e792f766965772f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/jasny/view/?branch=master)[![SensioLabsInsight](https://camo.githubusercontent.com/0b4cf4841e315b232dcd585d5cc703e56ad1b6f8d20f9783d8ca97ff9ed7f596/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f37353562393032632d393964382d343533352d386631652d3536333934343630613561392f6d696e692e706e67)](https://insight.sensiolabs.com/projects/755b902c-99d8-4535-8f1e-56394460a5a9)[![Packagist Stable Version](https://camo.githubusercontent.com/423e7b522cd5df3f3d8f1fc5790c3ad7f1019e454d39c51a81f2c5ba30d8d801/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a61736e792f766965772e737667)](https://packagist.org/packages/jasny/view)[![Packagist License](https://camo.githubusercontent.com/ed8e1c0ff2b9c58ea63a54d62bfac3b4dc2eaa7f353e3278afe6c0fb5ec74016/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6a61736e792f766965772e737667)](https://packagist.org/packages/jasny/view)

An abstraction for using [PSR-7](http://www.php-fig.org/psr/psr-7/) with template engines.

Jasny View isn't bound to any framework and can be used anywhere you want to use an existing template engine like Twig with PSR-7.

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

[](#installation)

Install using composer

```
composer require jasny\view

```

Usage
-----

[](#usage)

All view layers of Jasny View implement the `Jasny\ViewInterface`, which defines the following methods:

### Expose

[](#expose)

Expose a function to the view. This can be a build-in PHP function, a user defined function or even an anonymous function.

```
expose(string $name, callable $function = null);

```

You may omit the second argument. In that case, the name will be used as function name.

```
$view->expose('strlen');
$view->expose('replace', 'str_replace');
$view->expose('add', function($a, $b) { return $a + $b; });
```

### Render

[](#render)

Render and output a template. Outputting is done by writing the result to the response body and setting the response `Content-Type` header.

```
render(ResponseInterface $response, string $name, array $context = []);

```

The `name` is the name of the template and usually corresponds with a filename. The `context` are the values that are made available to the view (as variable or constant).

```
$view->render($response, 'index', ['color' => 'blue', 'answer' => 42]);
```

Twig
----

[](#twig)

`Jasny\View\Twig` is a wrapper around [`Twig_Environment`](http://twig.sensiolabs.org/doc/2.x/api.html). When creating the view object, you can either specify the options to create an environment or pass a Twig environment.

```
$view = new Jasny\View\Twig(['path' => 'views', 'cache' => '/tmp/views']);
```

The `path` option is required. It's passed to the `Twig_Loader_Filesystem` and serves as the base directory where the view files are located.

Other [options](http://twig.sensiolabs.org/doc/2.x/api.html#environment-options) are passed the constructor when creating a `Twig_Environment`. The following options are available:

- debug: When set to true, it automatically set "auto\_reload" to true as well (default to false).
- charset: The charset used by the templates (default to UTF-8).
- basetemplateclass: The base template class to use for generated templates (default to Twig\_Template).
- cache: An absolute path where to store the compiled templates, a TwigCacheInterface implementation, or false to disable compilation cache (default).
- autoreload: Whether to reload the template if the original source changed. If you don't provide the autoreload option, it will be determined automatically based on the debug value.
- strict\_variables: Whether to ignore invalid variables in templates (default to false).
- autoescape: Whether to enable auto-escaping (default to html):
    - false: disable auto-escaping
    - html, js: set the autoescaping to one of the supported strategies
    - name: set the autoescaping strategy based on the template name extension
    - PHP callback: a PHP callback that returns an escaping strategy based on the template "name"
- optimizations: A flag that indicates which optimizations to apply (default to -1 which means that all optimizations are enabled; set it to 0 to disable).

Passing a `Twig_Environment` is recommended if your applicated focusses on Dependency Injection.

### getTwig

[](#gettwig)

The `getTwig()` method returns the `Twig_Environment` object it wraps. It can be used to extends the twig environment.

```
$view = new Jasny\View\Twig(['path' => 'views']);
$view->getTwig()->addExtension(new MyTwigExtension());
$view->getTwig()->addGlobal('foo', 'bar');
```

### addDefaultExtensions

[](#adddefaultextensions)

Calling `$view->addDefaultExtensions()` will add all [Official Twig extensions](https://github.com/twigphp/Twig-extensions)and [Jasny Twig extensions](https://github.com/jasny/twig-extensions) if available.

### expose

[](#expose-1)

For Twig, `expose` optionally takes a third argument. You can specify if the function should be added as [Twig function](http://twig.sensiolabs.org/doc/2.x/advanced.html#functions) or [Twig filter](http://twig.sensiolabs.org/doc/2.x/advanced.html#filters).

```
expose($name, $function = null, $as = 'function')

```

### render

[](#render-1)

Render will automatically add `.html.twig` to the name, if the name doesn't contain an extension. It calls the [`render` method](http://twig.sensiolabs.org/doc/2.x/api.html#rendering-templates) of the Twig environment and write the redered content to the response body.

PHP
---

[](#php)

The PHP layer doesn't use template rendering engine, but simply includes a PHP file.

The constructor takes an array of options, which must contain a `path` property. This is the path to the directory where the view files are located.

Optionally the `ext` option may be passed. This determines the default extension for the view name for `render()`.

```
$view = new Jasny\View\Twig(['path' => 'views', 'ext' => 'phtml']);
```

### getPath

[](#getpath)

Get the directory path.

### getExt

[](#getext)

Get the default extension.

### expose

[](#expose-2)

It's typically not needed to call `expose`. Global PHP functions (build-in or user defined) are already available. Adding a function as alias (so `$name` is not the same as `$template`, is not availabe.

### render

[](#render-2)

The `render()` method will include the specified template file using `include`. In the same context, the `$context` is extracted, so everything is available as variable in the view.

The output is streamed to the response body, using an output buffer callback.

```
$view->render($response, 'index', ['color' => 'blue', 'answer' => 42]);
```

If the specified file name is a directory the index file from that directory is automatically used. If the file doesn't exist, a `RuntimeExpection` is thrown.

Related libraries
-----------------

[](#related-libraries)

- [Jasny HTTP Message](https://github.com/jasny/http-message) - A PSR-7 implementation
- [Jasny Controller](https://github.com/jasny/controller) - A general purpose controller for PSR-7
- [Jasny MVC](https://github.com/jasny/mvc) - Meta package for Jasny Router, Controller and View
- [Twig](http://twig.sensiolabs.org/) - The flexible, fast, and secure template engine for PHP

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity24

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~39 days

Total

4

Last Release

3269d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/3379a93d51305df325df9045e1a8b205d195e4e8c01312dff53a000ee79002eb?d=identicon)[jasny](/maintainers/jasny)

---

Top Contributors

[![jasny](https://avatars.githubusercontent.com/u/100821?v=4)](https://github.com/jasny "jasny (19 commits)")

---

Tags

psr-7mvcviewtemplates

### Embed Badge

![Health badge](/badges/jasny-view/health.svg)

```
[![Health](https://phpackages.com/badges/jasny-view/health.svg)](https://phpackages.com/packages/jasny-view)
```

###  Alternatives

[guzzlehttp/psr7

PSR-7 message implementation that also provides common utility methods

8.0k1.0B3.2k](/packages/guzzlehttp-psr7)[league/uri-interfaces

Common tools for parsing and resolving RFC3987/RFC3986 URI

538204.9M23](/packages/league-uri-interfaces)[aplus/mvc

Aplus Framework MVC Library

2601.6M3](/packages/aplus-mvc)[laminas/laminas-psr7bridge

Bidirectional conversions between PSR-7 and laminas-http messages

117.9M18](/packages/laminas-laminas-psr7bridge)[phpro/http-tools

HTTP tools for developing more consistent HTTP implementations.

28137.8k](/packages/phpro-http-tools)[mezzio/mezzio-authentication-oauth2

OAuth2 (server) authentication middleware for Mezzio and PSR-7 applications.

28483.0k2](/packages/mezzio-mezzio-authentication-oauth2)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
