dzango/twig-truncate-extension
Composer 安装命令:
composer require dzango/twig-truncate-extension
包简介
A Twig extension to truncate text while preserving HTML tags
README 文档
README
A custom twig extension to truncate text while preserving HTML tags.
Installation
Add the library to your app's composer.json:
"require": { "dzango/twig-truncate-extension": "~1.0", ... }
Add the extension to the Twig_Environment:
use Dzango\Twig\Extension\Truncate; $twig = new Twig_Environment(...); $twig->addExtension(new Truncate());
Usage
The bundle exposes a truncate twig filter, which can be applied to any string.
{{ "some ... very ... large ... text"|truncate }}
Arguments
The truncate filter accepts 4 arguments, all of which have sensible defaults and can therefore be ignored most of the time:
truncate($length = 100, $ending = '...', $exact = false, $considerHtml = true)
- length: the maximum number of characters to display, excluding any HTML markup (default
100) - ending: The characters to be appended to the truncated string (default
...) - exact: If set to true, the text may be cut off in the middle of a word. To avoid this, set this argument to false (default
false) - considerHtml: If set to true, HTML markup will be ignored and left unchanged (default
true)
Markdown and raw content
Because the truncate filter preserves HTML markup, it is perfectly able to handle rendered markdown content, as well as "raw" content (as rendered by the raw filter). For example, assuming your app has enabled a markdown twig filter for parsing markdown content, the following is fully supported:
{{ "some ... very ... long ... markdown text"|markdown|raw|truncate }}
Symfony2
To use this extension in a symfony2 project, you have 2 options:
1. Add a service "manually"
# app/config/config.yml services: dzango.twig.truncate_extension: class: Dzango\Twig\Extension\Truncate tags: - { name: twig.extension }
2. Use the TwigTruncateBundle
The Dzango/TwigTruncateBundle will register the extension for you as a service.
Credits
- http://alanwhipple.com/2011/05/25/php-truncate-string-preserving-html-tags-words/ for the truncation logic
dzango/twig-truncate-extension 适用场景与选型建议
dzango/twig-truncate-extension 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 242.02k 次下载、GitHub Stars 达 12, 最近一次更新时间为 2014 年 09 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「twig」 「truncate」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dzango/twig-truncate-extension 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dzango/twig-truncate-extension 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dzango/twig-truncate-extension 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
This Symfony bundle integrates PhpSpreadsheet into Symfony using Twig.
A Twig extension to insert css as inline styles with a tag
Handle truncate action on HTML strings
Caching and compression for Twig assets (JavaScript and CSS).
Twig extensions for Tracy Debugger
统计信息
- 总下载量: 242.02k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 13
- 点击次数: 19
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-09-07