Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Twig Truncate Extension Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Product Decisions This Supports

  • Content Moderation & UX: Enable clean truncation of long-form content (e.g., blog posts, comments, or product descriptions) in templates while preserving HTML structure (e.g., links, bold text) for a polished UI.
  • Performance Optimization: Reduce payload size for API responses or frontend rendering by truncating text dynamically without pre-processing content.
  • Multi-Format Compatibility: Support truncation of Markdown, raw HTML, or mixed content (e.g., {{ content|markdown|raw|truncate }}) in a single pipeline, reducing the need for custom logic.
  • Localization & Accessibility: Ensure truncated text respects word boundaries (via exact=false) for non-English languages or screen readers.
  • Roadmap Prioritization: Justify "build vs. buy" for text truncation by adopting a lightweight, MIT-licensed solution instead of reinventing wheel.
  • SEO & Metadata: Truncate meta descriptions or social media previews while preserving critical HTML tags (e.g., <strong>, <a>) for better rendering.

When to Consider This Package

  • Avoid if:
    • Your app uses non-Twig templating (e.g., Blade, Smarty) or lacks PHP/Twig integration.
    • You need client-side truncation (e.g., JavaScript libraries like Lodash’s _.truncate may suffice).
    • Your content is static or pre-processed (e.g., truncation happens in a CMS or database layer).
    • You require advanced truncation (e.g., handling nested HTML, SVG, or custom tags) beyond basic support.
  • Look elsewhere if:
    • You’re using Symfony 5+ without Twig: Consider the TwigTruncateBundle for seamless integration.
    • You need real-time truncation (e.g., live editing): Pair with frontend libraries or WebSockets.
    • Your team lacks PHP/Twig familiarity: Evaluate the learning curve for extension setup.

How to Pitch It (Stakeholders)

For Executives: "This lightweight Twig extension solves a common UX pain point—cluttered text in templates—by intelligently truncating content while preserving HTML structure. It’s a drop-in solution that reduces frontend bloat, improves load times, and ensures consistent formatting across platforms (e.g., web, APIs). With zero dependents and MIT licensing, it’s a low-risk, high-reward choice to standardize truncation logic, saving dev time and improving content display."

For Engineering: *"The dzango/twig-truncate-extension provides a battle-tested, configurable Twig filter to truncate text while respecting HTML tags. Key benefits:

  • Flexible: Customize length, ellipsis, and word boundaries via filter args.
  • Compatible: Works with Markdown, raw HTML, and nested pipelines (e.g., markdown|raw|truncate).
  • Lightweight: ~100 LOC, no external dependencies beyond Twig.
  • Easy to Integrate: Symfony2 support via bundle or manual service registration. Tradeoff: Requires Twig; for non-Twig stacks, we’d need a custom solution. Recommended for content-heavy apps where truncation is a recurring need."*
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
nasirkhan/laravel-sharekit
directorytree/privacy-filter-classifier
directorytree/privacy-filter
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata
splash/openapi
splash/scopes
splash/toolkit
testo/output-teamcity
testo/bridge-symfony