I have used slice filter to display few number of Japanese alphabets from a sentence.

{{ desc|length > 65 ? desc|slice(0, 65) ~ '...' : desc }}

But sometimes it slices very early when a Japanese full-stop appears and the preceding sentence has long length for full-stop to appear.

I have 3 patterns to show:

1) When I have following text to slice:


It slices the above thing as:


2) For following string


It slices to:


which is perfectly sliced to 65 alphabets.

3) For following string:


It slices to:


Is there any way to solve it or any other solution to achieve this?

  • Seems a little bit as if it slices to the nearest full stop if there is any inside the specified 65 character range. – leymannx Jun 13 '18 at 13:13
  • 1
    Your example 1 and 3 don't have the three dots ... added at the end. So this most likely means desc.length is less than 65 when it comes from the backend. Are you sure the text is not being trimmed by the Display manager or a preprocessor or something else before it comes to the twig template? – Beebee Jun 13 '18 at 13:23
  • 1
    @leymannx yes agree, full-stop trim seemed a very drupal thing to do, not really a twig slice thing. – Beebee Jun 13 '18 at 13:25
  • 2
    @Akshay what you don't understand about Display Manager's "Trim" is that it's most likely on a "Sentence" trim mode. So if it sees a full-stop, it trims there, even if there's more words available. So setting to 80 is too close. It might still generate a sentence that's 64 chars or less due to the full-stop. Try with 200-300 and see. Better yet, don't set a limit on Display manager, output the full body. – Beebee Jun 13 '18 at 14:02
  • 1
    @Beebee Ok. No issues. Just to suggest others, use only single trim at a time. – Akshay Jun 13 '18 at 15:21

As the trim was enabled from both twig as well as display format, it was not working. So keeping the display format for that field to Default, it solved my issue.

For other users who might face such problem, just keep in mind to set only single trim (either from back end or front end side)

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