vivait/twig-safe-date
Composer 安装命令:
composer require vivait/twig-safe-date
包简介
A safe date function in twig that will not set null dates to today.
README 文档
README
Simple filter in twig to help avoid problems with things like: {{ null|date('Y-m-d') }}
There are ways around it using twig - the documentation even states:
If the value passed to the date filter is null, it will return the current date by default. If an empty string is desired instead of the current date, use a ternary operator:
{{ post.published_at is empty ? "" : post.published_at|date("m/d/Y") }}
But often, this is overlooked leading to erroneous information being displayed.
Requirements
- PHP = ^8.3
- Twig ^3.10
Installation & Usage
composer require vivait/twig-safe-date
Once required, register the extension with Twig:
$twig = new \Twig_Environment($loader); $twig->addExtension(new TwigSafeDateExtension);
Once registered, you can use the filter date to output dates as before, with the change that: null values will get turned into known content (default: -) rather than today's date
The default format is 'F j, Y H:i.' (the same as the core date filter in Twig)
{{ post.posted_at|date }}
If you wish to change the format of the date, pass it a parameter with your preferred format:
{{ post.posted_at|date("d/m/Y") }}
If post.posted_at is null, then by default the filter will output -, if you wish to change this to a different value, pass a new default as the third parameter:
{{ post.posted_at|date("d/m/Y", "Europe/London" "Content if null") }}
vivait/twig-safe-date 适用场景与选型建议
vivait/twig-safe-date 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 28.21k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 10 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 vivait/twig-safe-date 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 vivait/twig-safe-date 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 28.21k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-10-27