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


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

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

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

But if you dont need such advanced functionality, just truncating strings by words, you dont need plugin at all. Macro will take care of it.

{% macro truncate(text, limit, append) %}
{# settings #}
{% set suffix = append|default('...') %}
{% set punctuation = ['.',',','-',':',';'] %}
{# logic #}
{% set array = text|split(' ') %}
{% set arrayTruncated = array|slice(0, limit) %}
{% set string = arrayTruncated|join(' ') %}
{% if limit and array|length > limit %}
    {% for mark in punctuation %}
    {% set string = string|trim(mark) %}
    {% endfor %}
    {% set string = string ~ suffix %}
{% endif %}
{{ string }}
{% endmacro %}

This macro also strips punctuation marks from end of strings to avoid results like this:

lorem ipsum;...

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.