|
2 years ago | |
---|---|---|
.. | ||
config | 2 years ago | |
src | 2 years ago | |
CONTRIBUTING.md | 2 years ago | |
LICENSE.txt | 2 years ago | |
README.md | 2 years ago | |
composer.json | 2 years ago | |
composer.lock | 2 years ago | |
quickstart.md | 2 years ago |
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.
To install via Composer, use the command below, it will automatically detect the latest version and bind it with ~
.
composer require wyrihaximus/twig-view
Add the following to your config/bootstrap.php
to load the plugin.
Plugin::load('WyriHaximus/TwigView', [
'bootstrap' => true,
]);
Instead of extending from the View
let AppView
extend TwigView
:
namespace App\View;
use WyriHaximus\TwigView\View\TwigView;
class AppView extends TwigView
{
}
In Controller/AppController
load your helpers:
public $helpers = ['Html', 'Form']; // And more
Or in your AppView file :
public function initialize()
{
parent::initialize();
$this->loadHelper('Html');
$this->loadHelper('Form');
}
Note: TwigView will look for its templates with the extension .twig
and then for .tpl
(deprecated).
Replace Template/Layout/default.ctp
by this Layout/default.twig
<!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>
Create a template, for exemple Template/Users/index.twig
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>
$this
With twig $this
is replaced by _view
For example, without using Twig writing
<?= $this->fetch('content') ?>
But with Twig
{{ _view.fetch('content')|raw }}
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 }}
Basic
{% element 'Element' %}
With variables or options
{% element 'Plugin.Element' {
dataName: 'dataValue'
} {
optionName: 'optionValue'
} %}
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'
} %}
If i want extend to Common/demo.twig
<div id="sidebar">
{% block sidebar %}{% endblock %}
</div>
<div id="content">
{% block body %}{% endblock %}
</div>
We can write 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') ?>
debug
maps to debug
pr
maps to pr
low
maps to low
up
maps to up
env
maps to env
count
maps to count
pluralize
maps to Cake\Utility\Inflector::pluralize
singularize
maps to Cake\Utility\Inflector::singularize
camelize
maps to Cake\Utility\Inflector::camelize
underscore
maps to Cake\Utility\Inflector::underscore
humanize
maps to Cake\Utility\Inflector::humanize
tableize
maps to Cake\Utility\Inflector::tableize
classify
maps to Cake\Utility\Inflector::classify
variable
maps to Cake\Utility\Inflector::variable
slug
maps to Cake\Utility\Inflector::slug
toReadableSize
maps to Cake\I18n\Number::toReadableSize
fromReadableSize
maps to Cake\I18n\Number::fromReadableSize
toPercentage
maps to Cake\I18n\Number::toPercentage
number_format
maps to Cake\I18n\Number::format
formatDelta
maps to Cake\I18n\Number::formatDelta
currency
maps to Cake\I18n\Number::currency
substr
maps to substr
tokenize
maps to Cake\Utility\Text::tokenize
insert
maps to Cake\Utility\Text::insert
cleanInsert
maps to Cake\Utility\Text::cleanInsert
wrap
maps to Cake\Utility\Text::wrap
wrapBlock
maps to Cake\Utility\Text::wrapBlock
wordWrap
maps to Cake\Utility\Text::wordWrap
highlight
maps to Cake\Utility\Text::highlight
tail
maps to Cake\Utility\Text::tail
truncate
maps to Cake\Utility\Text::truncate
excerpt
maps to Cake\Utility\Text::excerpt
toList
maps to Cake\Utility\Text::toList
stripLinks
maps to Cake\Utility\Text::stripLinks
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
serialize
maps to serialize
unserialize
maps to unserialize
md5
maps to md5
base64_encode
maps to base64_encode
base64_decode
maps to base64_decode
nl2br
maps to nl2br
string
cast to string
in_array
maps to in_array
explode
maps to explode
array
cast to array
array_push
maps to push
array_add
maps to add
array_prev
maps to prev
array_next
maps to next
array_current
maps to current
array_each
maps to each
__
maps to __
__d
maps to __d
__n
maps to __n
__x
maps to __x
__dn
maps to __dn
defaultCurrency
maps to Cake\I18n\Number::defaultCurrency
number_formatter
maps to Cake\I18n\Number::formatter
uuid
maps to Cake\Utility\Text::uuid
time
passed the first and optional second argument into new \Cake\I18n\Time()
timezones
maps to Cake\I18n\Time::listTimezones
elementExists
maps to Cake\View\View::elementExists
,getVars
maps to Cake\View\View::getVars
get
maps to Cake\View\View::get
Visite Twig Documentaion for more tips
This plugin emits several events.
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 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);
}
}
You can use Bake to generate your basic CRUD views using the theme
option.
Let’s say you have a TasksController
for which you want to generate twig templates.
You can use the following command to generate your index, add, edit and view file formatted
using Twig :
bin/cake bake twig_template Tasks all -t WyriHaximus/TwigView
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.