cakephp / twig-view
Twig powered View for CakePHP
Installs: 4 312 456
Dependents: 10
Suggesters: 0
Security: 0
Stars: 15
Watchers: 17
Forks: 8
Open Issues: 2
Type:cakephp-plugin
pkg:composer/cakephp/twig-view
Requires
- cakephp/cakephp: ^5.0.0
- jasny/twig-extensions: ^1.3
- twig/markdown-extra: ^3.0
- twig/twig: ^3.11.1
Requires (Dev)
- cakephp/cakephp-codesniffer: ^5.0
- cakephp/debug_kit: ^5.0
- michelf/php-markdown: ^1.9
- mikey179/vfsstream: ^1.6.10
- phpunit/phpunit: ^10.5.5 || ^11.1.3
Conflicts
This package is auto-updated.
Last update: 2025-10-23 06:51:36 UTC
README
This plugin allows you to use the Twig Templating Language for your views.
It provides wrappers for common View opertions and many helpful extensions that expose CakePHP functions and jasny/twig-extensions helpers.
Installation
To install with Composer, use the command below.
composer require cakephp/twig-view
Then, load the Cake/TwigView plugin
in your Application bootstrap just like other Cake plugins.
Configuration
TwigView allows you to configure the Twig Environment through View options. You can set these through ViewBuilder
in the Controller or set them directly in TwigView.
// In controller public function initialize(): void { $this->viewBuilder()->setOption('environment', ['cache' => false]); } // In your AppView public function initialize(): void { $this->setConfig('environment', ['cache' => false]); // Call parent TwigView initialize parent::initialize(); }
Available Options
- 
environmentDefaults to empty. 
- 
markdownWhich markdown engine is used for markdown_to_htmlfilter. Set todefaultto useDefaultMarkdownor set custom Twig Markdown extensionMarkdownInterfaceinstance.If using default, require one of: -erusev/parsedown-league/commonmark-michelf/php-markdownDefaults to disabled. 
AppView Setup
To start using Twig templates in your application, simply extend TwigView in your AppView.
In general, it is safe to add your application's setup in AppView::initialize().
namespace App\View; use Cake\TwigView\View\TwigView; class AppView extends TwigView { public function initialize(): void { parent::initialize(); // Add application-specific extensions } }
Customization
You can override several parts of TwigView initialization to create a custom Twig setup.
- 
File Extensions You can specify the file extensions used to search for templates by overriding the $extensionsproperty.class AppView extends TwigView { protected $extensions = [ '.custom', ]; } 
- 
Twig Loader You can override the template loader used by Twig. protected function createLoader(): \Twig\Loader\LoaderInterface { // Return a custom Twig template loader } 
- 
Twig Extensions You can override the Twig Extensions loading. If you want to use the built-in Viewwrappers, make sure you loadCake\TwigView\Twig\Extensions\ViewExtension.protected function initializeExtensions(): void { // Load only specific extensions } 
- 
Twig Profiler You can override the Twig profiler used when DebugKitis loaded.protected function initializeProfiler(): void { parent::initializeProfiler(); // Add custom profiler logging using $this->getProfile() } 
Templates
You can create views using Twig templates much like you can with standard CakePHP templates.
Templates are loaded the same way wherever they are used and follow the View path conventions.
{% extends 'Common/base' %}
{{ include('Common/helper') }}
- Template names are always relative to App.path.templatesnot the current file.
- File extensions are automatically generated. Defaults to '.twig'.
- Templates can be loaded from plugins the same as Viewtemplates.
Layout templates are supported and loaded the same way as View layouts.
templates/layout/default.twig:
<!DOCTYPE html> <html> <head> <title> {{ fetch('title') }} </title> {{ fetch('meta') }} {{ fetch('css') }} {{ fetch('script') }} </head> <body> {{ fetch('content') }} </body> </html>
The layout can be set from the template using the layout tag.
{% layout 'Error' %}
Accessing View
You can access the View instance using the _view global.
TwigView provides wrappers for fetch(), cell() and element() rendering.
Cell and element templates are always loaded from cell/ and element/ sub-directories
the same as View templates.
{{ fetch('content')}}
{{ cell('myCell')}}
{{ element('myElement') }}
TwigView also provides wrappers for any loaded helper using a special naming convention - helper_Name_function().
{{ helper_Text_autoParagraph('some text for a paragarph') }}
All wrapper functions are pre-escaped and do not require using |raw filter. However, keep in mind that Twig keeps the whitespace when using {{ }} to print. Please read the Twig documentation on how to remove the extra white space when needed.
Extension Filters
- lowmaps to- strtolower
- upmaps to- strtoupper
- envmaps to- env
- pluralizemaps to- Cake\Utility\Inflector::pluralize
- singularizemaps to- Cake\Utility\Inflector::singularize
- camelizemaps to- Cake\Utility\Inflector::camelize
- underscoremaps to- Cake\Utility\Inflector::underscore
- humanizemaps to- Cake\Utility\Inflector::humanize
- tableizemaps to- Cake\Utility\Inflector::tableize
- classifymaps to- Cake\Utility\Inflector::classify
- variablemaps to- Cake\Utility\Inflector::variable
- slugmaps to- Cake\Utility\Inflector::slug
- toReadableSizemaps to- Cake\I18n\Number::toReadableSize
- toPercentagemaps to- Cake\I18n\Number::toPercentage
- cake_number_formatmaps to- Cake\I18n\Number::format
- formatDeltamaps to- Cake\I18n\Number::formatDelta
- currencymaps to- Cake\I18n\Number::currency
- substrmaps to- substr
- tokenizemaps to- Cake\Utility\Text::tokenize
- insertmaps to- Cake\Utility\Text::insert
- cleanInsertmaps to- Cake\Utility\Text::cleanInsert
- wrapmaps to- Cake\Utility\Text::wrap
- wrapBlockmaps to- Cake\Utility\Text::wrapBlock
- wordWrapmaps to- Cake\Utility\Text::wordWrap
- highlightmaps to- Cake\Utility\Text::highlight
- tailmaps to- Cake\Utility\Text::tail
- truncatemaps to- Cake\Utility\Text::truncate
- excerptmaps to- Cake\Utility\Text::excerpt
- toListmaps to- Cake\Utility\Text::toList
- stripLinksmaps to- Cake\Utility\Text::stripLinks
- isMultibytemaps to- Cake\Utility\Text::isMultibyte
- utf8maps to- Cake\Utility\Text::utf8
- asciimaps to- Cake\Utility\Text::ascii
- parseFileSizemaps to- Cake\Utility\Text::parseFileSize
- serializemaps to- serialize
- unserializemaps to- unserialize
- md5maps to- md5
- base64_encodemaps to- base64_encode
- base64_decodemaps to- base64_decode
- stringcast to- string
See jasny/twig-extensions for the filters they provide.
Extension Functions
- in_arraymaps to- in_array
- explodemaps to- explode
- arraycast to- array
- array_pushmaps to- push
- array_prevmaps to- prev
- array_nextmaps to- next
- array_currentmaps to- current
- __maps to- __
- __dmaps to- __d
- __nmaps to- __n
- __xmaps to- __x
- __dnmaps to- __dn
- defaultCurrencymaps to- Cake\I18n\Number::getDefaultCurrency
- uuidmaps to- Cake\Utility\Text::uuid
- timepassed the first and optional second argument into- new \Cake\I18n\DateTime()
- timezonesmaps to- Cake\I18n\DateTime::listTimezones()
See jasny/twig-extensions for the functions they provide.