PHPackages                             plumphp/plum-twig - 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. plumphp/plum-twig

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

plumphp/plum-twig
=================

PlumTwig is a Twig converter for Plum.

v0.1(11y ago)127MITPHP

Since May 17Pushed 10y ago1 watchersCompare

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

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

 [![Plum](https://camo.githubusercontent.com/a81342cbfd6f64a484988488ad37bbd0e665d0f14f65ec655ae986097447bfb6/687474703a2f2f63646e2e666c6f7269616e2e65632f706c756d2d6c6f676f2e737667)](https://camo.githubusercontent.com/a81342cbfd6f64a484988488ad37bbd0e665d0f14f65ec655ae986097447bfb6/687474703a2f2f63646e2e666c6f7269616e2e65632f706c756d2d6c6f676f2e737667)
==================================================================================================================================================================================================================================================================================================================================================================

[](#----)

> PlumTwig is a [Twig](http://twig.sensiolabs.org) converter for Plum. Plum is a data processing pipeline for PHP.

[![Latest Version](https://camo.githubusercontent.com/fe9acc02881b8a31f002c0b165641fcbadb066510f965590eadac6b531509418/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f706c756d7068702f706c756d2d747769672e737667)](https://packagist.org/packages/plumphp/plum-twig)[![Build Status](https://camo.githubusercontent.com/56202f31e064431dc8280a1243d469a10719a77553be2c6a1b70d96c44f79eb3/68747470733a2f2f7472617669732d63692e6f72672f706c756d7068702f706c756d2d747769672e737667)](https://travis-ci.org/plumphp/plum-twig)[![Windows Build status](https://camo.githubusercontent.com/5bdfdec6b9af34a749201e1be4768ff1eb17be0e9b72966078e29ca4bb9fa662/68747470733a2f2f63692e6170707665796f722e636f6d2f6170692f70726f6a656374732f7374617475732f376436766b6a367164636e6f7970726a3f7376673d74727565)](https://ci.appveyor.com/project/florianeckerstorfer/plum-twig)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/dc45a1555aac1251f8e5d94330c614526c24425db5b225328ef00a2aeff9ee3b/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f706c756d7068702f706c756d2d747769672f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/plumphp/plum-twig/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/52e508615e5ef27113d52d540d53383122819d4ed9dfc0ca4b856eed3753ecf5/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f706c756d7068702f706c756d2d747769672f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/plumphp/plum-twig/?branch=master)[![StyleCI](https://camo.githubusercontent.com/04c2c94848d026171cc2067f78660fa4b61a4b6d1fc6a80d5653180100246695/68747470733a2f2f7374796c6563692e696f2f7265706f732f33323632383236392f736869656c64)](https://styleci.io/repos/32628269)

Developed by [Florian Eckerstorfer](https://florian.ec) in Vienna, Europe.

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

[](#installation)

You can install PlumTwig using [Composer](http://getcomposer.org).

```
$ composer require plumphp/plum-twig
```

Usage
-----

[](#usage)

Please refer to the [Plum documentation](https://github.com/plumphp/plum/blob/master/docs/index.md) for more information about Plum in general.

### `TwigConverter`

[](#twigconverter)

In its simplest form `Plum\PlumTwig\TwigConverter` takes an item and returns a rendered template. The given item is passed to Twigs `render()` method as context. The following code renders the `hello.html.twig` template and passes `["name" => "Florian"]` as context to the template. The return value of `convert()` is the rendered template.

```
use Plum\PlumTwig\TwigConverter;

$converter = new TwigConverter($twig, 'hello');
$converter->convert(['name' => 'Florian']);
```

By default `.html.twig` is appended on the given template name. You can change the file extension by calling `setFileExtension()`:

```
$converter->setFileExtension('.twig');
```

Sometimes different items should be rendered using different templates. If you pass a template property to the constructor, the template name retrieved from the given item. Because `TwigConverter` uses [Vale](https://github.com/cocur/vale) to retrieve the value this works even if the item is a complex and nested structure.

```
$converter = new TwigConverter($twig, 'default', ['template' => 'layout']);

// The template name.html.twig is used to render the item
$converter->convert(['name' => 'Florian', 'layout' => 'name']);
```

We have seen that by default `TwigConverter` takes an arbitrary item (e.g., an array or object) and converts it into a string. In many cases the converter will be part of a bigger Plum workflow and you would like to keep the data in the item. You can pass a target property to the constructor and the rendered template will be stored in the item using Vale.

```
$converter = new TwigConverter($twig, 'layout', ['target' => 'content']);

// The rendered template is added to the item with the key "content"
$converter->convert(['name' => 'Florian']); // -> ['name' => 'Florian', 'content' => '...']
```

Not every time the full item should be passed as context, but rather an element of the item. You can pass the context property to tell `TwigConverter` which field in the item should be used as context.

```
$converter = new TwigConverter($twig, 'layout', ['context' => 'data']);

// Only the ['name' => 'Florian'] is passed as context to Twig
$converter->convert([['data' => ['name' => 'Florian'], 'file' => 'person']);
```

Whether the whole item or just part of it is used as context, Twig only allows arrays to be passed as context. Thus, `TwigConverter` checks if the context is an object and it will call its `toArray()` method (if it has one).

Change Log
----------

[](#change-log)

### Version 0.1 (17 May 2015)

[](#version-01-17-may-2015)

- Initial release

Author
------

[](#author)

Plum and PlumTwig have been developed by [Florian Eckerstorfer](https://florian.ec)([Twitter](https://twitter.com/Florian_)) in Vienna, Europe.

> Plum is a project of [Cocur](http://cocur.co). You can contact us on Twitter: [**@cocurco**](https://twitter.com/cocurco)

License
-------

[](#license)

The MIT license applies to plumphp/plum-twig. For the full copyright and license information, please view the [LICENSE](https://github.com/plumphp/plum-twig/blob/master/LICENSE) file distributed with this source code.

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

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 ~0 days

Total

2

Last Release

4019d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/a80f9fc61cd3a7d7779e8f120b458ca4d18fdd885d719bb77d3379b96bf714d9?d=identicon)[florianeckerstorfer](/maintainers/florianeckerstorfer)

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/plumphp-plum-twig/health.svg)

```
[![Health](https://phpackages.com/badges/plumphp-plum-twig/health.svg)](https://phpackages.com/packages/plumphp-plum-twig)
```

###  Alternatives

[twig/extra-bundle

A Symfony bundle for extra Twig extensions

91492.0M315](/packages/twig-extra-bundle)[twig/intl-extra

A Twig extension for Intl

36763.2M221](/packages/twig-intl-extra)[rcrowe/twigbridge

Adds the power of Twig to Laravel

9105.9M50](/packages/rcrowe-twigbridge)[twig/string-extra

A Twig extension for Symfony String

22046.0M133](/packages/twig-string-extra)[twig/cssinliner-extra

A Twig extension to allow inlining CSS

22918.5M55](/packages/twig-cssinliner-extra)[symfony/ux-twig-component

Twig components for Symfony

21914.8M162](/packages/symfony-ux-twig-component)

PHPackages © 2026

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