PHPackages                             dzango/twig-truncate-extension - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. dzango/twig-truncate-extension

ActiveLibrary[Templating &amp; Views](/categories/templating)

dzango/twig-truncate-extension
==============================

A Twig extension to truncate text while preserving HTML tags

1.1.0(8y ago)13242.1k↓48.4%7[3 issues](https://github.com/dzango/TwigTruncateExtension/issues)2MITPHPPHP &gt;=5.3.3

Since Sep 7Pushed 8y ago1 watchersCompare

[ Source](https://github.com/dzango/TwigTruncateExtension)[ Packagist](https://packagist.org/packages/dzango/twig-truncate-extension)[ Docs](https://github.com/dzango/TwigTruncateExtension)[ RSS](/packages/dzango-twig-truncate-extension/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (2)Versions (13)Used By (2)

TwigTruncateExtension
=====================

[](#twigtruncateextension)

A custom twig extension to truncate text while preserving HTML tags.

Installation
------------

[](#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
-----

[](#usage)

The bundle exposes a `truncate` twig filter, which can be applied to any string.

```
{{ "some ... very ... large ... text"|truncate }}
```

### Arguments

[](#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
------------------------

[](#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
--------

[](#symfony2)

To use this extension in a symfony2 project, you have 2 options:

### 1. Add a service "manually"

[](#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

[](#2-use-the-twigtruncatebundle)

The [Dzango/TwigTruncateBundle](https://github.com/dzango/TwigTruncateBundle) will register the extension for you as a service.

Credits
-------

[](#credits)

-  for the truncation logic

###  Health Score

39

—

LowBetter than 84% of packages

Maintenance15

Infrequent updates — may be unmaintained

Popularity42

Moderate usage in the ecosystem

Community20

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 69.8% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~107 days

Recently: every ~295 days

Total

12

Last Release

3137d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/61710b3e2bdf8cc378cb14f7e9c3d4fa753738373565270820a7be21c89f6d69?d=identicon)[opichon](/maintainers/opichon)

---

Top Contributors

[![opichon](https://avatars.githubusercontent.com/u/203745?v=4)](https://github.com/opichon "opichon (37 commits)")[![Tsubin](https://avatars.githubusercontent.com/u/14118546?v=4)](https://github.com/Tsubin "Tsubin (13 commits)")[![garak](https://avatars.githubusercontent.com/u/179866?v=4)](https://github.com/garak "garak (1 commits)")[![ogizanagi](https://avatars.githubusercontent.com/u/2211145?v=4)](https://github.com/ogizanagi "ogizanagi (1 commits)")[![tm57](https://avatars.githubusercontent.com/u/7309040?v=4)](https://github.com/tm57 "tm57 (1 commits)")

---

Tags

twigtruncate

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/dzango-twig-truncate-extension/health.svg)

```
[![Health](https://phpackages.com/badges/dzango-twig-truncate-extension/health.svg)](https://phpackages.com/packages/dzango-twig-truncate-extension)
```

###  Alternatives

[symfony/ux-twig-component

Twig components for Symfony

22018.6M356](/packages/symfony-ux-twig-component)[symfony/ux-live-component

Live components for Symfony

1647.0M128](/packages/symfony-ux-live-component)[symfony/ux-toolkit

A tool to easily create a design system in your Symfony app with customizable, well-crafted Twig components

16126.1k1](/packages/symfony-ux-toolkit)[mati365/ckeditor5-symfony

CKEditor 5 integration for Symfony

262.6k](/packages/mati365-ckeditor5-symfony)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
