Coder Social home page Coder Social logo

twigview's Introduction

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

Configuration

Load Plugin

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

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

Use View class

Instead of extending from the View let AppView extend TwigView:

namespace App\View;

use WyriHaximus\TwigView\View\TwigView;

class AppView extends TwigView
{
}

Quick Start

Load Helpers

In Controller/AppController load your plugins

public $helpers = ['Html', 'Form']; // And more

Layout

Replace Template/Layout/default.ctp by this Layout/default.tpl

<!DOCTYPE html>
<html>
<head>
    {{ Html.charset()|raw }}

    <title>
        {{ __('myTwigExample') }}
        {{ _view.fetch('title')|raw }}
    </title>

    {{ Html.meta('icon')|raw }}

    {{ Html.css('default.app.css')|raw }}
    {{ Html.script('app')|raw }}

    {{ _view.fetch('meta')|raw }}
    {{ _view.fetch('css')|raw }}
    {{ _view.fetch('script')|raw }}
</head>
<body>
    <header>
        {{ _view.fetch('header')|raw }}
    </header>

    {{ Flash.render()|raw }}

    <section>

        <h1>{{ _view.fetch('title')|raw }}</h1>

        {{ _view.fetch('content')|raw }}
    </section>

    <footer>
        {{ _view.fetch('footer')|raw }}
    </footer>
</body>
</html>

Template View

Create a template, for exemple Template/Users/index.tpl like this

{{ _view.assign('title', __("I'm title")) }}

{{ _view.start('header') }}
    <p>I'm header</p>
{{ _view.end() }}

{{ _view.start('footer') }}
    <p>I'm footer</p>
{{ _view.end() }}

<p>I'm content</p>

Usage

Use $this

With twig $this is replaced by _view

For example, without using Twig writing

<?= $this->fetch('content') ?>

But with Twig

{{ _view.fetch('content')|raw }}

Helpers

Any helper you defined in your controller.

public $helpers = ['Html', 'Form']; // ...

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

{{ Html.link('Edit user', {'controller':'Users', 'action': 'edit' ~ '/' ~ user.id}, {'class':'myclass'})|raw }}

Elements

Basic

{% element 'Element' %}

With variables or options

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

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

Extends

If i want extend to Common/demo.tpl

<div id="sidebar">
    {% block sidebar %}{% endblock %}
</div>

<div id="content">
    {% block body %}{% endblock %}
</div>

We can wrote in a view

{% extends 'Common/demo' %}

{% block sidebar %}
    <ul>
        <li>Item 1</li>
        <li>Item 2</li>
        <li>Item 3</li>
    </ul>
{% endblock %}

{% block body %}

    {{ _view.assign('title', __("I'm title")) }}

    {{ _view.start('header') }}
        <p>I'm header</p>
    {{ _view.end() }}

    {{ _view.start('footer') }}
        <p>I'm footer</p>
    {{ _view.end() }}

    <p>I'm content</p>
{% endblock %}

Note : the block body is required, it's equivalent to <?= $this->fetch('content') ?>

Filters

Functions

Twig

Visite Twig Documentaion for more tips

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.

twigview's People

Contributors

wyrihaximus avatar m3nt0r avatar predominant avatar scrutinizer-auto-fixer avatar bitdeli-chef avatar iknowthis avatar ozee31 avatar lorenzo avatar shama avatar rewish avatar

Watchers

James Cloos avatar Max Schorradt avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.