2017 © Pedro Peláez
 

library twig-truncate-extension

A Twig extension to truncate text while preserving HTML tags

image

dzango/twig-truncate-extension

A Twig extension to truncate text while preserving HTML tags

  • Saturday, December 2, 2017
  • by opichon
  • Repository
  • 1 Watchers
  • 10 Stars
  • 55,821 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 8 Forks
  • 3 Open issues
  • 14 Versions
  • 6 % Grown

The README.md

TwigTruncateExtension

A custom twig extension to truncate text while preserving HTML tags., (*1)

Installation

Add the library to your app's composer.json:, (*2)

    "require": {
        "dzango/twig-truncate-extension": "~1.0",
        ...
    }

Add the extension to the Twig_Environment:, (*3)


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., (*4)

{{ "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:, (*5)

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:, (*6)

{{ "some ... very ... long ... markdown text"|markdown|raw|truncate }}

Symfony2

To use this extension in a symfony2 project, you have 2 options:, (*7)

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., (*8)

Credits

  • http://alanwhipple.com/2011/05/25/php-truncate-string-preserving-html-tags-words/ for the truncation logic

The Versions

02/12 2017

dev-master

9999999-dev https://github.com/dzango/TwigTruncateExtension

A Twig extension to truncate text while preserving HTML tags

  Sources   Download

MIT

The Requires

 

The Development Requires

twig truncate

02/12 2017

1.1.0

1.1.0.0 https://github.com/dzango/TwigTruncateExtension

A Twig extension to truncate text while preserving HTML tags

  Sources   Download

MIT

The Requires

 

The Development Requires

twig truncate

19/08 2016

dev-travis

dev-travis https://github.com/dzango/TwigTruncateExtension

A Twig extension to truncate text while preserving HTML tags

  Sources   Download

MIT

The Requires

 

The Development Requires

twig truncate

17/08 2016

1.0.10

1.0.10.0 https://github.com/dzango/TwigTruncateExtension

A Twig extension to truncate text while preserving HTML tags

  Sources   Download

MIT

The Requires

 

The Development Requires

twig truncate

15/08 2016

1.0.9

1.0.9.0 https://github.com/dzango/TwigTruncateExtension

A Twig extension to truncate text while preserving HTML tags

  Sources   Download

MIT

The Requires

 

twig truncate

08/09 2014

1.0.8

1.0.8.0 https://github.com/dzango/TwigTruncateExtension

A Twig extension to truncate text while preserving HTML tags

  Sources   Download

MIT

The Requires

 

twig truncate

08/09 2014

1.0.7

1.0.7.0 https://github.com/dzango/TwigTruncateExtension

A Twig extension to truncate text while preserving HTML tags

  Sources   Download

MIT

The Requires

 

twig truncate

08/09 2014

1.0.6

1.0.6.0 https://github.com/dzango/TwigTruncateExtension

A Twig extension to truncate text while preserving HTML tags

  Sources   Download

MIT

The Requires

 

twig truncate

08/09 2014

1.0.5

1.0.5.0 https://github.com/dzango/TwigTruncateExtension

A Twig extension to truncate text while preserving HTML tags

  Sources   Download

MIT

The Requires

 

twig truncate

08/09 2014

1.0.4

1.0.4.0 https://github.com/dzango/TwigTruncateExtension

A Twig extension to truncate text while preserving HTML tags

  Sources   Download

MIT

The Requires

 

twig truncate

08/09 2014

1.0.3

1.0.3.0 https://github.com/dzango/TwigTruncateExtension

A Twig extension to truncate text while preserving HTML tags

  Sources   Download

MIT

The Requires

 

twig truncate

08/09 2014

1.0.2

1.0.2.0 https://github.com/dzango/TwigTruncateExtension

A Twig extension to truncate text while preserving HTML tags

  Sources   Download

MIT

The Requires

 

twig truncate

08/09 2014

1.0.1

1.0.1.0 https://github.com/dzango/TwigTruncateExtension

A Twig extension to truncate text while preserving HTML tags

  Sources   Download

MIT

The Requires

 

twig truncate

07/09 2014

1.0.0

1.0.0.0 https://github.com/dzango/TwigTruncateExtension

A Twig extension to truncate text while preserving HTML tags

  Sources   Download

MIT

The Requires

 

twig truncate