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

Overview

TwigTruncateExtension

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

You might also like...
UMySQL is an extremely simple PHP library for communicating with MySQL databases with ease while keeping overhead to a bare minimum.

Uncomplicated MySQL UMySQL is an extremely simple PHP library for communicating with MySQL databases with ease while keeping overhead to a bare minimu

Glz custom fields - Unlimited Custom Fields for Textpattern

Unlimited custom fields for Textpattern This plugin sits under the Extensions tab in the back-end and gives your custom fields new life. You can final

A PHP component to convert HTML into a plain text format

html2text html2text is a very simple script that uses DOM methods to convert HTML into a format similar to what would be rendered by a browser - perfe

Advanced Custom Fields Contact Form Extension (Beta)

Advanced Custom Fields Contact Form Extension (Beta) This is an extension for Advanced Custom Fields 5 for Wordpress, which enables you to create a co

OctoberCMS BlogHub Plugin - Extends RainLab's Blog extension with custom meta details, additional archives and more.

BlogHub extends the RainLab.Blog OctoberCMS plugin with many necessary and helpful features such as Moderatable Comments, Promotable Tags, Custom Meta Fields, additional Archives, basic Statistics, Views counter and more.

Magento 2 Extension to cleanup admin menu and Store > Configuration area by arranging third party extension items.
Magento 2 Extension to cleanup admin menu and Store Configuration area by arranging third party extension items.

Clean Admin Menu - Magento 2 Extension It will merge all 3rd party extension's menu items in backend's primary menu to a common menu item named "Exten

Magento 2 Blog Extension is a better blog extension for Magento 2 platform. These include all useful features of Wordpress CMS
Magento 2 Blog Extension is a better blog extension for Magento 2 platform. These include all useful features of Wordpress CMS

Magento 2 Blog extension FREE Magento 2 Better Blog by Mageplaza is integrated right into the Magento backend so you can manage your blog and your e-c

html-sanitizer is a library aiming at handling, cleaning and sanitizing HTML sent by external users

html-sanitizer html-sanitizer is a library aiming at handling, cleaning and sanitizing HTML sent by external users (who you cannot trust), allowing yo

Sanitize untrustworthy HTML user input (Symfony integration for https://github.com/tgalopin/html-sanitizer)

html-sanitizer is a library aiming at handling, cleaning and sanitizing HTML sent by external users (who you cannot trust), allowing you to store it and display it safely. It has sensible defaults to provide a great developer experience while still being entierely configurable.

Comments
  • Getting doctype etc.

    Getting doctype etc.

    Given a text like "lorem ipsum dolor sit amet" and calling truncate_letters, I'm getting the following as result:

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
    <html><body>lorem ipsum</body>
    

    I don't want any doctype added, nor additional html and/or body tags

    opened by garak 1
  • Html attribute is truncated

    Html attribute is truncated

    Hello,

    The truncate function breaks html attributes :

    For exemple:

    {{ '<div style="color: red;">Hello world</div>'|truncate(5) }}
    

    display:

    <div style="color:</div>...
    

    style attribute is not closed and it's truncated

    opened by hlecorche 1
Owner
dzango
dzango
Magento 2 custom extension to add custom attributes(longitude, latitude) to customer address

Magento 2 custom extension to add custom attributes(longitude, latitude) to customer address. Then save them to quote model and copy them from quote address to order address on bakend, frontend, rest api

MageArab 2 Jul 14, 2022
JSONFinder - a library that can find json values in a mixed text or html documents, can filter and search the json tree, and converts php objects to json without 'ext-json' extension.

JSONFinder - a library that can find json values in a mixed text or html documents, can filter and search the json tree, and converts php objects to json without 'ext-json' extension.

Eboubaker Eboubaker 2 Jul 31, 2022
This Statamic addon allows you to modify the tags rendered by the Bard fieldtype, giving you full control over the final HTML.

Bard Mutator This Statamic addon allows you to modify the tags rendered by the Bard fieldtype, giving you full control over the final HTML. You can ad

Jack Sleight 10 Sep 26, 2022
TYPO3 CMS extension which extends TYPO3 page cache, by tags based on entities used in fluid templates.

Fluid Page Cache for TYPO3 CMS This TYPO3 CMS extension allows you to clear frontend page caches, automatically when a displayed record has been updat

Armin Vieweg 1 Apr 8, 2022
Laminas\Text is a component to work on text strings

laminas-text This package is considered feature-complete, and is now in security-only maintenance mode, following a decision by the Technical Steering

Laminas Project 38 Dec 31, 2022
Zend\Text is a component to work on text strings from Zend Framework

zend-text Repository abandoned 2019-12-31 This repository has moved to laminas/laminas-text. Zend\Text is a component to work on text strings. It cont

Zend Framework 31 Jan 24, 2021
A Pocketmine Plug-in For Executing Commands While You Click on a item

ItemInteract What This Plugin Do? well This Plugin Is Usefull For Some Servers Like minigames,Skyblock,Lobby etc. This plugin can execute commands whi

null 6 May 31, 2022
Allow players to see how well they are doing while pvping with the help of a combo counter

Combo-Counter Allow players to see how well they are doing while pvping with the help of a combo counter Settngs / Config #set to false if you dont wa

null 3 Jun 1, 2022
This plugin gets player time while playing and leaderboard

TopNolep TopNolep is a plugin that retrieves the game time the player was on the server during, not only that, topnolep now provides a leaderboard tha

Edo 5 Oct 22, 2021