{"id":13684622,"url":"https://github.com/cakephp/twig-view","last_synced_at":"2026-01-06T00:17:24.855Z","repository":{"id":37405052,"uuid":"245700994","full_name":"cakephp/twig-view","owner":"cakephp","description":"Twig View for CakePHP ","archived":false,"fork":false,"pushed_at":"2025-06-23T03:29:57.000Z","size":1527,"stargazers_count":14,"open_issues_count":2,"forks_count":7,"subscribers_count":18,"default_branch":"2.x","last_synced_at":"2025-06-23T04:24:13.183Z","etag":null,"topics":["cakephp","twig","twig-template-engine"],"latest_commit_sha":null,"homepage":null,"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,"zenodo":null}},"created_at":"2020-03-07T20:30:34.000Z","updated_at":"2025-06-23T03:10:54.000Z","dependencies_parsed_at":"2025-01-05T17:22:53.094Z","dependency_job_id":null,"html_url":"https://github.com/cakephp/twig-view","commit_stats":{"total_commits":812,"total_committers":38,"mean_commits":21.36842105263158,"dds":0.541871921182266,"last_synced_commit":"3d21b2138d182dcb047923a3e3734c3dc3be0a9f"},"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"purl":"pkg:github/cakephp/twig-view","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cakephp%2Ftwig-view","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cakephp%2Ftwig-view/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cakephp%2Ftwig-view/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cakephp%2Ftwig-view/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cakephp","download_url":"https://codeload.github.com/cakephp/twig-view/tar.gz/refs/heads/2.x","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cakephp%2Ftwig-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","twig","twig-template-engine"],"created_at":"2024-08-02T14:00:35.788Z","updated_at":"2026-01-06T00:17:24.834Z","avatar_url":"https://github.com/cakephp.png","language":"PHP","readme":"# TwigView plugin for CakePHP\n\n[![CI](https://github.com/cakephp/twig-view/actions/workflows/ci.yml/badge.svg)](https://github.com/cakephp/twig-view/actions/workflows/ci.yml)\n[![Latest Stable Version](https://img.shields.io/github/v/release/cakephp/twig-view?sort=semver\u0026style=flat-square)](https://packagist.org/packages/cakephp/twig-view)\n[![Total Downloads](https://img.shields.io/packagist/dt/cakephp/twig-view?style=flat-square)](https://packagist.org/packages/cakephp/twig-view/stats)\n[![Code Coverage](https://img.shields.io/coveralls/cakephp/twig-view/master.svg?style=flat-square)](https://coveralls.io/r/cakephp/twig-view?branch=master)\n[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)\n\n\nThis plugin allows you to use the [Twig Templating Language](https://twig.symfony.com/doc/) for your views.\n\nIt provides wrappers for common View operations and many helpful extensions that expose CakePHP functions and `jasny/twig-extensions` helpers.\n\n## Installation\n\nTo install with [Composer](https://getcomposer.org/), use the command below.\n\n```bash\ncomposer require cakephp/twig-view\n```\n\nThen, [load the `Cake/TwigView` plugin](https://book.cakephp.org/4/en/plugins.html#loading-a-plugin)\nin your `Application` bootstrap just like other Cake plugins.\n\n## Configuration\n\n`TwigView` allows you to configure the Twig Environment through `View` options. You can set these through `ViewBuilder`\nin the `Controller` or set them directly in `TwigView`.\n\n```php\n// In controller\npublic function initialize(): void\n{\n    $this-\u003eviewBuilder()-\u003esetOption('environment', ['cache' =\u003e false]);\n}\n\n// In your AppView\npublic function initialize(): void\n{\n    $this-\u003esetConfig('environment', ['cache' =\u003e false]);\n\n    // Call parent TwigView initialize\n    parent::initialize();\n}\n```\n\n### Available Options\n\n- `environment`\n\n    [Twig Environment options](https://twig.symfony.com/doc/3.x/api.html#environment-options).\n\n    Defaults to empty.\n\n- `markdown`\n\n    Which markdown engine is used for `markdown_to_html` filter. Set to `default` to use `DefaultMarkdown` or set\n    custom [Twig Markdown extension](https://packagist.org/packages/twig/markdown-extra) `MarkdownInterface` instance.\n\n    If using `default`, require one of:\n        - `erusev/parsedown`\n        - `league/commonmark`\n        - `michelf/php-markdown`\n\n    Defaults to disabled.\n\n## AppView Setup\n\nTo start using Twig templates in your application, simply extend `TwigView` in your `AppView`.\nIn general, it is safe to add your application's setup in `AppView::initialize()`.\n\n```php\nnamespace App\\View;\n\nuse Cake\\TwigView\\View\\TwigView;\n\nclass AppView extends TwigView\n{\n    public function initialize(): void\n    {\n        parent::initialize();\n\n        // Add application-specific extensions\n    }\n}\n```\n\n### Customization\n\nYou can override several parts of `TwigView` initialization to create a custom Twig setup.\n\n- File Extensions\n\n    You can specify the file extensions used to search for templates by overriding the\n    `$extensions` property.\n\n    ```php\n    class AppView extends TwigView\n    {\n        protected $extensions = [\n            '.custom',\n        ];\n    }\n    ```\n\n- Twig Loader\n\n    You can override the template loader used by Twig.\n\n    ```php\n    protected function createLoader(): \\Twig\\Loader\\LoaderInterface\n    {\n        // Return a custom Twig template loader\n    }\n    ```\n\n- Twig Extensions\n\n    You can override the Twig Extensions loading. If you want to use the built-in\n    `View` wrappers, make sure you load `Cake\\TwigView\\Twig\\Extensions\\ViewExtension`.\n\n    ```php\n    protected function initializeExtensions(): void\n    {\n        // Load only specific extensions\n    }\n\n- Twig Profiler\n\n    You can override the Twig profiler used when `DebugKit` is loaded.\n\n    ```php\n        protected function initializeProfiler(): void\n        {\n            parent::initializeProfiler();\n            // Add custom profiler logging using $this-\u003egetProfile()\n        }\n    ```\n\n## Templates\n\nYou can create views using Twig templates much like you can with standard CakePHP templates.\n\nTemplates are loaded the same way wherever they are used and follow the `View` path conventions.\n\n```twig\n{% extends 'Common/base' %}\n{{ include('Common/helper') }}\n```\n\n- Template names are always relative to `App.path.templates` not the current file.\n- File extensions are automatically generated. Defaults to '.twig'.\n- Templates can be loaded from plugins the same as `View` templates.\n\nLayout templates are supported and loaded the same way as `View` layouts.\n\n`templates/layout/default.twig`:\n\n```twig\n\u003c!DOCTYPE html\u003e\n\u003chtml\u003e\n\u003chead\u003e\n    \u003ctitle\u003e\n        {{ fetch('title') }}\n    \u003c/title\u003e\n\n    {{ fetch('meta') }}\n    {{ fetch('css') }}\n    {{ fetch('script') }}\n\u003c/head\u003e\n\u003cbody\u003e\n    {{ fetch('content') }}\n\u003c/body\u003e\n\u003c/html\u003e\n```\n\nThe layout can be set from the template using the `layout` tag.\n\n```twig\n{% layout 'Error' %}\n```\n\n### Accessing View\n\nYou can access the `View` instance using the `_view` global.\n\n`TwigView` provides wrappers for `fetch()`, `cell()` and `element()` rendering.\nCell and element templates are always loaded from **cell/** and **element/** sub-directories\nthe same as `View` templates.\n\n```twig\n{{ fetch('content')}}\n\n{{ cell('myCell')}}\n{{ element('myElement') }}\n```\n\n`TwigView` also provides wrappers for any loaded helper using a special naming convention - `helper_Name_function()`.\n\n```twig\n{{ helper_Text_autoParagraph('some text for a paragarph') }}\n```\n\nAll wrapper functions are pre-escaped and do not require using `|raw` filter. However, keep in mind that Twig keeps the whitespace when using `{{ }}` to print. Please read the [Twig documentation]((https://twig.symfony.com/doc/3.x/templates.html#whitespace-control)) on how to remove the extra white space when needed.\n\n### Extension Filters\n\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* `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* `cake_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* `string` cast to [`string`](https://php.net/manual/en/language.types.type-juggling.php)\n\nSee `jasny/twig-extensions` for the filters they provide.\n\n### Extension Functions\n\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::getDefaultCurrency`](https://book.cakephp.org/4/en/core-libraries/number.html#Cake\\\\I18n\\\\Number::getDefaultCurrency)\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\\DateTime()`](https://book.cakephp.org/4/en/core-libraries/time.html#creating-time-instances)\n* `timezones` maps to `Cake\\I18n\\DateTime::listTimezones()`\n\nSee `jasny/twig-extensions` for the functions they provide.\n","funding_links":[],"categories":["Templating"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcakephp%2Ftwig-view","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcakephp%2Ftwig-view","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcakephp%2Ftwig-view/lists"}