felds/twig-extra
Composer 安装命令:
composer require felds/twig-extra
包简介
Custom Twig tags and extensions (switch tag).
README 文档
README
Custom Twig extras for Twig 3.22+ and PHP 8.4+:
switchtag?.nullsafe operator
Installation
composer require felds/twig-extra
Usage
Register the desired extensions with Twig:
$twig->addExtension(new \Felds\TwigExtra\SwitchExtension()); $twig->addExtension(new \Felds\TwigExtra\NullsafeExtension());
In Symfony, you can let autoconfigure pick it up by registering the services:
# services.yaml Felds\TwigExtra\SwitchExtension: ~ Felds\TwigExtra\NullsafeExtension: ~
Switch tag ({% switch %})
Match a value against multiple comparisons, picking the first case that evaluates truthy:
{% switch value %}
{% case 1 %} exactly 1
{% case == 2 %} exactly 2
{% case > 1 %} More than 1
{% default %} Other
{% endswitch %}
- Supports all Twig comparison operators (==, !=, >, <, >=, <=,
is same as,in,not in,matches,starts with,ends with, etc.). - Cases are evaluated top-down; the first hit wins, otherwise
defaultruns when present. - The switch value is stored internally and the original context is restored after the block.
Nullsafe operator (?.)
Access attributes and call methods without throwing when the left side is null or missing:
{{ user?.name }}
{{ post?.author?.getName() }}
{{ date?.format('Y-m-d') ?? 'no date' }}
- If the base value is null or undefined, the chain short-circuits to null.
- Works with arrays, objects, property fetches, and method calls.
- Respects strict variables (nullsafe short-circuits instead of raising).
Tests
composer install ./vendor/bin/phpunit tests
License
MIT
Changelog
See CHANGELOG.md.
felds/twig-extra 适用场景与选型建议
felds/twig-extra 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 01 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 felds/twig-extra 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 felds/twig-extra 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 26
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-22