Get support for cakephp/legacy-twig-view
If you're new to LTH, please see our FAQ for more information on what it is we do.
Support Options
Unfortunately, there are currently no active helpers for this repository on the platform. Until they become available, we reccomend the following actions:
View Open IssuesTake a look to see if anyone else has experienced the same issue as you and if they managed to solve it.
Open an IssueMake sure to read any relevant guidelines for opening issues on this repo before posting a new issue.
Sponsor directlyCheck out the page and see if there are any options to sponsor this project or it's developers directly.
cakephp/legacy-twig-view
TwigView plugin for CakePHP
This plugin 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.
DEPRECATED: Use cakephp/twig-view instead.
Installation
To install via Composer, use the command below.
composer require wyrihaximus/twig-view
Configuration
Load Plugin
Run the following CLI command:
bin/cake plugin load WyriHaximus/TwigView
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
TwigView will look for its templates with the extension .twig.
Layout
Replace templates/layout/default.php by this templates/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>
Template View
Create a template, for example templates/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>
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 can be access by their CamelCase name, for example:
{{ 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.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') ?>
Filters
-
debugmaps todebug -
prmaps topr -
lowmaps tostrtolower -
upmaps tostrtoupper -
envmaps toenv -
countmaps tocount -
pluralizemaps toCake\Utility\Inflector::pluralize -
singularizemaps toCake\Utility\Inflector::singularize -
camelizemaps toCake\Utility\Inflector::camelize -
underscoremaps toCake\Utility\Inflector::underscore -
humanizemaps toCake\Utility\Inflector::humanize -
tableizemaps toCake\Utility\Inflector::tableize -
classifymaps toCake\Utility\Inflector::classify -
variablemaps toCake\Utility\Inflector::variable -
slugmaps toCake\Utility\Inflector::slug -
toReadableSizemaps toCake\I18n\Number::toReadableSize -
toPercentagemaps toCake\I18n\Number::toPercentage -
number_formatmaps toCake\I18n\Number::format -
formatDeltamaps toCake\I18n\Number::formatDelta -
currencymaps toCake\I18n\Number::currency -
substrmaps tosubstr -
tokenizemaps toCake\Utility\Text::tokenize -
insertmaps toCake\Utility\Text::insert -
cleanInsertmaps toCake\Utility\Text::cleanInsert -
wrapmaps toCake\Utility\Text::wrap -
wrapBlockmaps toCake\Utility\Text::wrapBlock -
wordWrapmaps toCake\Utility\Text::wordWrap -
highlightmaps toCake\Utility\Text::highlight -
tailmaps toCake\Utility\Text::tail -
truncatemaps toCake\Utility\Text::truncate -
excerptmaps toCake\Utility\Text::excerpt -
toListmaps toCake\Utility\Text::toList -
stripLinksmaps toCake\Utility\Text::stripLinks -
isMultibytemaps toCake\Utility\Text::isMultibyte -
utf8maps toCake\Utility\Text::utf8 -
asciimaps toCake\Utility\Text::ascii -
parseFileSizemaps toCake\Utility\Text::parseFileSize -
serializemaps toserialize -
unserializemaps tounserialize -
md5maps tomd5 -
base64_encodemaps tobase64_encode -
base64_decodemaps tobase64_decode -
nl2brmaps tonl2br -
stringcast tostring
Functions
-
in_arraymaps toin_array -
explodemaps toexplode -
arraycast toarray -
array_pushmaps topush -
array_prevmaps toprev -
array_nextmaps tonext -
array_currentmaps tocurrent -
__maps to__ -
__dmaps to__d -
__nmaps to__n -
__xmaps to__x -
__dnmaps to__dn -
defaultCurrencymaps toCake\I18n\Number::defaultCurrency -
number_formattermaps toCake\I18n\Number::formatter -
uuidmaps toCake\Utility\Text::uuid -
timepassed the first and optional second argument intonew \Cake\I18n\Time() -
timezonesmaps toCake\I18n\Time::listTimezones -
elementExistsmaps toCake\View\View::elementExists, -
getVarsmaps toCake\View\View::getVars -
getmaps toCake\View\View::get
Twig
Visite Twig Documentaion for more tips
Extra included extensions
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(): array
{
return [
LoaderEvent::EVENT => 'loader',
ConstructEvent::EVENT => 'construct',
];
}
public function loader(LoaderEvent $event): void
{
$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): void
{
$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(): array
{
return [
ConstructEvent::EVENT => 'construct',
];
}
public function construct(ConstructEvent $event): void
{
$event->getTwig()->addExtension(new YourTwigExtension);
}
}
Bake
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
Screenshots
Profiler

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.
Our Mission
We want to make open source more sustainable. The entire platform was born from this and everything we do is in aid of this.
From the Blog
Interesting Articles
-
Generating income from open source
Jun 23 • 8 min read
-
2023 State of OSS
Apr 23 • 45 min read ★
-
A funding experiment...
Aug 19 • 10 min read
-
But You Said I could
Aug 19 • 2 min read
Thank you for checking out LiveTechHelper |
2025 © lth-dev incorporated
p-e622a1a2