PHPackages                             cakephp/twig-view - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. cakephp/twig-view

ActiveCakephp-plugin[Framework](/categories/framework)

cakephp/twig-view
=================

Twig powered View for CakePHP

2.1.0(4mo ago)155.1M—0.4%7[2 issues](https://github.com/cakephp/twig-view/issues)8MITPHPPHP &gt;=8.1CI failing

Since Mar 28Pushed 1mo ago17 watchersCompare

[ Source](https://github.com/cakephp/twig-view)[ Packagist](https://packagist.org/packages/cakephp/twig-view)[ RSS](/packages/cakephp-twig-view/feed)WikiDiscussions 2.x Synced 1mo ago

READMEChangelog (10)Dependencies (9)Versions (21)Used By (8)

TwigView plugin for CakePHP
===========================

[](#twigview-plugin-for-cakephp)

[![CI](https://github.com/cakephp/twig-view/actions/workflows/ci.yml/badge.svg)](https://github.com/cakephp/twig-view/actions/workflows/ci.yml)[![Latest Stable Version](https://camo.githubusercontent.com/26820760af0425d0ee0d355a735f88baf1e8942c3b2061557e27127149b4c095/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f63616b657068702f747769672d766965773f736f72743d73656d766572267374796c653d666c61742d737175617265)](https://packagist.org/packages/cakephp/twig-view)[![Total Downloads](https://camo.githubusercontent.com/94b3b38ad2f3e2ff5b2e767a70ce0ae9a27aed8379fb4557083b74add4982150/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f63616b657068702f747769672d766965773f7374796c653d666c61742d737175617265)](https://packagist.org/packages/cakephp/twig-view/stats)[![Code Coverage](https://camo.githubusercontent.com/580f00ecbe9615394af2202d31c62c7d95e3480733109c9e25014deb665599f8/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f63616b657068702f747769672d766965772f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://coveralls.io/r/cakephp/twig-view?branch=master)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)

This plugin allows you to use the [Twig Templating Language](https://twig.symfony.com/doc/) for your views.

It provides wrappers for common View operations and many helpful extensions that expose CakePHP functions and `jasny/twig-extensions` helpers.

Installation
------------

[](#installation)

To install with [Composer](https://getcomposer.org/), use the command below.

```
composer require cakephp/twig-view
```

Then, [load the `Cake/TwigView` plugin](https://book.cakephp.org/4/en/plugins.html#loading-a-plugin)in your `Application` bootstrap just like other Cake plugins.

Configuration
-------------

[](#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

[](#available-options)

- `environment`

    [Twig Environment options](https://twig.symfony.com/doc/3.x/api.html#environment-options).

    Defaults to empty.
- `markdown`

    Which markdown engine is used for `markdown_to_html` filter. Set to `default` to use `DefaultMarkdown` or set custom [Twig Markdown extension](https://packagist.org/packages/twig/markdown-extra) `MarkdownInterface` instance.

    If using `default`, require one of: - `erusev/parsedown`- `league/commonmark`- `michelf/php-markdown`

    Defaults to disabled.

AppView Setup
-------------

[](#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

[](#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 `$extensions` property.

    ```
    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 `View` wrappers, make sure you load `Cake\TwigView\Twig\Extensions\ViewExtension`.

    ```
    protected function initializeExtensions(): void
    {
        // Load only specific extensions
    }
    ```
- Twig Profiler

    You can override the Twig profiler used when `DebugKit` is loaded.

    ```
        protected function initializeProfiler(): void
        {
            parent::initializeProfiler();
            // Add custom profiler logging using $this->getProfile()
        }
    ```

Templates
---------

[](#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.templates` not the current file.
- File extensions are automatically generated. Defaults to '.twig'.
- Templates can be loaded from plugins the same as `View` templates.

Layout templates are supported and loaded the same way as `View` layouts.

`templates/layout/default.twig`:

```

        {{ fetch('title') }}

    {{ fetch('meta') }}
    {{ fetch('css') }}
    {{ fetch('script') }}

    {{ fetch('content') }}

```

The layout can be set from the template using the `layout` tag.

```
{% layout 'Error' %}
```

### Accessing View

[](#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

[](#extension-filters)

- `low` maps to [`strtolower`](https://php.net/strtolower)
- `up` maps to [`strtoupper`](https://php.net/strtoupper)
- `env` maps to [`env`](https://book.cakephp.org/4/en/core-libraries/global-constants-and-functions.html#global-functions)
- `pluralize` maps to [`Cake\Utility\Inflector::pluralize`](https://book.cakephp.org/4/en/core-libraries/inflector.html#Cake%5CUtility%5CInflector::pluralize)
- `singularize` maps to [`Cake\Utility\Inflector::singularize`](https://book.cakephp.org/4/en/core-libraries/inflector.html#Cake%5CUtility%5CInflector::singularize)
- `camelize` maps to [`Cake\Utility\Inflector::camelize`](https://book.cakephp.org/4/en/core-libraries/inflector.html#Cake%5CUtility%5CInflector::camelize)
- `underscore` maps to [`Cake\Utility\Inflector::underscore`](https://book.cakephp.org/4/en/core-libraries/inflector.html#Cake%5CUtility%5CInflector::underscore)
- `humanize` maps to [`Cake\Utility\Inflector::humanize`](https://book.cakephp.org/4/en/core-libraries/inflector.html#Cake%5CUtility%5CInflector::humanize)
- `tableize` maps to [`Cake\Utility\Inflector::tableize`](https://book.cakephp.org/4/en/core-libraries/inflector.html#Cake%5CUtility%5CInflector::tableize)
- `classify` maps to [`Cake\Utility\Inflector::classify`](https://book.cakephp.org/4/en/core-libraries/inflector.html#Cake%5CUtility%5CInflector::classify)
- `variable` maps to [`Cake\Utility\Inflector::variable`](https://book.cakephp.org/4/en/core-libraries/inflector.html#Cake%5CUtility%5CInflector::variable)
- `slug` maps to [`Cake\Utility\Inflector::slug`](https://book.cakephp.org/4/en/core-libraries/inflector.html#Cake%5CUtility%5CInflector::slug)
- `toReadableSize` maps to [`Cake\I18n\Number::toReadableSize`](https://book.cakephp.org/4/en/core-libraries/number.html#Cake%5CI18n%5CNumber::toReadableSize)
- `toPercentage` maps to [`Cake\I18n\Number::toPercentage`](https://book.cakephp.org/4/en/core-libraries/number.html#Cake%5CI18n%5CNumber::toPercentage)
- `cake_number_format` maps to [`Cake\I18n\Number::format`](https://book.cakephp.org/4/en/core-libraries/number.html#Cake%5CI18n%5CNumber::format)
- `formatDelta` maps to [`Cake\I18n\Number::formatDelta`](https://book.cakephp.org/4/en/core-libraries/number.html#Cake%5CI18n%5CNumber::formatDelta)
- `currency` maps to [`Cake\I18n\Number::currency`](https://book.cakephp.org/4/en/core-libraries/number.html#Cake%5CI18n%5CNumber::currency)
- `substr` maps to [`substr`](https://php.net/substr)
- `tokenize` maps to [`Cake\Utility\Text::tokenize`](https://book.cakephp.org/4/en/core-libraries/text.html#simple-string-parsing)
- `insert` maps to [`Cake\Utility\Text::insert`](https://book.cakephp.org/4/en/core-libraries/text.html#formatting-strings)
- `cleanInsert` maps to [`Cake\Utility\Text::cleanInsert`](https://book.cakephp.org/4/en/core-libraries/text.html#formatting-strings)
- `wrap` maps to [`Cake\Utility\Text::wrap`](https://book.cakephp.org/4/en/core-libraries/text.html#wrapping-text)
- `wrapBlock` maps to [`Cake\Utility\Text::wrapBlock`](https://book.cakephp.org/4/en/core-libraries/text.html#wrapping-text)
- `wordWrap` maps to [`Cake\Utility\Text::wordWrap`](https://book.cakephp.org/4/en/core-libraries/text.html#wrapping-text)
- `highlight` maps to [`Cake\Utility\Text::highlight`](https://book.cakephp.org/4/en/core-libraries/text.html#highlighting-substrings)
- `tail` maps to [`Cake\Utility\Text::tail`](https://book.cakephp.org/4/en/core-libraries/text.html#truncating-the-tail-of-a-string)
- `truncate` maps to [`Cake\Utility\Text::truncate`](https://book.cakephp.org/4/en/core-libraries/text.html#truncating-text)
- `excerpt` maps to [`Cake\Utility\Text::excerpt`](https://book.cakephp.org/4/en/core-libraries/text.html#extracting-an-excerpt)
- `toList` maps to [`Cake\Utility\Text::toList`](https://book.cakephp.org/4/en/core-libraries/text.html#converting-an-array-to-sentence-)
- `stripLinks` maps to [`Cake\Utility\Text::stripLinks`](https://book.cakephp.org/4/en/core-libraries/text.html#removing-links)
- `isMultibyte` maps to `Cake\Utility\Text::isMultibyte`
- `utf8` maps to `Cake\Utility\Text::utf8`
- `ascii` maps to `Cake\Utility\Text::ascii`
- `parseFileSize` maps to [`Cake\Utility\Text::parseFileSize`](https://book.cakephp.org/4/en/core-libraries/text.html#simple-string-parsing)
- `serialize` maps to [`serialize`](https://php.net/serialize)
- `unserialize` maps to [`unserialize`](https://php.net/unserialize)
- `md5` maps to [`md5`](https://php.net/md5)
- `base64_encode` maps to [`base64_encode`](https://php.net/base64_encode)
- `base64_decode` maps to [`base64_decode`](https://php.net/base64_decode)
- `string` cast to [`string`](https://php.net/manual/en/language.types.type-juggling.php)

See `jasny/twig-extensions` for the filters they provide.

### Extension Functions

[](#extension-functions)

- `in_array` maps to [`in_array`](https://php.net/in_array)
- `explode` maps to [`explode`](https://php.net/explode)
- `array` cast to [`array`](https://php.net/manual/en/language.types.type-juggling.php)
- `array_push` maps to [`push`](https://php.net/array_push)
- `array_prev` maps to [`prev`](https://php.net/prev)
- `array_next` maps to [`next`](https://php.net/next)
- `array_current` maps to [`current`](https://php.net/current)
- `__` maps to [`__`](https://book.cakephp.org/4/en/core-libraries/internationalization-and-localization.html)
- `__d` maps to [`__d`](https://book.cakephp.org/4/en/core-libraries/internationalization-and-localization.html)
- `__n` maps to [`__n`](https://book.cakephp.org/4/en/core-libraries/internationalization-and-localization.html)
- `__x` maps to [`__x`](https://book.cakephp.org/4/en/core-libraries/internationalization-and-localization.html)
- `__dn` maps to [`__dn`](https://book.cakephp.org/4/en/core-libraries/internationalization-and-localization.html)
- `defaultCurrency` maps to [`Cake\I18n\Number::getDefaultCurrency`](https://book.cakephp.org/4/en/core-libraries/number.html#Cake%5CI18n%5CNumber::getDefaultCurrency)
- `uuid` maps to [`Cake\Utility\Text::uuid`](https://book.cakephp.org/4/en/core-libraries/text.html#generating-uuids)
- `time` passed the first and optional second argument into [`new \Cake\I18n\DateTime()`](https://book.cakephp.org/4/en/core-libraries/time.html#creating-time-instances)
- `timezones` maps to `Cake\I18n\DateTime::listTimezones()`

See `jasny/twig-extensions` for the functions they provide.

###  Health Score

64

—

FairBetter than 99% of packages

Maintenance81

Actively maintained with recent releases

Popularity53

Moderate usage in the ecosystem

Community36

Small or concentrated contributor base

Maturity74

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~121 days

Recently: every ~130 days

Total

19

Last Release

64d ago

Major Versions

1.3.0 → 2.0.02023-09-06

1.3.1 → 2.0.32024-10-11

PHP version history (3 changes)1.0.0-beta1PHP ^7.2

1.1.1PHP &gt;=7.2

2.1.0PHP &gt;=8.1

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/23666?v=4)[CakePHP](/maintainers/cakephp)[@cakephp](https://github.com/cakephp)

---

Top Contributors

[![WyriHaximus](https://avatars.githubusercontent.com/u/147145?v=4)](https://github.com/WyriHaximus "WyriHaximus (525 commits)")[![othercorey](https://avatars.githubusercontent.com/u/24221186?v=4)](https://github.com/othercorey "othercorey (149 commits)")[![ADmad](https://avatars.githubusercontent.com/u/142658?v=4)](https://github.com/ADmad "ADmad (113 commits)")[![inoas](https://avatars.githubusercontent.com/u/20972207?v=4)](https://github.com/inoas "inoas (52 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (44 commits)")[![m3nt0r](https://avatars.githubusercontent.com/u/23321?v=4)](https://github.com/m3nt0r "m3nt0r (43 commits)")[![predominant](https://avatars.githubusercontent.com/u/24568?v=4)](https://github.com/predominant "predominant (33 commits)")[![dependabot-support](https://avatars.githubusercontent.com/u/112581971?v=4)](https://github.com/dependabot-support "dependabot-support (32 commits)")[![dereuromark](https://avatars.githubusercontent.com/u/39854?v=4)](https://github.com/dereuromark "dereuromark (24 commits)")[![markstory](https://avatars.githubusercontent.com/u/24086?v=4)](https://github.com/markstory "markstory (22 commits)")[![LordSimal](https://avatars.githubusercontent.com/u/9105243?v=4)](https://github.com/LordSimal "LordSimal (21 commits)")[![HavokInspiration](https://avatars.githubusercontent.com/u/5243386?v=4)](https://github.com/HavokInspiration "HavokInspiration (16 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (10 commits)")[![vonboth](https://avatars.githubusercontent.com/u/2707967?v=4)](https://github.com/vonboth "vonboth (7 commits)")[![Arhell](https://avatars.githubusercontent.com/u/26163841?v=4)](https://github.com/Arhell "Arhell (4 commits)")[![scrutinizer-auto-fixer](https://avatars.githubusercontent.com/u/6253494?v=4)](https://github.com/scrutinizer-auto-fixer "scrutinizer-auto-fixer (4 commits)")[![jadb](https://avatars.githubusercontent.com/u/33527?v=4)](https://github.com/jadb "jadb (3 commits)")[![julianpollmann](https://avatars.githubusercontent.com/u/2836863?v=4)](https://github.com/julianpollmann "julianpollmann (2 commits)")[![Mechtecs](https://avatars.githubusercontent.com/u/5622125?v=4)](https://github.com/Mechtecs "Mechtecs (1 commits)")[![batopa](https://avatars.githubusercontent.com/u/1306319?v=4)](https://github.com/batopa "batopa (1 commits)")

---

Tags

cakephptwigtwig-template-enginetwigcakephptemplateview

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/cakephp-twig-view/health.svg)

```
[![Health](https://phpackages.com/badges/cakephp-twig-view/health.svg)](https://phpackages.com/packages/cakephp-twig-view)
```

###  Alternatives

[wyrihaximus/twig-view

Twig powered View for CakePHP

804.7M1](/packages/wyrihaximus-twig-view)[slim/twig-view

Slim Framework 4 view helper built on top of the Twig 3 templating component

3708.0M210](/packages/slim-twig-view)[slim/php-view

Render PHP view scripts into a PSR-7 Response object.

2739.7M95](/packages/slim-php-view)[cakephp/bake

Bake plugin for CakePHP

11211.2M158](/packages/cakephp-bake)[htmlburger/wpemerge

A micro framework which modernizes WordPress as a CMS development by providing tools to implement MVC and more.

456137.8k8](/packages/htmlburger-wpemerge)[kanellov/slim-twig-flash

A Twig extension to access Slim Flash messages in templates

22107.6k9](/packages/kanellov-slim-twig-flash)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
