2

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
2

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.