jasny/twig-extensions
Composer 安装命令:
composer require jasny/twig-extensions
包简介
A set of useful Twig filters
README 文档
README
A number of useful filters for Twig.
Installation
Jasny's Twig Extensions can be easily installed using composer
composer require jasny/twig-extensions
Usage
$twig = new Twig_Environment($loader, $options); $twig->addExtension(new Jasny\Twig\DateExtension()); $twig->addExtension(new Jasny\Twig\PcreExtension()); $twig->addExtension(new Jasny\Twig\TextExtension()); $twig->addExtension(new Jasny\Twig\ArrayExtension());
To use in a Symfony project register the extensions as a service.
services: twig.extension.date: class: Jasny\Twig\DateExtension tags: - { name: twig.extension } twig.extension.pcre: class: Jasny\Twig\PcreExtension tags: - { name: twig.extension } twig.extension.text: class: Jasny\Twig\TextExtension tags: - { name: twig.extension } twig.extension.array: class: Jasny\Twig\ArrayExtension tags: - { name: twig.extension }
Date extension
Format a date based on the current locale. Requires the intl extension.
- localdate - Format the date value as a string based on the current locale
- localtime - Format the time value as a string based on the current locale
- localdatetime - Format the date/time value as a string based on the current locale
- age - Get the age (in years) based on a date
- duration - Get the duration string from seconds
Locale::setDefault(LC_ALL, "en_US"); // vs "nl_NL"
{{"now"|localdate('long')}} <!-- July 12, 2013 --> <!-- 12 juli 2013 -->
{{"now"|localtime('short')}} <!-- 5:53 PM --> <!-- 17:53 -->
{{"2013-10-01 23:15:00"|localdatetime}} <!-- 10/01/2013 11:15 PM --> <!-- 01-10-2013 23:15 -->
{{"22-08-1981"|age}} <!-- 35 -->
{{ 3600|duration }} <!-- 1h -->
PCRE
Exposes PCRE to Twig.
- preg_quote - Quote regular expression characters
- preg_match - Perform a regular expression match
- preg_get - Perform a regular expression match and return the matched group
- preg_get_all - Perform a regular expression match and return the group for all matches
- preg_grep - Perform a regular expression match and return an array of entries that match the pattern
- preg_replace - Perform a regular expression search and replace
- preg_filter - Perform a regular expression search and replace, returning only matched subjects.
- preg_split - Split text into an array using a regular expression
{% if client.email|preg_match('/^.+@.+\.\w+$/') %}Email: {{ client.email }}{% endif %}
Website: {{ client.website|preg_replace('~^https?://~')
First name: {{ client.fullname|preg_get('/^\S+/') }}
<ul>
{% for item in items|preg_split('/\s+/')|preg_filter('/-test$/', 'invert') %}
<li>{{ item }}</li>
{% endfor %}
</ul>
Text
Convert text to HTML + string functions
- paragraph - Add HTML paragraph and line breaks to text
- line - Get a single line of text
- less - Cut of text on a page break
- truncate - Cut off text if it's too long
- linkify - Turn all URLs into clickable links (also supports Twitter @user and #subject)
Array
Brings PHP's array functions to Twig
- sum - Calculate the sum of values in an array
- product - Calculate the product of values in an array
- values - Return all the values of an array
- as_array - Cast an object to an associated array
- html_attr - Turn an array into an HTML attribute string
jasny/twig-extensions 适用场景与选型建议
jasny/twig-extensions 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10.9M 次下载、GitHub Stars 达 107, 最近一次更新时间为 2013 年 07 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「templating」 「text」 「time」 「date」 「array」 「regex」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jasny/twig-extensions 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jasny/twig-extensions 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jasny/twig-extensions 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
Texy converts plain text in easy to read Texy syntax into structurally valid (X)HTML. It supports adding of images, links, nested lists, tables and has full support for CSS. Texy supports hyphenation of long words (which reflects language rules), clickable emails and URL (emails are obfuscated again
Tools to convert between .NET and PHP date formats
Adds the EDTF data type to Wikibase
Date component is a set of methods to help with the manipulation of dates.
A Processor for Markup written in PHP. Allows extraction of Markup into a data structure, orchestrated nested manipulation of said structure, and output as (optimized) Markup.
统计信息
- 总下载量: 10.9M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 109
- 点击次数: 33
- 依赖项目数: 10
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2013-07-12