dzango/twig-truncate-extension
Twig extension that truncates text while preserving HTML tags. Adds a truncate filter with options for length, ending, exact word cutting, and HTML handling—works with rendered markdown or raw content. Easy to register in Twig or Symfony2.
twig/bridge).Blade::directive() to create a custom truncate directive that wraps the Twig extension’s logic. Example:
Blade::directive('truncate', function ($expression) {
$truncate = new \Dzango\Twig\Extension\Truncate();
return "<?php echo \$truncate->getTruncateToken()->getNode()->getNode($expression)->render(1); ?>";
});
Usage: @truncate($string, 100, '...')TwigTruncateBundle simplifies integration in Symfony/Lumen but adds dependency overhead for Laravel.<table> with <thead>/<tbody>).domdocument).~1.0, which may conflict with Laravel’s bundled Twig (if using TwigBridge). Pin versions explicitly in composer.json.Str::limit()) suffice?{{ $text|truncate(50, '...') }} vs. @truncate($text, 50, '...'))?<a href="...">, <img src="...">)?TwigTruncateBundle if already using Symfony components.Str::limit() may suffice (no HTML awareness).twig/twig or twig/bridge by pinning versions.Str::markdown() + raw filter). ).dzango/twig-truncate-extension for updates (MIT license allows forks if needed).Str::limit()) for when HTML parsing fails.var_dump()).<script> tags safely").excerpt).| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Malformed HTML crashes truncation | Broken templates, 500 errors | Input sanitization (e.g., strip_tags() fallback). |
| Twig extension not loaded | Filter/directive fails silently | Validate Twig environment setup in tests. |
| Performance degradation under load | Slow response times | Cache truncated results; optimize HTML parsing. |
| Twig version incompatibility | Package breaks after Laravel update | Pin Twig version in composer.json. |
| Blade directive syntax errors | Runtime exceptions | Comprehensive test suite for directive usage. |
@truncate($text, 100) for HTML-aware truncation").How can I help you explore Laravel packages today?