1

I'm using the following code in my template to truncate the body of an entry (coming from a Rich Text field):

{{ entry.body | slice(0, 500) | markdown }}

To this, I'd like to add ... as a string, so that it will output like:

Words words words...

Since the field part I'm using outputs into its own <p> tag, adding the ... into the HTML itself has them sitting in two different tags. I've tried using |join but I must be doing something wrong, its putting the first part into a separate <p> tag than the ...

Thanks for your help!

2 Answers 2

2

Try something like

{{ (entry.body | slice(0, 500) ~ '...') | markdown }}
2
  • This is probably really close, but it's not converting it to markdown. Is there a smarter way of outputting the Rich Text's content?
    – jasonetco
    Commented Mar 19, 2016 at 19:40
  • 1
    This works perfectly: {{ (entry.body | slice(0, 500) ~ '...') | markdown }}
    – jasonetco
    Commented Mar 19, 2016 at 19:51
3

You might find the Hacksaw plugin useful for this https://github.com/ehousestudio/craft_hacksaw/ this will truncate text for you and has a wealth of options available for doing so, specifically the append parameter, which will do exactly what you want :)

1
  • Cool, thanks! I'd rather not use a plugin, but this will be my back up answer :)
    – jasonetco
    Commented Mar 19, 2016 at 19:41

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.