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
映射到debug
pr
映射到pr
low
映射到strtolower
up
映射到strtoupper
env
映射到env
count
映射到count
pluralize
映射到Cake\Utility\Inflector::pluralize
singularize
映射到Cake\Utility\Inflector::singularize
camelize
映射到Cake\Utility\Inflector::camelize
underscore
映射到Cake\Utility\Inflector::underscore
humanize
映射到Cake\Utility\Inflector::humanize
tableize
映射到Cake\Utility\Inflector::tableize
classify
对应于Cake\Utility\Inflector::classify
variable
对应于Cake\Utility\Inflector::variable
slug
对应于Cake\Utility\Inflector::slug
toReadableSize
对应于Cake\I18n\Number::toReadableSize
toPercentage
对应于Cake\I18n\Number::toPercentage
number_format
对应于Cake\I18n\Number::format
formatDelta
对应于Cake\I18n\Number::formatDelta
currency
对应于Cake\I18n\Number::currency
substr
对应于substr
tokenize
对应于Cake\Utility\Text::tokenize
insert
对应于Cake\Utility\Text::insert
cleanInsert
对应于Cake\Utility\Text::cleanInsert
wrap
对应于Cake\Utility\Text::wrap
wrapBlock
对应于Cake\Utility\Text::wrapBlock
wordWrap
对应于Cake\Utility\Text::wordWrap
highlight
对应于Cake\Utility\Text::highlight
tail
对应于Cake\Utility\Text::tail
truncate
对应于Cake\Utility\Text::truncate
excerpt
对应于Cake\Utility\Text::excerpt
toList
对应于Cake\Utility\Text::toList
stripLinks
对应于Cake\Utility\Text::stripLinks
isMultibyte
对应于Cake\Utility\Text::isMultibyte
utf8
对应于Cake\Utility\Text::utf8
ascii
对应于Cake\Utility\Text::ascii
parseFileSize
对应于Cake\Utility\Text::parseFileSize
serialize
对应于serialize
unserialize
对应于unserialize
md5
对应于md5
base64_encode
对应于base64_encode
base64_decode
对应于base64_decode
nl2br
对应于nl2br
string
类型转换为string
功能
in_array
对应in_array
explode
对应explode
array
强制转换为array
array_push
对应push
array_prev
对应prev
array_next
对应next
array_current
对应current
__
对应__
__d
对应__d
__n
对应__n
__x
对应__x
__dn
对应__dn
defaultCurrency
对应Cake\I18n\Number::defaultCurrency
number_formatter
对应Cake\I18n\Number::formatter
uuid
对应Cake\Utility\Text::uuid
time
将第一个和可选的第二个参数传递给new \Cake\I18n\Time()
timezones
对应Cake\I18n\Time::listTimezones
elementExists
对应Cake\View\View::elementExists
,getVars
对应Cake\View\View::getVars
get
对应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
特此授予任何获得此软件及其相关文档副本(以下简称“软件”)的人免费权利,无限制地使用软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本,并允许向软件提供者提供软件的人这样做,前提是
上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。
本软件按“现状”提供,不提供任何形式的保证,无论是明示的还是隐含的,包括但不限于对适销性、适用于特定目的和非侵权的保证。在任何情况下,作者或版权所有者不应对任何索赔、损害或其他责任负责,无论这些责任是由于合同行为、侵权行为或其他原因,无论这些责任是由于软件本身、使用软件或与软件相关的任何其他行为引起的。