wyrihaximus / twig-view
为 CakePHP 优化的 Twig 视图
Requires
- php: ^7.2
- cakephp/cakephp: ^4.0
- jasny/twig-extensions: ^1.3
- twig/markdown-extra: ^3.0
- twig/twig: ^3.0
Requires (Dev)
- cakephp/cakephp-codesniffer: ^4.0
- cakephp/debug_kit: ^4.0
- jakub-onderka/php-console-highlighter: ^0.4.0
- jakub-onderka/php-parallel-lint: ^1.0
- phpunit/phpunit: ^8.0
- wyrihaximus/phpunit-class-reflection-helpers: dev-master
This package is auto-updated.
Last update: 2024-09-09 00:29:48 UTC
README
此插件允许您在视图中使用Twig 模板语言。
除了启用使用大多数 Twig 功能外,该插件与 CakePHP 视图渲染器紧密集成,让您完全访问助手、对象和元素。
已弃用:请使用cakephp/twig-view代替。
安装
要通过Composer安装,请使用以下命令。
composer require wyrihaximus/twig-view
配置
加载插件
运行以下 CLI 命令
bin/cake plugin load WyriHaximus/TwigView
使用 View 类
而不是扩展 View,让 AppView 扩展 TwigView
namespace App\View; use WyriHaximus\TwigView\View\TwigView; class AppView extends TwigView { }
快速入门
TwigView 将查找扩展名为 .twig 的模板。
布局
将 templates/layout/default.php 替换为以下 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>
模板视图
创建一个模板,例如 templates/Users/index.twig,如下所示
{{ _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
在 twig 中,$this 被替换为 _view
例如,不使用 Twig 编写
<?= $this->fetch('content') ?>
但是使用 Twig
{{ _view.fetch('content')|raw }}
助手
任何助手都可以通过其 CamelCase 名称访问,例如
{{ Html.link('Edit user', {'controller':'Users', 'action': 'edit' ~ '/' ~ user.id}, {'class':'myclass'})|raw }}
元素
基本
{% element 'Element' %}
带有变量或选项
{% element 'Plugin.Element' {
dataName: 'dataValue'
} {
optionName: 'optionValue'
} %}
单元格
存储在上下文中然后输出它
{% cell cellObject = 'Plugin.Cell' {
dataName: 'dataValue'
} {
optionName: 'optionValue'
} %}
{{ cellObject|raw }}
获取并直接输出它
{% cell 'Plugin.Cell' {
dataName: 'dataValue'
} {
optionName: 'optionValue'
} %}
扩展
如果要扩展到 Common/demo.twig
<div id="sidebar"> {% block sidebar %}{% endblock %} </div> <div id="content"> {% block body %}{% endblock %} </div>
我们可以在视图中编写
{% 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 %}
注意:需要 body 块,它等同于 <?= $this->fetch('content') ?>
过滤器
debug映射到debugpr映射到prlow映射到strtolowerup映射到strtoupperenv映射到envcount映射到countpluralize映射到Cake\Utility\Inflector::pluralizesingularize映射到Cake\Utility\Inflector::singularizecamelize映射到Cake\Utility\Inflector::camelizeunderscore映射到Cake\Utility\Inflector::underscorehumanize映射到Cake\Utility\Inflector::humanizetableize映射到Cake\Utility\Inflector::tableizeclassify对应于Cake\Utility\Inflector::classifyvariable对应于Cake\Utility\Inflector::variableslug对应于Cake\Utility\Inflector::slugtoReadableSize对应于Cake\I18n\Number::toReadableSizetoPercentage对应于Cake\I18n\Number::toPercentagenumber_format对应于Cake\I18n\Number::formatformatDelta对应于Cake\I18n\Number::formatDeltacurrency对应于Cake\I18n\Number::currencysubstr对应于substrtokenize对应于Cake\Utility\Text::tokenizeinsert对应于Cake\Utility\Text::insertcleanInsert对应于Cake\Utility\Text::cleanInsertwrap对应于Cake\Utility\Text::wrapwrapBlock对应于Cake\Utility\Text::wrapBlockwordWrap对应于Cake\Utility\Text::wordWraphighlight对应于Cake\Utility\Text::highlighttail对应于Cake\Utility\Text::tailtruncate对应于Cake\Utility\Text::truncateexcerpt对应于Cake\Utility\Text::excerpttoList对应于Cake\Utility\Text::toListstripLinks对应于Cake\Utility\Text::stripLinksisMultibyte对应于Cake\Utility\Text::isMultibyteutf8对应于Cake\Utility\Text::utf8ascii对应于Cake\Utility\Text::asciiparseFileSize对应于Cake\Utility\Text::parseFileSizeserialize对应于serializeunserialize对应于unserializemd5对应于md5base64_encode对应于base64_encodebase64_decode对应于base64_decodenl2br对应于nl2brstring类型转换为string
功能
in_array对应in_arrayexplode对应explodearray强制转换为arrayarray_push对应pusharray_prev对应prevarray_next对应nextarray_current对应current__对应____d对应__d__n对应__n__x对应__x__dn对应__dndefaultCurrency对应Cake\I18n\Number::defaultCurrencynumber_formatter对应Cake\I18n\Number::formatteruuid对应Cake\Utility\Text::uuidtime将第一个和可选的第二个参数传递给new \Cake\I18n\Time()timezones对应Cake\I18n\Time::listTimezoneselementExists对应Cake\View\View::elementExists,getVars对应Cake\View\View::getVarsget对应Cake\View\View::get
Twig
访问 Twig 文档 获取更多技巧
额外包含的扩展
事件
此插件会发出多个事件。
加载器
默认加载器可以通过监听 WyriHaximus\TwigView\Event\LoaderEvent::EVENT 来替换,例如使用 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'); } }
扩展
可以通过监听 WyriHaximus\TwigView\Event\ConstructEvent::EVENT 将扩展添加到 twig 环境,例如
<?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
您可以使用 Bake 通过 theme 选项生成基本的 CRUD 视图。假设您有一个 TasksController,您希望为它生成 twig 模板。您可以使用以下命令生成索引、添加、编辑和查看文件,使用 Twig 格式化:
bin/cake bake twig_template Tasks all -t WyriHaximus/TwigView
屏幕截图
分析器
找到的模板
许可证
版权所有 2015 Cees-Jan Kiewiet
特此授予任何获得此软件及其相关文档副本(以下简称“软件”)的人免费权利,无限制地使用软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本,并允许向软件提供者提供软件的人这样做,前提是
上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。
本软件按“现状”提供,不提供任何形式的保证,无论是明示的还是隐含的,包括但不限于对适销性、适用于特定目的和非侵权的保证。在任何情况下,作者或版权所有者不应对任何索赔、损害或其他责任负责,无论这些责任是由于合同行为、侵权行为或其他原因,无论这些责任是由于软件本身、使用软件或与软件相关的任何其他行为引起的。

