Skip to content
This is the documentation for pieceofcake2/twig-view, a CakePHP 2.x plugin for pieceofcake2/cakephp. About this fork.

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:

bash
composer require pieceofcake2/twig-view

The 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:

Layoutinstaller-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):

php
CakePlugin::load('TwigView');
// or
CakePlugin::loadAll();

Quick Start ​

Set the view class in src/Controller/AppController.php (legacy layout: app/Controller/AppController.php):

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):

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/ and templates/ directories
  • Pre-compilation: Compile templates for production performance
  • PSR-4 Namespaces: Modern PHP architecture

Further Documentation ​

Examples ​

Example layouts in Twig:

Fork History ​

This project is based on the following repositories:

  1. m3nt0r-legacy/cakephp-twig-view - Original implementation by Kjell Bublitz
  2. predominant/TwigView - Fork by Graham Weldon (archived in 2019) - Direct fork source
  3. cakephp/legacy-twig-view - CakePHP community version (formerly WyriHaximus/TwigView, deprecated)
  4. 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.

See LICENSE for copyright and license information. Not affiliated with the Cake Software Foundation.