3

I'm using the Hacksaw plugin to truncate text and it does a great job except instead of rendering HTML entities such as "&", it's outputting them as "&". How can I correct this? Putting them in allow="" doesn't work. Do I need to do something to manipulate the text string first before applying the Hacksaw filter? Or is there a Hacksaw option that would correct this?

Edit for more info...

Example of text as it should appear:

Colour & Light; A House Re-crafted, cover & pg.99-108

Example of text output from Hacksaw:

Colour & Light; A House Re-crafted, cover & pg.99-108

Stupidly I hadn't looked at the HTML until now. It looks like the first & is rendering correctly but then there's a second partial entity after it?!

Template code I've tried:

{% set summary = article.newsArticle.type('text').first().text|hacksaw(words='35', append='...') %}

{% set summary = article.newsArticle.type('text').first().text|hacksaw(words='35', append='...')|raw %}

{% set summary = article.newsArticle.type('text').first().text|raw|hacksaw(words='35', append='...') %}

The order of plugin doesn't appear to have any effect. I get same result with all three of these arrangment above, including no Raw plugin at all.

Edit for answer

As per accepted answer below, final working code is:

{% set summary = article.newsArticle.type('text').first().text|htmlEntityDecode|hacksaw(words='35', append='...') %}

The Craft Helpers plugins did the trick.

2
  • Is this happening for just & used in URLs or any &?
    – Brad Bell
    Commented Nov 2, 2016 at 16:54
  • @BradBell no not in URLs. Any & in text from Rich Text fields. Commented Nov 5, 2016 at 22:32

3 Answers 3

5

You could try to fight that ampersand with the htmlEntityDecode filter that is included in my Craft Helpers plugin https://github.com/carlcs/craft-helpers

0
2

Try using the raw twig filter.

{{ variable|hacksaw(words='100')|raw }}

I did not test this answer since I have't used hacksaw yet. However I believe you can combine filters like this just fine.

http://twig.sensiolabs.org/doc/filters/raw.html

1
  • Thanks @spAnser for the idea but unfortunately it didn't work. The output is still showing the the entity name rather than rendering it. Commented Nov 1, 2016 at 21:12
1

I have used hacksaw on a few projects, but I haven't come across this before so I can't replicate it. The above answer may work but you could try reordering it to:

{{ entry.thing|raw|hacksaw(words='100') }}

If this doesn't work, I'll try and replicate it. Can you paste the code for us?

1
  • 1
    Thanks Jay. Tried it in that order and still no luck. I've edited my original question with some more details. Commented Nov 5, 2016 at 22:39

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.