{"id":29017878,"url":"https://github.com/cakephp/legacy-twig-view","last_synced_at":"2025-06-25T23:07:26.161Z","repository":{"id":2393142,"uuid":"3359412","full_name":"cakephp/legacy-twig-view","owner":"cakephp","description":"Twig for CakePHP","archived":false,"fork":false,"pushed_at":"2024-01-08T23:02:54.000Z","size":1441,"stargazers_count":81,"open_issues_count":1,"forks_count":29,"subscribers_count":28,"default_branch":"master","last_synced_at":"2024-09-07T02:18:56.428Z","etag":null,"topics":["cakephp","cakephp-plugin","php","twig","twigview"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cakephp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2012-02-05T13:03:49.000Z","updated_at":"2023-09-19T01:51:21.000Z","dependencies_parsed_at":"2022-08-06T12:15:17.126Z","dependency_job_id":"eb4eee29-0737-4057-ba13-0de1b7a45a43","html_url":"https://github.com/cakephp/legacy-twig-view","commit_stats":{"total_commits":701,"total_committers":34,"mean_commits":20.61764705882353,"dds":0.4693295292439372,"last_synced_commit":"2cddea9c680d2ea8339a8a15f570912ed0af51cc"},"previous_names":[],"tags_count":35,"template":false,"template_full_name":null,"purl":"pkg:github/cakephp/legacy-twig-view","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cakephp%2Flegacy-twig-view","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cakephp%2Flegacy-twig-view/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cakephp%2Flegacy-twig-view/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cakephp%2Flegacy-twig-view/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cakephp","download_url":"https://codeload.github.com/cakephp/legacy-twig-view/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cakephp%2Flegacy-twig-view/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261967132,"owners_count":23237663,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cakephp","cakephp-plugin","php","twig","twigview"],"created_at":"2025-06-25T23:07:18.293Z","updated_at":"2025-06-25T23:07:26.147Z","avatar_url":"https://github.com/cakephp.png","language":"PHP","readme":"# TwigView plugin for CakePHP #\n\n![Build Status](https://github.com/cakephp/legacy-twig-view/actions/workflows/ci.yml/badge.svg?branch=master)\n[![Latest Stable Version](https://img.shields.io/github/v/release/cakephp/legacy-twig-view?sort=semver\u0026style=flat-square)](https://packagist.org/packages/wyrihaximus/twig-view)\n[![Total Downloads](https://img.shields.io/packagist/dt/wyrihaximus/twig-view?style=flat-square)](https://packagist.org/packages/wyrihaximus/twig-view/stats)\n[![Code Coverage](https://img.shields.io/coveralls/cakephp/legacy-twig-view/master.svg?style=flat-square)](https://coveralls.io/r/cakephp/legacy-twig-view?branch=master)\n[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)\n\nThis plugin allows you to use the [Twig Templating Language](http://twig.sensiolabs.org) for your views.\n\nIn 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.\n\nDEPRECATED: Use [cakephp/twig-view](https://github.com/cakephp/twig-view) instead.\n\n## Installation ##\n\nTo install via [Composer](https://getcomposer.org/), use the command below.\n\n```bash\ncomposer require wyrihaximus/twig-view\n```\n\n## Configuration ##\n\n### Load Plugin\nRun the following CLI command:\n\n```sh\nbin/cake plugin load WyriHaximus/TwigView\n```\n\n### Use View class\nInstead of extending from the `View` let `AppView` extend `TwigView`:\n\n```php\nnamespace App\\View;\n\nuse WyriHaximus\\TwigView\\View\\TwigView;\n\nclass AppView extends TwigView\n{\n}\n```\n\n## Quick Start\n\nTwigView will look for its templates with the extension `.twig`.\n\n### Layout\nReplace `templates/layout/default.php` by this `templates/layout/default.twig`\n\n``` twig\n\u003c!DOCTYPE html\u003e\n\u003chtml\u003e\n\u003chead\u003e\n    {{ Html.charset()|raw }}\n\n    \u003ctitle\u003e\n        {{ __('myTwigExample') }}\n        {{ _view.fetch('title')|raw }}\n    \u003c/title\u003e\n\n    {{ Html.meta('icon')|raw }}\n\n    {{ Html.css('default.app.css')|raw }}\n    {{ Html.script('app')|raw }}\n\n    {{ _view.fetch('meta')|raw }}\n    {{ _view.fetch('css')|raw }}\n    {{ _view.fetch('script')|raw }}\n\u003c/head\u003e\n\u003cbody\u003e\n    \u003cheader\u003e\n        {{ _view.fetch('header')|raw }}\n    \u003c/header\u003e\n\n    {{ Flash.render()|raw }}\n\n    \u003csection\u003e\n\n        \u003ch1\u003e{{ _view.fetch('title')|raw }}\u003c/h1\u003e\n\n        {{ _view.fetch('content')|raw }}\n    \u003c/section\u003e\n\n    \u003cfooter\u003e\n        {{ _view.fetch('footer')|raw }}\n    \u003c/footer\u003e\n\u003c/body\u003e\n\u003c/html\u003e\n```\n\n### Template View\nCreate a template, for example `templates/Users/index.twig` like this\n```Twig\n{{ _view.assign('title', __(\"I'm title\")) }}\n\n{{ _view.start('header') }}\n    \u003cp\u003eI'm header\u003c/p\u003e\n{{ _view.end() }}\n\n{{ _view.start('footer') }}\n    \u003cp\u003eI'm footer\u003c/p\u003e\n{{ _view.end() }}\n\n\u003cp\u003eI'm content\u003c/p\u003e\n```\n\n## Usage\n\n### Use `$this`\nWith twig `$this` is replaced by `_view`\n\nFor example, without using Twig writing\n```php\n\u003c?= $this-\u003efetch('content') ?\u003e\n```\nBut with Twig\n```twig\n{{ _view.fetch('content')|raw }}\n```\n### Helpers\n\nAny helper can be access by their CamelCase name, for example:\n\n```twig\n{{ Html.link('Edit user', {'controller':'Users', 'action': 'edit' ~ '/' ~ user.id}, {'class':'myclass'})|raw }}\n```\n\n### Elements\nBasic\n```Twig\n{% element 'Element' %}\n```\nWith variables or options\n```Twig\n{% element 'Plugin.Element' {\n    dataName: 'dataValue'\n} {\n    optionName: 'optionValue'\n} %}\n```\n\n### Cells\n\nStore in context then echo it\n```twig\n{% cell cellObject = 'Plugin.Cell' {\n    dataName: 'dataValue'\n} {\n    optionName: 'optionValue'\n} %}\n\n{{ cellObject|raw }}\n```\n\nFetch and directly echo it\n```twig\n{% cell 'Plugin.Cell' {\n    dataName: 'dataValue'\n} {\n    optionName: 'optionValue'\n} %}\n```\n\n### Extends\nIf i want extend to `Common/demo.twig`\n```twig\n\u003cdiv id=\"sidebar\"\u003e\n    {% block sidebar %}{% endblock %}\n\u003c/div\u003e\n\n\u003cdiv id=\"content\"\u003e\n    {% block body %}{% endblock %}\n\u003c/div\u003e\n```\nWe can write in a view\n```twig\n{% extends 'Common/demo' %}\n\n{% block sidebar %}\n    \u003cul\u003e\n        \u003cli\u003eItem 1\u003c/li\u003e\n        \u003cli\u003eItem 2\u003c/li\u003e\n        \u003cli\u003eItem 3\u003c/li\u003e\n    \u003c/ul\u003e\n{% endblock %}\n\n{% block body %}\n\n    {{ _view.assign('title', __(\"I'm title\")) }}\n\n    {{ _view.start('header') }}\n        \u003cp\u003eI'm header\u003c/p\u003e\n    {{ _view.end() }}\n\n    {{ _view.start('footer') }}\n        \u003cp\u003eI'm footer\u003c/p\u003e\n    {{ _view.end() }}\n\n    \u003cp\u003eI'm content\u003c/p\u003e\n{% endblock %}\n```\n\n**Note : the block `body` is required, it's equivalent to `\u003c?= $this-\u003efetch('content') ?\u003e`**\n\n### Filters\n* `debug` maps to [`debug`](https://book.cakephp.org/4/en/development/debugging.html#basic-debugging)\n* `pr` maps to `pr`\n* `low` maps to [`strtolower`](https://php.net/strtolower)\n* `up` maps to [`strtoupper`](https://php.net/strtoupper)\n* `env` maps to [`env`](https://book.cakephp.org/4/en/core-libraries/global-constants-and-functions.html#global-functions)\n* `count` maps to [`count`](https://php.net/count)\n* `pluralize` maps to [`Cake\\Utility\\Inflector::pluralize`](https://book.cakephp.org/4/en/core-libraries/inflector.html#Cake\\\\Utility\\\\Inflector::pluralize)\n* `singularize` maps to [`Cake\\Utility\\Inflector::singularize`](https://book.cakephp.org/4/en/core-libraries/inflector.html#Cake\\\\Utility\\\\Inflector::singularize)\n* `camelize` maps to [`Cake\\Utility\\Inflector::camelize`](https://book.cakephp.org/4/en/core-libraries/inflector.html#Cake\\\\Utility\\\\Inflector::camelize)\n* `underscore` maps to [`Cake\\Utility\\Inflector::underscore`](https://book.cakephp.org/4/en/core-libraries/inflector.html#Cake\\\\Utility\\\\Inflector::underscore)\n* `humanize` maps to [`Cake\\Utility\\Inflector::humanize`](https://book.cakephp.org/4/en/core-libraries/inflector.html#Cake\\\\Utility\\\\Inflector::humanize)\n* `tableize` maps to [`Cake\\Utility\\Inflector::tableize`](https://book.cakephp.org/4/en/core-libraries/inflector.html#Cake\\\\Utility\\\\Inflector::tableize)\n* `classify` maps to [`Cake\\Utility\\Inflector::classify`](https://book.cakephp.org/4/en/core-libraries/inflector.html#Cake\\\\Utility\\\\Inflector::classify)\n* `variable` maps to [`Cake\\Utility\\Inflector::variable`](https://book.cakephp.org/4/en/core-libraries/inflector.html#Cake\\\\Utility\\\\Inflector::variable)\n* `slug` maps to [`Cake\\Utility\\Inflector::slug`](https://book.cakephp.org/4/en/core-libraries/inflector.html#Cake\\\\Utility\\\\Inflector::slug)\n* `toReadableSize` maps to [`Cake\\I18n\\Number::toReadableSize`](https://book.cakephp.org/4/en/core-libraries/number.html#Cake\\\\I18n\\\\Number::toReadableSize)\n* `toPercentage` maps to [`Cake\\I18n\\Number::toPercentage`](https://book.cakephp.org/4/en/core-libraries/number.html#Cake\\\\I18n\\\\Number::toPercentage)\n* `number_format` maps to [`Cake\\I18n\\Number::format`](https://book.cakephp.org/4/en/core-libraries/number.html#Cake\\\\I18n\\\\Number::format)\n* `formatDelta` maps to [`Cake\\I18n\\Number::formatDelta`](https://book.cakephp.org/4/en/core-libraries/number.html#Cake\\\\I18n\\\\Number::formatDelta)\n* `currency` maps to [`Cake\\I18n\\Number::currency`](https://book.cakephp.org/4/en/core-libraries/number.html#Cake\\\\I18n\\\\Number::currency)\n* `substr` maps to [`substr`](https://php.net/substr)\n* `tokenize` maps to [`Cake\\Utility\\Text::tokenize`](https://book.cakephp.org/4/en/core-libraries/text.html#simple-string-parsing)\n* `insert` maps to [`Cake\\Utility\\Text::insert`](https://book.cakephp.org/4/en/core-libraries/text.html#formatting-strings)\n* `cleanInsert` maps to [`Cake\\Utility\\Text::cleanInsert`](https://book.cakephp.org/4/en/core-libraries/text.html#formatting-strings)\n* `wrap` maps to [`Cake\\Utility\\Text::wrap`](https://book.cakephp.org/4/en/core-libraries/text.html#wrapping-text)\n* `wrapBlock` maps to [`Cake\\Utility\\Text::wrapBlock`](https://book.cakephp.org/4/en/core-libraries/text.html#wrapping-text)\n* `wordWrap` maps to [`Cake\\Utility\\Text::wordWrap`](https://book.cakephp.org/4/en/core-libraries/text.html#wrapping-text)\n* `highlight` maps to [`Cake\\Utility\\Text::highlight`](https://book.cakephp.org/4/en/core-libraries/text.html#highlighting-substrings)\n* `tail` maps to [`Cake\\Utility\\Text::tail`](https://book.cakephp.org/4/en/core-libraries/text.html#truncating-the-tail-of-a-string)\n* `truncate` maps to [`Cake\\Utility\\Text::truncate`](https://book.cakephp.org/4/en/core-libraries/text.html#truncating-text)\n* `excerpt` maps to [`Cake\\Utility\\Text::excerpt`](https://book.cakephp.org/4/en/core-libraries/text.html#extracting-an-excerpt)\n* `toList` maps to [`Cake\\Utility\\Text::toList`](https://book.cakephp.org/4/en/core-libraries/text.html#converting-an-array-to-sentence-)\n* `stripLinks` maps to [`Cake\\Utility\\Text::stripLinks`](https://book.cakephp.org/4/en/core-libraries/text.html#removing-links)\n* `isMultibyte` maps to `Cake\\Utility\\Text::isMultibyte`\n* `utf8` maps to `Cake\\Utility\\Text::utf8`\n* `ascii` maps to `Cake\\Utility\\Text::ascii`\n* `parseFileSize` maps to [`Cake\\Utility\\Text::parseFileSize`](https://book.cakephp.org/4/en/core-libraries/text.html#simple-string-parsing)\n* `serialize` maps to [`serialize`](https://php.net/serialize)\n* `unserialize` maps to [`unserialize`](https://php.net/unserialize)\n* `md5` maps to [`md5`](https://php.net/md5)\n* `base64_encode` maps to [`base64_encode`](https://php.net/base64_encode)\n* `base64_decode` maps to [`base64_decode`](https://php.net/base64_decode)\n* `nl2br` maps to [`nl2br`](https://php.net/nl2br)\n* `string` cast to [`string`](https://php.net/manual/en/language.types.type-juggling.php)\n\n### Functions\n* `in_array` maps to [`in_array`](https://php.net/in_array)\n* `explode` maps to [`explode`](https://php.net/explode)\n* `array` cast to [`array`](https://php.net/manual/en/language.types.type-juggling.php)\n* `array_push` maps to [`push`](https://php.net/array_push)\n* `array_prev` maps to [`prev`](https://php.net/prev)\n* `array_next` maps to [`next`](https://php.net/next)\n* `array_current` maps to [`current`](https://php.net/current)\n* `__` maps to [`__`](https://book.cakephp.org/4/en/core-libraries/internationalization-and-localization.html)\n* `__d` maps to [`__d`](https://book.cakephp.org/4/en/core-libraries/internationalization-and-localization.html)\n* `__n` maps to [`__n`](https://book.cakephp.org/4/en/core-libraries/internationalization-and-localization.html)\n* `__x` maps to [`__x`](https://book.cakephp.org/4/en/core-libraries/internationalization-and-localization.html)\n* `__dn` maps to [`__dn`](https://book.cakephp.org/4/en/core-libraries/internationalization-and-localization.html)\n* `defaultCurrency` maps to [`Cake\\I18n\\Number::defaultCurrency`](https://book.cakephp.org/4/en/core-libraries/number.html#Cake\\\\I18n\\\\Number::defaultCurrency)\n* `number_formatter` maps to [`Cake\\I18n\\Number::formatter`](https://book.cakephp.org/4/en/core-libraries/number.html#Cake\\\\I18n\\\\Number::formatter)\n* `uuid` maps to [`Cake\\Utility\\Text::uuid`](https://book.cakephp.org/4/en/core-libraries/text.html#generating-uuids)\n* `time` passed the first and optional second argument into [`new \\Cake\\I18n\\Time()`](https://book.cakephp.org/4/en/core-libraries/time.html#creating-time-instances)\n* `timezones` maps to `Cake\\I18n\\Time::listTimezones`\n* `elementExists` maps to `Cake\\View\\View::elementExists`,\n* `getVars` maps to `Cake\\View\\View::getVars`\n* `get` maps to `Cake\\View\\View::get`\n\n### Twig\nVisite [Twig Documentaion](http://twig.sensiolabs.org/documentation) for more tips\n\n### Extra included extensions\n\n* [jasny/twig-extensions](https://github.com/jasny/twig-extensions)\n* [twig/markdown-extra](https://github.com/twigphp/markdown-extra)\n\n## Events ##\n\nThis plugin emits several events.\n\n### Loaders ###\n\nThe default loader can be replace by listening to the `WyriHaximus\\TwigView\\Event\\LoaderEvent::EVENT`, for example with [twital](https://github.com/goetas/twital):\n\n```php\n\u003c?php\n\nuse Cake\\Event\\EventListenerInterface;\nuse Goetas\\Twital\\TwitalLoader;\nuse WyriHaximus\\TwigView\\Event\\ConstructEvent;\nuse WyriHaximus\\TwigView\\Event\\LoaderEvent;\n\nclass LoaderListener implements EventListenerInterface\n{\n    public function implementedEvents(): array\n    {\n        return [\n            LoaderEvent::EVENT =\u003e 'loader',\n            ConstructEvent::EVENT =\u003e 'construct',\n        ];\n    }\n\n    public function loader(LoaderEvent $event): void\n    {\n        $event-\u003eresult = new TwitalLoader($event-\u003egetLoader());\n    }\n\n    /**\n     * We've also listening in on this event so we can add the needed extensions to check for to the view\n     */\n    public function construct(ConstructEvent $event): void\n    {\n        $event-\u003egetTwigView()-\u003eunshiftExtension('.twital.html');\n        $event-\u003egetTwigView()-\u003eunshiftExtension('.twital.xml');\n        $event-\u003egetTwigView()-\u003eunshiftExtension('.twital.xhtml');\n    }\n}\n\n\n```\n\n### Extensions ###\n\nExtensions can be added to the twig environment by listening to the `WyriHaximus\\TwigView\\Event\\ConstructEvent::EVENT`, for example:\n\n```php\n\u003c?php\n\nuse Cake\\Event\\EventListenerInterface;\nuse WyriHaximus\\TwigView\\Event\\ConstructEvent;\n\nclass LoaderListener implements EventListenerInterface\n{\n    public function implementedEvents(): array\n    {\n        return [\n            ConstructEvent::EVENT =\u003e 'construct',\n        ];\n    }\n\n    public function construct(ConstructEvent $event): void\n    {\n        $event-\u003egetTwig()-\u003eaddExtension(new YourTwigExtension);\n    }\n}\n\n```\n\n## Bake\n\nYou can use Bake to generate your basic CRUD views using the `theme` option.\nLet's say you have a `TasksController` for which you want to generate twig templates.\nYou can use the following command to generate your index, add, edit and view file formatted\nusing Twig :\n\n```bash\nbin/cake bake twig_template Tasks all -t WyriHaximus/TwigView\n```\n\n## Screenshots ##\n\n### Profiler ###\n\n![Profiler](/docs/images/profiler.png)\n\n### Templates found ###\n\n![Templates found](/docs/images/templates-found.png)\n\n## License ##\n\nCopyright 2015 [Cees-Jan Kiewiet](https://wyrihaximus.net/)\n\nPermission is hereby granted, free of charge, to any person\nobtaining a copy of this software and associated documentation\nfiles (the \"Software\"), to deal in the Software without\nrestriction, including without limitation the rights to use,\ncopy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the\nSoftware is furnished to do so, subject to the following\nconditions:\n\nThe above copyright notice and this permission notice shall be\nincluded in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES\nOF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\nNONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT\nHOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,\nWHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING\nFROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR\nOTHER DEALINGS IN THE SOFTWARE.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcakephp%2Flegacy-twig-view","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcakephp%2Flegacy-twig-view","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcakephp%2Flegacy-twig-view/lists"}