Skip to content

mario-bros/TwigView

 
 

Repository files navigation

TwigView plugin for CakePHP

Build Status Latest Stable Version Total Downloads Code Coverage License PHP 7 ready

This plugin for version 3 the CakePHP Framework allows you to use the Twig Templating Language for your views.

In addition to enabling the use of most of Twig's features, the plugin is tightly integrated with the CakePHP view renderer giving you full access to helpers, objects and elements.

Installation

To install via Composer, use the command below, it will automatically detect the latest version and bind it with ~.

composer require wyrihaximus/twig-view 

Bootstrap

Add the following to your config/bootstrap.php to load the plugin.

Plugin::load('WyriHaximus/TwigView', [
    'bootstrap' => true,
]);

Application wide usage

class AppController extends Controller {
    public $viewClass = '\WyriHaximus\TwigView\View\TwigView';
}

Elements

{% element 'Plugin.Element' {
    dataName: 'dataValue'
} {
    optionName: 'optionValue'
} %}

Helpers

Any helper you defined in your controller.

public $helpers = ['Form'];

Can be access by their CamelCase name, for example creating a form using the FormHelper:

{{ Form.create()|raw }}

Cells

Store in context then echo it

{% cell cellObject = 'Plugin.Cell' {
    dataName: 'dataValue'
} {
    optionName: 'optionValue'
} %}

{{ cellObject|raw }}

Fetch and directly echo it

{% cell 'Plugin.Cell' {
    dataName: 'dataValue'
} {
    optionName: 'optionValue'
} %}

Filters

Functions

Events

This plugin emits several events.

Loaders

The default loader can be replace by listening to the WyriHaximus\TwigView\Event\LoaderEvent::EVENT, for example with twital:

<?php

use Cake\Event\EventListenerInterface;
use Goetas\Twital\TwitalLoader;
use WyriHaximus\TwigView\Event\ConstructEvent;
use WyriHaximus\TwigView\Event\LoaderEvent;

class LoaderListener implements EventListenerInterface
{
    public function implementedEvents()
    {
        return [
            LoaderEvent::EVENT => 'loader',
            ConstructEvent::EVENT => 'construct',
        ];
    }

    public function loader(LoaderEvent $event)
    {
        $event->result = new TwitalLoader($event->getLoader());
    }

    /**
     * We've also listening in on this event so we can add the needed extensions to check for to the view
     */
    public function construct(ConstructEvent $event)
    {
        $event->getTwigView()->unshiftExtension('.twital.html');
        $event->getTwigView()->unshiftExtension('.twital.xml');
        $event->getTwigView()->unshiftExtension('.twital.xhtml');
    }
}

Extensions

Extensions can be added to the twig environment by listening to the WyriHaximus\TwigView\Event\ConstructEvent::EVENT, for example:

<?php

use Cake\Event\EventListenerInterface;
use WyriHaximus\TwigView\Event\ConstructEvent;

class LoaderListener implements EventListenerInterface
{
    public function implementedEvents()
    {
        return [
            ConstructEvent::EVENT => 'construct',
        ];
    }

    public function construct(ConstructEvent $event)
    {
        $event->getTwig()->addExtension(new YourTwigExtension);
    }
}

Screenshots

Profiler

Profiler

Templates found

Templates found

License

Copyright 2015 Cees-Jan Kiewiet

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

Twig for CakePHP

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 99.2%
  • Other 0.8%