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.

  • Hacksaw has an open source license (MIT), so you could make a Craft version of it. – Marion Newlevant Jul 1 '14 at 19:12
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") }}

  • 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. – Christopher Healey Jul 1 '14 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 Jul 1 '14 at 21:44
  • Nice! tnx for sharing. – Martijn La Feber Feb 21 '16 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. – Ryan Masuga Jul 21 '16 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). – certainlyakey Nov 1 '16 at 20:55
11

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.

  • Thanks! This worked perfectly. Can you explain what it's doing exactly? Is the ? an if else? – mateostabio Mar 19 at 18:02
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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