PHPackages                             alanrodas/twig-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. alanrodas/twig-view

ActiveCakephp-plugin[Templating &amp; Views](/categories/templating)

alanrodas/twig-view
===================

Twig powered View for CakePHP 2.x

1.1(10y ago)0114MITPHPPHP &gt;=5.2.4

Since Jul 10Pushed 10y ago1 watchersCompare

[ Source](https://github.com/alanrodas/TwigView)[ Packagist](https://packagist.org/packages/alanrodas/twig-view)[ RSS](/packages/alanrodas-twig-view/feed)WikiDiscussions develop Synced 1mo ago

READMEChangelogDependencies (3)Versions (3)Used By (0)

TwigView plugin for CakePHP
===========================

[](#twigview-plugin-for-cakephp)

[![Build Status](https://camo.githubusercontent.com/ade3e62c945f3f585d4b90464e7761596972f642830d0b5af31ca5266720fd16/68747470733a2f2f7472617669732d63692e6f72672f57797269486178696d75732f54776967566965772e706e67)](https://travis-ci.org/WyriHaximus/TwigView)[![Latest Stable Version](https://camo.githubusercontent.com/f4b190540d0dcd90d90d326f04dd12b58c82587186b0b2611051f835faf2b9ac/68747470733a2f2f706f7365722e707567782e6f72672f57797269486178696d75732f547769672d566965772f762f737461626c652e706e67)](https://packagist.org/packages/WyriHaximus/Twig-View)[![Total Downloads](https://camo.githubusercontent.com/2911aa191fce53e6d8c72c9db6c15cb814a6589f9ab1134099dd68da42598843/68747470733a2f2f706f7365722e707567782e6f72672f57797269486178696d75732f547769672d566965772f646f776e6c6f6164732e706e67)](https://packagist.org/packages/WyriHaximus/Twig-View)[![Coverage Status](https://camo.githubusercontent.com/a463f82f431cf527acc97936d2600147370de57ace1650c140ba5907c0d1a1b1/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f57797269486178696d75732f54776967566965772f62616467652e706e67)](https://coveralls.io/r/WyriHaximus/TwigView)[![Bitdeli Badge](https://camo.githubusercontent.com/8b59c2ab405e03bc4188ac27ea9522472307847651be2b78aa296437e65f775f/68747470733a2f2f64327765637a68766c38323376302e636c6f756466726f6e742e6e65742f57797269486178696d75732f74776967766965772f7472656e642e706e67)](https://bitdeli.com/free "Bitdeli Badge")

This plugin for the [CakePHP Framework](http://cakephp.org) allows you to use the [Twig Templating Language](http://twig.sensiolabs.org) for your views.

In addition to enabling the use of most of Twig's features, the plugin is tightly integrated with the CakePHP view renderer giving you full access to helpers, objects and elements.

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

[](#installation)

Make sure you have [composer](http://getcomposer.org/) installed and configured with the autoloader registering during bootstrap as described [here](http://ceeram.github.io/blog/2013/02/22/using-composer-with-cakephp-2-dot-x/). Make sure you have a composer.json and add the following to your required section.

```
"wyrihaximus/twig-view": "dev-master"
```

### Cache Permissions

[](#cache-permissions)

Make the default view-cache folder writeable.

```
APP/Plugin/TwigView/tmp/views

```

Alternatively: Set where you want cache files to be stored.

```
define('TWIG_VIEW_CACHE', APP . 'tmp');
```

Using the View Class
--------------------

[](#using-the-view-class)

To make CakePHP aware of TwigView edit your `APP/Controller/AppController.php` file and add the following:

```
class AppController extends Controller  {
	public $viewClass = 'TwigView.Twig';
}
```

Be sure to load the TwigView plugin in your bootstrap.php file with:

```
CakePlugin::load('TwigView', array('bootstrap' => true));
```

or:

```
CakePlugin::loadAll();
```

Now start creating view files using the `.tpl` extension.

Default Layouts
---------------

[](#default-layouts)

This plugin comes with all default layouts converted to Twig. Examples can be found in:

```
APP/Plugin/TwigView/examples

```

Themes
------

[](#themes)

The plugin has support for themes and works just like the `Theme` view. Simply add the `$theme` property to your controller and you're set.

```
class AppController extends Controller  {
	public $viewClass = 'TwigView.Twig';
	public $theme = 'Rockstar';
}
```

This will cause the view to also look in the `Themed` folder for templates. In the above example templates in the following directory are favored over their non-themed version.

```
APP/View/Themed/Rockstar/

```

If you, for example, want to overwrite the `Layouts/default.tpl` file in the `Rockstar` theme, then create this file:

```
APP/View/Themed/Rockstar/Layouts/default.tpl

```

Using Helpers inside Templates
------------------------------

[](#using-helpers-inside-templates)

All helper objects are available inside a view and can be used like any other variable inside Twig.

```
{{ time.nice(user.created) }}
```

... where ...

```
{{ time.nice(user.created) }}
    ^    ^    ^    ^____key
    |    |    |____array (from $this->set() or loop)
    |    |_____ method
    |______ helper
```

Which is the equivalent of writing:

```
