Installation:
composer require dzango/twig-truncate-bundle
For Symfony 4+, the bundle auto-enables. For older versions, manually register it in config/bundles.php or AppKernel.php.
First Use Case: Truncate a string while preserving HTML tags in a Twig template:
{{ 'This is a <b>long</b> text to truncate...'|truncate(20, '...') }}
Output: This is a <b>long</b>...
Where to Look First:
config/packages/twig.yaml to ensure Twig extensions are loaded.Basic Truncation:
{{ article.content|truncate(100) }} {# Truncate to 100 chars #}
<p>, <a>) within the truncated text.Custom Separators:
{{ text|truncate(50, ' [read more]') }}
... with a custom suffix (e.g., for "Read More" links).Absolute vs. Relative Truncation:
{{ text|truncate(50, '', true) }} {# Absolute truncation (no partial words) #}
Integration with Forms/Entities:
<div class="preview">{{ post.excerpt|truncate(80) }}</div>
Dynamic Truncation Length:
return $this->render('template.html.twig', [
'truncateLength' => $user->preferredLength ?? 100,
]);
{{ text|truncate(truncateLength) }}
Combining with Other Filters:
{{ text|striptags|truncate(30)|upper }}
Conditional Truncation:
{% if showFullText %}
{{ text }}
{% else %}
{{ text|truncate(150) }}
{% endif %}
Reusable Macros: Define a macro in Twig for consistent truncation:
{% macro truncateText(text, length=100) %}
{{ text|truncate(length, '...') }}
{% endmacro %}
Usage:
{{ _self.truncateText(article.body, 200) }}
Bundle Auto-Loading:
config/bundles.php:
Dzango\Bundle\TwigTruncateBundle\TwigTruncateBundle::class => ['all' => true],
HTML Tag Preservation Limits:
<a href="... mid-truncate). Test edge cases like:
{{ '<a href="very-long-url-here">Click</a>'|truncate(10) }}
Output: <a href="very-lo... (may render invalid HTML).Caching Issues:
php bin/console cache:clear
Deprecated Symfony Versions:
Verify Extension Registration:
php bin/console debug:container dzango_twig_truncate
TwigTruncateExtension class.Test Edge Cases:
{{ ''|truncate(10) }} → Returns empty string.{{ 'short'|truncate(10) }} → Returns original.Custom Logic:
TwigTruncateExtension and register it as a compiler pass.No Config File:
Performance:
{% for item in hugeList %}{{ item.text|truncate }}...{% endfor %}). Cache truncated results if needed.Custom Truncation Logic:
use Dzango\TwigTruncateExtension\TwigTruncateExtension as BaseExtension;
class CustomTruncateExtension extends BaseExtension {
public function getFunctions() {
return array_merge(parent::getFunctions(), [
new \Twig\TwigFunction('customTruncate', [$this, 'customTruncate']),
]);
}
public function customTruncate($text, $length, $suffix = '...') {
// Custom logic here
return parent::truncate($text, $length, $suffix);
}
}
services.yaml:
services:
App\Twig\CustomTruncateExtension:
tags: ['twig.extension']
Integration with API Platform:
use ApiPlatform\Core\Serializer\SerializerContextBuilderInterface;
class ArticleOutput {
#[Groups(['default'])]
public string $contentPreview;
public function __construct(string $content) {
$this->contentPreview = $content;
}
}
// In your controller:
$preview = $article->content|truncate(150); // Hypothetical; use a custom serializer method.
How can I help you explore Laravel packages today?