PHPackages                             gigablah/silex-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. [Templating &amp; Views](/categories/templating)
4. /
5. gigablah/silex-view

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

gigablah/silex-view
===================

Engine-agnostic view component for Silex

0.0.1(12y ago)6862MITPHP

Since Dec 11Pushed 12y ago1 watchersCompare

[ Source](https://github.com/gigablah/silex-view)[ Packagist](https://packagist.org/packages/gigablah/silex-view)[ Docs](https://github.com/gigablah/silex-view)[ RSS](/packages/gigablah-silex-view/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (6)Versions (2)Used By (0)

ViewServiceProvider
===================

[](#viewserviceprovider)

[![Build Status](https://camo.githubusercontent.com/7fa00d7b20311a494124cc4049f26e821d45077722b77454c86e7c7e0030cdfa/68747470733a2f2f7472617669732d63692e6f72672f67696761626c61682f73696c65782d766965772e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/gigablah/silex-view) [![Coverage Status](https://camo.githubusercontent.com/0cd2a570596b539667a8eaf26e303e35566c5bfad2ceedfcd9d0c245b1ae28f8/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f67696761626c61682f73696c65782d766965772f62616467652e706e67)](https://coveralls.io/r/gigablah/silex-view)

The ViewServiceProvider gives engine-agnostic templating capabilities to your [Silex](http://silex.sensiolabs.org) application.

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

[](#installation)

Use [Composer](http://getcomposer.org) to install the gigablah/silex-view library by adding it to your `composer.json`. You'll also need a rendering engine, such as [Mustache](http://mustache.github.io).

```
{
    "require": {
        "silex/silex": "~1.0",
        "mustache/mustache": "~2.4",
        "gigablah/silex-view": "~0.0.1"
    }
}
```

Usage
-----

[](#usage)

Just register the service provider and optionally pass in some defaults.

```
$app->register(new Gigablah\Silex\View\ViewServiceProvider(), array(
    'view.globals' => array('foo' => 'bar'),
    'view.default_engine' => 'mustache'
));
```

The provider registers the `ArrayToViewListener` which intercepts the output from your controllers and wraps it with a `View` object. For it to work, you have to return an array of data from your controller function.

Views
-----

[](#views)

Normally you do not need to instantiate any view entities on your own; the listener will convert your controller output. If you wish to do it manually, the syntax is as follows:

```
$view = $app['view']->create($template = '/path/to/template', $context = array('foo' => 'bar'));
```

Views can be rendered by calling the `render()` function, or casting to string:

```
$output = $view->render();
$output = (string) $view;
```

Again, you should not need to render your views manually since they will be handled by the `Response` object.

View Context
------------

[](#view-context)

The view entity is simply an instance of `ArrayObject`, so you can use regular array notation to set the context, along with convenience functions like `with()`:

```
$view['foo'] = 'bar';
$view->with(array('foo' => 'bar'));
```

To insert into the global context, use `share()`:

```
$view->share(array('foo' => 'bar'));
```

You can initialize the global context by overriding `view.globals`.

Resolving Templates
-------------------

[](#resolving-templates)

How does the listener know which template to use? By default it reads the `_route` attribute from the request entity in lowercase, and appends the extension based on the value of `view.default_engine`. Some examples:

```
$app->get('/foobar', function () {}); // get_foobar.mustache
$app->get('/', function () {}); // get_.mustache
$app->match('/', function () {}); // _.mustache
```

Since you probably want more descriptive template names, you can use named routes:

```
$app->match('/', function () {})->bind('home'); // home.mustache
```

You can also set the `_template` attribute in the request, or as part of the controller output:

```
$app->get('/foo', function (Symfony\Component\HttpFoundation\Request $request) {
    $request->attributes->set('_template', 'foo.html');
});

$app->get('/bar', function () {
    return array('_template' => 'bar.html');
});
```

If you need custom logic for generating template paths, you can create your own class that implements `TemplateResolverInterface` and override `view.template_resolver`.

Engines
-------

[](#engines)

This library does not handle any actual view rendering; that task is delegated to the templating library of your choice. Currently adapters are provided for:

- [Mustache](http://mustache.github.io)
- [Smarty](http://www.smarty.net)
- [Twig](http://twig.sensiolabs.org)
- [Aura.View](http://github.com/auraphp/Aura.View)
- [Plates](http://platesphp.com)
- Raw PHP
- Token replacement using strtr()

There is a special `DelegatingEngine` which acts as a registry for multiple different engines, selecting the appropriate one based on the template file extension. Since Aura.View, Plates and Raw PHP all use the same default file extension (.php), you will need to manually configure the extension mapping as follows:

```
$app->register(new Gigablah\Silex\View\ViewServiceProvider(), array(
    'view.default_engine' => 'php',
    'view.engines' => array(
        'php' => 'view.engine.plates'
    )
));
```

Composite Views
---------------

[](#composite-views)

Views can be nested inside another:

```
$view->nest($app['view']->create('foobar.html'), 'section');
```

For a single view, it is equivalent to:

```
$view['section'] = $app['view']->create('foobar.html');
```

However, the difference lies in nesting multiple views in the same location. Doing this will place the child views adjacent to each other rather than overwriting:

```
$view->nest($app['view']->create('foobar.html'), 'section');
$view->nest($app['view']->create('foobar.html'), 'section'); // foobar.html is now repeated twice
```

What's more, you can mix and match different engines:

```
$mustacheView = $app['view']->create('foo.mustache');
$smartyView = $app['view']->create('bar.tpl')->nest($mustacheView, 'section');
```

Nested views will inherit the context of their parent views.

Exception Handling
------------------

[](#exception-handling)

All rendering exceptions are captured and stored in a shared `ExceptionBag`.

To access the last thrown exception, or return all of them:

```
$exception = $app['view']->getExceptionBag()->pop();
$exceptions = $app['view']->getExceptionBag()->all();
```

More Examples
-------------

[](#more-examples)

You can view a code sample of various usage scenarios in the [demo application](http://github.com/gigablah/silex-view/blob/master/demo/app.php).

License
-------

[](#license)

Released under the MIT license. See the LICENSE file for details.

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity19

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 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

Unknown

Total

1

Last Release

4535d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/2c9db29bd91fb91e04478866b21aafca02baea586244fe8e0bc2181f4e16e925?d=identicon)[Gigablah](/maintainers/Gigablah)

---

Top Contributors

[![gigablah](https://avatars.githubusercontent.com/u/471275?v=4)](https://github.com/gigablah "gigablah (6 commits)")

---

Tags

templateviewsilexrender

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[jenssegers/blade

The standalone version of Laravel's Blade templating engine for use outside of Laravel.

8661.2M109](/packages/jenssegers-blade)[fiskhandlarn/blade

A library for using Laravel Blade templates in WordPress/WordPlate.

365.8k](/packages/fiskhandlarn-blade)

PHPackages © 2026

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