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
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.
