定制 dzango/twig-truncate-extension 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

dzango/twig-truncate-extension

最新稳定版本:1.1.0

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

统计信息

  • 总下载量: 236.55k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 13
  • 点击次数: 1
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 12
  • Watchers: 1
  • Forks: 7
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固