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:

Origin ​

This package is a maintained fork of predominant/TwigView (not published on Packagist, last upstream version v0.6), originally by Kjell Bublitz and Graham Weldon. It is maintained for CakePHP 2 on modern PHP by pieceofcake2. Changes from the original are listed in CHANGELOG.md.

predominant/TwigView itself is based on m3nt0r-legacy/cakephp-twig-view, the original implementation by Kjell Bublitz.

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