Home

Awesome

TwigView plugin for CakePHP

CI Latest Stable Version Total Downloads Code Coverage Software License

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

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.

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') }}

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

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

Extension Functions

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