Download the PHP package dzango/twig-truncate-extension without Composer
On this page you can find all versions of the php package dzango/twig-truncate-extension. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download dzango/twig-truncate-extension
More information about dzango/twig-truncate-extension
Files in dzango/twig-truncate-extension
Package twig-truncate-extension
Short Description A Twig extension to truncate text while preserving HTML tags
License MIT
Homepage https://github.com/dzango/TwigTruncateExtension
Informations about the package twig-truncate-extension
TwigTruncateExtension
A custom twig extension to truncate text while preserving HTML tags.
Installation
Add the library to your app's composer.json
:
Add the extension to the Twig_Environment
:
Usage
The bundle exposes a truncate
twig filter, which can be applied to any string.
Arguments
The truncate
filter accepts 4 arguments, all of which have sensible defaults and can therefore be ignored most of the time:
- 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:
Symfony2
To use this extension in a symfony2 project, you have 2 options:
1. Add a service "manually"
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