16

I see that there are Trimmer and Truncate out there that have been created for truncating text but I don't believe either does what it is I'm looking for.

I'd like to truncate text similar to how Hacksaw for ExpressionEngine works. Allowing me to allow or disallow html tags, and making sure that even if it cuts off before a tag is closed, that it automatically properly closes the tag. I wouldn't want to turn the entire lower half of my page into a hyperlink for instance if i happen to truncate mid-link. I do want to maintain the ability for links to be in the text that is being truncated, and multiple paragraphs, etc. however.

Any tips would be fantastic.

1
  • Hacksaw has an open source license (MIT), so you could make a Craft version of it. Commented Jul 1, 2014 at 19:12

4 Answers 4

2

If you need to allow or disallow specific tags, you can use wordsmith plugin and its chop filter.

But if you don't need such advanced functionality, you can just use Twig macros.

In article Truncating text with Twig macros in Craft CMS on my blog I describe two macros - one that truncates by number of words and one that truncates by number of characters - while ensuring that truncated string does not have split words.

1
  • In 2022 this is a better solution, marked it as the new correct answer given it references Wordsmith as well as alternate methods. Commented Nov 26, 2022 at 11:32
18

https://github.com/ryanshrum/hacksaw

This hasn't been fully tested for production, or documented for that matter, but I have migrated it over and tested basic usage as per the docs on devot-ee. It should, at the very least, give you a nice start.

You can use the Twig filter with: {{ entry.fieldHandle|hacksaw(words="100") }}

6
  • Rock on Ryan, thanks for porting it over. Get it up on straightupcraft.com when you get a chance. This does exactly what i need (as far as my current needs), haven't tested it extensively. Commented Jul 1, 2014 at 21:20
  • After posting it originally, I went back and modified a few things after testing it a bit more thoroughly. I noticed one of the downfalls was as you said leaving HTML elements unclosed so I added in a new method to take care of that. I believe it now to function as the EE version does. I will work at getting it up on Straight up Craft as well as getting it fully documented here in the next couple days. Glad to help.
    – Ryan Shrum
    Commented Jul 1, 2014 at 21:44
  • Nice! tnx for sharing. Commented Feb 21, 2016 at 11:45
  • We see an issue with current version of Hacksaw where the content contains non-breaking spaces. They're being converted into little black diamonds with questions marks in them. Redactor loves to add non-breaking spaces, in my experience. Commented Jul 21, 2016 at 15:25
  • There's also another issue that it's not working currently well with text in Cyrillic (or maybe any Unicode enabled range of characters). Commented Nov 1, 2016 at 20:55
13

Doesn't answer the specific question but based on the title in search results it might be helpful for anyone just wanting to truncate a string:

{{ entry.title | length > 75 ? entry.title | slice(0, 75) ~ '...' : entry.title }}

Also doesn't require a plugin. Based on this.

0
2

Wordsmith plugin is very useful in manipulating the text. For example:

{{ entry.fieldHandle|chop(limit=100, unit='c', append=' ...') }}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.