cakephp/twig-view
Composer 安装命令:
composer require cakephp/twig-view
包简介
Twig powered View for CakePHP
README 文档
README
This plugin allows you to use the Twig Templating Language for your views.
It provides wrappers for common View operations and many helpful extensions that expose CakePHP functions and jasny/twig-extensions helpers.
Installation
To install with Composer, use the command below.
composer require cakephp/twig-view
Then, load the Cake/TwigView plugin
in your Application bootstrap just like other Cake plugins.
Configuration
TwigView allows you to configure the Twig Environment through View options. You can set these through ViewBuilder
in the Controller or set them directly in TwigView.
// In controller public function initialize(): void { $this->viewBuilder()->setOption('environment', ['cache' => false]); } // In your AppView public function initialize(): void { $this->setConfig('environment', ['cache' => false]); // Call parent TwigView initialize parent::initialize(); }
Available Options
-
environmentDefaults to empty.
-
markdownWhich markdown engine is used for
markdown_to_htmlfilter. Set todefaultto useDefaultMarkdownor set custom Twig Markdown extensionMarkdownInterfaceinstance.If using
default, require one of: -erusev/parsedown-league/commonmark-michelf/php-markdownDefaults to disabled.
AppView Setup
To start using Twig templates in your application, simply extend TwigView in your AppView.
In general, it is safe to add your application's setup in AppView::initialize().
namespace App\View; use Cake\TwigView\View\TwigView; class AppView extends TwigView { public function initialize(): void { parent::initialize(); // Add application-specific extensions } }
Customization
You can override several parts of TwigView initialization to create a custom Twig setup.
-
File Extensions
You can specify the file extensions used to search for templates by overriding the
$extensionsproperty.class AppView extends TwigView { protected $extensions = [ '.custom', ]; }
-
Twig Loader
You can override the template loader used by Twig.
protected function createLoader(): \Twig\Loader\LoaderInterface { // Return a custom Twig template loader }
-
Twig Extensions
You can override the Twig Extensions loading. If you want to use the built-in
Viewwrappers, make sure you loadCake\TwigView\Twig\Extensions\ViewExtension.protected function initializeExtensions(): void { // Load only specific extensions }
-
Twig Profiler
You can override the Twig profiler used when
DebugKitis loaded.protected function initializeProfiler(): void { parent::initializeProfiler(); // Add custom profiler logging using $this->getProfile() }
Templates
You can create views using Twig templates much like you can with standard CakePHP templates.
Templates are loaded the same way wherever they are used and follow the View path conventions.
{% extends 'Common/base' %}
{{ include('Common/helper') }}
- Template names are always relative to
App.path.templatesnot the current file. - File extensions are automatically generated. Defaults to '.twig'.
- Templates can be loaded from plugins the same as
Viewtemplates.
Layout templates are supported and loaded the same way as View layouts.
templates/layout/default.twig:
<!DOCTYPE html> <html> <head> <title> {{ fetch('title') }} </title> {{ fetch('meta') }} {{ fetch('css') }} {{ fetch('script') }} </head> <body> {{ fetch('content') }} </body> </html>
The layout can be set from the template using the layout tag.
{% layout 'Error' %}
Accessing View
You can access the View instance using the _view global.
TwigView provides wrappers for fetch(), cell() and element() rendering.
Cell and element templates are always loaded from cell/ and element/ sub-directories
the same as View templates.
{{ fetch('content')}}
{{ cell('myCell')}}
{{ element('myElement') }}
TwigView also provides wrappers for any loaded helper using a special naming convention - helper_Name_function().
{{ helper_Text_autoParagraph('some text for a paragarph') }}
All 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 on how to remove the extra white space when needed.
Extension Filters
lowmaps tostrtolowerupmaps tostrtoupperenvmaps toenvpluralizemaps toCake\Utility\Inflector::pluralizesingularizemaps toCake\Utility\Inflector::singularizecamelizemaps toCake\Utility\Inflector::camelizeunderscoremaps toCake\Utility\Inflector::underscorehumanizemaps toCake\Utility\Inflector::humanizetableizemaps toCake\Utility\Inflector::tableizeclassifymaps toCake\Utility\Inflector::classifyvariablemaps toCake\Utility\Inflector::variableslugmaps toCake\Utility\Inflector::slugtoReadableSizemaps toCake\I18n\Number::toReadableSizetoPercentagemaps toCake\I18n\Number::toPercentagecake_number_formatmaps toCake\I18n\Number::formatformatDeltamaps toCake\I18n\Number::formatDeltacurrencymaps toCake\I18n\Number::currencysubstrmaps tosubstrtokenizemaps toCake\Utility\Text::tokenizeinsertmaps toCake\Utility\Text::insertcleanInsertmaps toCake\Utility\Text::cleanInsertwrapmaps toCake\Utility\Text::wrapwrapBlockmaps toCake\Utility\Text::wrapBlockwordWrapmaps toCake\Utility\Text::wordWraphighlightmaps toCake\Utility\Text::highlighttailmaps toCake\Utility\Text::tailtruncatemaps toCake\Utility\Text::truncateexcerptmaps toCake\Utility\Text::excerpttoListmaps toCake\Utility\Text::toListstripLinksmaps toCake\Utility\Text::stripLinksisMultibytemaps toCake\Utility\Text::isMultibyteutf8maps toCake\Utility\Text::utf8asciimaps toCake\Utility\Text::asciiparseFileSizemaps toCake\Utility\Text::parseFileSizeserializemaps toserializeunserializemaps tounserializemd5maps tomd5base64_encodemaps tobase64_encodebase64_decodemaps tobase64_decodestringcast tostring
See jasny/twig-extensions for the filters they provide.
Extension Functions
in_arraymaps toin_arrayexplodemaps toexplodearraycast toarrayarray_pushmaps topusharray_prevmaps toprevarray_nextmaps tonextarray_currentmaps tocurrent__maps to____dmaps to__d__nmaps to__n__xmaps to__x__dnmaps to__dndefaultCurrencymaps toCake\I18n\Number::getDefaultCurrencyuuidmaps toCake\Utility\Text::uuidtimepassed the first and optional second argument intonew \Cake\I18n\DateTime()timezonesmaps toCake\I18n\DateTime::listTimezones()
See jasny/twig-extensions for the functions they provide.
cakephp/twig-view 适用场景与选型建议
cakephp/twig-view 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.63M 次下载、GitHub Stars 达 15, 最近一次更新时间为 2020 年 03 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「twig」 「template」 「view」 「cakephp」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 cakephp/twig-view 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 cakephp/twig-view 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 cakephp/twig-view 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
This Symfony bundle integrates PhpSpreadsheet into Symfony using Twig.
A Twig extension to insert css as inline styles with a tag
Caching and compression for Twig assets (JavaScript and CSS).
Twig extensions for Tracy Debugger
Illuminate View for Morningmedley.
统计信息
- 总下载量: 5.63M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 16
- 点击次数: 9
- 依赖项目数: 10
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-03-08