TwigView Documentation
pieceofcake2/twig-view integrates the Twig Templating Language (Twig 3.x) with CakePHP 2, providing access to helpers, elements and themes from Twig templates.
NOTE
For CakePHP 5.x, use the official cakephp/twig-view package instead.
Installation
Install the plugin with Composer:
composer require pieceofcake2/twig-viewThe plugin has the cakephp-plugin type, so it is installed into your plugins directory by composer/installers. Set installer-paths in your application's composer.json so it is installed into the right place:
| Layout | installer-paths |
|---|---|
Modern (config/, src/, templates/, plugins/) | "plugins/{$name}/": ["type:cakephp-plugin"] |
Legacy (app/Config/, app/Plugin/) | "app/Plugin/{$name}/": ["type:cakephp-plugin"] |
Load the plugin in config/bootstrap.php (legacy layout: app/Config/bootstrap.php):
CakePlugin::load('TwigView');
// or
CakePlugin::loadAll();Quick Start
Set the view class in src/Controller/AppController.php (legacy layout: app/Controller/AppController.php):
class AppController extends Controller
{
public $viewClass = 'TwigView.Twig';
}Create templates with the .twig extension, e.g. templates/Posts/index.twig (legacy layout: app/View/Posts/index.twig):
{% for post in posts %}
<h2>{{ post.Post.title }}</h2>
<p>{{ post.Post.body }}</p>
{% endfor %}Features
- CakePHP Integration: Full access to helpers, elements, and themes
- Custom Extensions: Extend Twig with custom filters and functions
- Theme Support: Compatible with CakePHP's themed views
- Plugin Support: Works with both
View/andtemplates/directories - Pre-compilation: Compile templates for production performance
- PSR-4 Namespaces: Modern PHP architecture
Further Documentation
- Function & Filter Reference - Complete list of available Twig functions and filters
- Using Helpers - How to use CakePHP helpers in Twig templates
- Configuration - Advanced configuration options
- Extensions - Popular Twig extensions
Examples
Example layouts in Twig:
Layouts/default.twigLayouts/ajax.twigLayouts/flash.twigLayouts/js/default.twigLayouts/rss/default.twigLayouts/xml/default.twigLayouts/email/html/default.twigLayouts/email/text/default.twig
Fork History
This project is based on the following repositories:
- m3nt0r-legacy/cakephp-twig-view - Original implementation by Kjell Bublitz
- predominant/TwigView - Fork by Graham Weldon (archived in 2019) - Direct fork source
- cakephp/legacy-twig-view - CakePHP community version (formerly WyriHaximus/TwigView, deprecated)
- pieceofcake2/twig-view (this repository) - Modernized fork with Twig 3.x and PHP 8.1+ support
The original repositories supported Twig 1.x with CakePHP 2.x but are no longer maintained. This fork updates the codebase to work with modern PHP and Twig versions while maintaining CakePHP 2.x compatibility.
